Easy Tutorial: Computer Programming for DUMMIES (p7) -- very beginner friendly!

The following tutorial is about basic input and output. I have already gone over one function that handles basic output. This was 'cout.' This tutorial will be in C++, but some of these functions will apply to C also. C++ was derived from C, so many of the functions that you can use in C can also be used in C++. If you want to catch up on this tutorial series, here are links to the others:

Part 1: Hello World!

Part 2: Variables

Part 3: Functions

Part 4: if(), else, else if()

Part 5: Loops

Part 6: Arrays

Functions only in C++ -- include the iostream file

  • cout
  • cin

Functions available to both C++ and C -- include the stdio.h file

  • printf()
  • scanf()
  • fgets()
  • puts()
    Note: we are using fgets() instead of gets() because gets() may lead to errors. The user may input more characters than the size of the character array we store the input in allows. With fgets(), we can choose how many characters we store in the array so that we will never go over the limit. This will make more sense later.

cout

We have already gone over this function in this tutorial series, but we can refresh our memory.
The following code...

cout << "Your message goes here.";

...outputs...

Your message goes here.

...to the screen. More specifically, it prints to stdout (standard output -- your screen).

cin

The function 'cin' is basically the opposite of 'cout.' It takes in input from the user. Here is an example:

int a;
cout << "Enter an integer: ";
cin >> a;
cout << "You entered: " << a;

The output looks like this (user input is in bold):


Enter an integer: 3
You entered: 3


Pretty simple!

printf()

The printf() statement basically serves the exact same purpose as 'cout.' It outputs to the screen, but it is a little harder to use. The parameters that go in the parenthesis are, first, quotations for a string to go in. You cannot put variables inside the quotes. Instead, you must put a specifier. Second, you place the variables you want respectively to the specifiers, and be sure to separate them by commas. I know that didn't make much sense yet, but we can look at an example:

int x = 5;
printf("The variable x contains: %d", x);
  • %d is the specifier used for type int

The output:

The variable x contains: 5

You don't have to put any characters in between the quotes. You can put only specifiers if you want:

int a = 1;
string b = "string";
char c = 'P';
printf("%d %s %c", a, b, c);
  • %s is the specifier for a string
  • %c is the specifier for a character

Output:

1 string P

Also, you do not have to use any specifiers. You can just output a string:

printf("Hello world!");

Output:

Hello world!

Here are the specifiers:

(picture taken from: http://www.cplusplus.com/reference/cstdio/printf/)

scanf()

This is the input equivalent of printf(). It is basically the same syntax, just a few differences. If you are scanning for data, you must put a '&' before the variable you want to store the data in if the variable is not a pointer. That dereferences the variable. What that means is that the memory address of the variable is passed, not the variable itself. This is what I mean by that:

int data;
scanf("%d", &data);

This scans stdin (user input) and places the integer in the variable 'data.' You do not have to put a '&' before a string variable. A character array is already treated as a pointer (to the memory address).

char data[50];
scanf("%s", data);

Here is a more thorough example:

int x;
printf("Enter an integer: ");
scanf("%d", &x);
printf("You entered: %d", x);

Output (user input in bold):


Enter an integer: 21
You entered: 21


fgets()

When reading from stdin, the function fgets() takes user input up to a new line, and stores it into a character array (it scans until the user presses enter, or if the user has entered the maximum amount of characters). It has 3 parameters:

  1. The character array you are storing the input in.
  2. The amount of characters to be scanned.
  3. A file pointer (fgets() can scan from a file, but we aren't doing that right now. We just want to scan user input. 'stdin' (user input) is actually a file pointer, so we can use this!

Here is an example:

char data[500];
cout << "Enter up to 500 characters:\n";
fgets(data, 500, stdin);
cout << "You entered: " << data;

The output will look like this (again, user input is in bold)


Enter up to 500 characters:
A bunch of characters......
You entered: A bunch of characters......


Note that since 'data' could only hold 500 characters, I set the maximum amount of characters fgets() would scan to be 500. The problem with the function gets() is that the user could enter way more than 500 characters, and bad things would happen!

puts()

The function puts() writes a string to stdout (the screen) and adds a new line.
Example:

puts("Hey!");

Output:

Hey!

Example 2:

char string[] = "Hey!";
puts(string);

Output:

Hey!

Super simple!

Here is a program I wrote to help you understand this better:

#include<iostream>
#include<stdio.h>
#include<string>
using namespace std;

int main()
{
    cout << "Enter a sentence (100 character maximum): ";
    char input[100];
    fgets(input, 100, stdin);
    cout << "You entered: ";
    puts(input);

    
    cout << "Hello World!" << endl;
    printf("Hello World... Again!\n");
    puts("Hello World, yet again!\n");

    char prompt[] = "Enter 5 integers: ";
    printf("%s", prompt);
    int a, b, c, d, e;
    scanf("%d %d %d %d %d", &a, &b, &c, &d, &e);
    
    printf("You entered the values: %d %d %d %d %d\n", a, b, c, d, e);

    cout << "Enter 5 more values: ";
    cin >> a >> b >> c >> d >> e;
    cout << "You entered: " << a << " " << b << " " << c << " " << d << " " << e << endl << endl;

    char string[25];
    cout << "Enter a string (maximum of 25 characters -- no spaces): ";
    scanf("%s", string);
    cout << "You entered: " << string << endl;
    return 0;
}

Here is the output (user input in bold):


[cmw4026@omega test]$ g++ io.cpp
[cmw4026@omega test]$ ./a.out
Enter a sentence (100 character maximum): I like typing for long periods of time..
You entered: I like typing for long periods of time..

Hello World!
Hello World... Again!
Hello World, yet again!

Enter 5 integers: 1 2 3 4 5
You entered the values: 1 2 3 4 5
Enter 5 more values: 5 4 3 2 1
You entered: 5 4 3 2 1

Enter a string (maximum of 25 characters -- no spaces): supercalifragilistic
You entered: supercalifragilistic
[cmw4026@omega test]$


I hope this helped you learn something! Please leave suggestions in the comments.

Coin Marketplace

STEEM 0.20
TRX 0.12
JST 0.028
BTC 64182.86
ETH 3531.12
USDT 1.00
SBD 2.53