Home > Mobile >  Can't input integers after reading a character
Can't input integers after reading a character

Time:10-23

#include <iostream>
using namespace std;

int Variable() {
    char Letter= ' ';
    cout << "Your Name: ";
    cin >> Letter;
    cout << "Your first Letter is : " << Letter;

return 0;
}

int userInput(int number)
{
    int input = 0;
    cout << "Give me your " << number << ". number: ";
    cin >> input;
    cout << "\n";
    return input;
}


int main() {

    Variable();

    int input1 = 0;
    int input2 = 0;


    input1 = userInput(1);
    input2 = userInput(2);

    cout << "Result: " << (input1 * input2) << "\n\n";
    

    return 0;
}

Output:

Your Name: Kevin
Your first Letter is : K

Give me your 1:
Give me your 2:
Result: 0

I write in the console a name and after that I get the first Letter, but after that I cant enter the two numbers because the programends. I dont understand why the program skips the userInput function.

Edit:

#include <iostream>
using namespace std;

int Variable() {
    char name[MAX_NAME_LENGTH];
    cout << "Your Name: ";
    cin.ignore(0);
    cin.getline (name, MAX_NAME_LENGTH);
    char firstLetter = name[0];
    cout << "Your first Letter is: " << firstLetter << endl;
return 0;
}

Now the other cin in int userInput(int number) works and you can also enter names like Mary Jo without problems. Thanks for your help.

CodePudding user response:

Basically problem is that you prompt user to provide name and you are reading only one character K.

Outcome is that when you try read number there are still some letters waiting for reading evin. So attempt to read integer leads to an error and value of input remains unchanged.

Once std::cin enters error state all other reads will fail until you will clear error flag and discard characters which reading caused error.

CodePudding user response:

At a guess, you are supposing that cin >> reads input line by line, discarding whatever data it does not assign to the right-hand operand.

However, although you have to provide input in units of full lines when you run the program interactively, the input is not, in general, consumed that way. Roughly speaking, each >> operation will consume as much data from cin as is appropriate for a value of the type of the right-hand operand, leaving the rest to be consumed by later operations.

Thus, with ...

    char Letter= ' ';
    cout << "Your Name: ";
    cin >> Letter;

... only one character is read from the input into Letter, because Letter is a char. In your example, that character is a 'K', and evin\n is left waiting to be read.

When you then attempt to read into an int with ...

    int input = 0;
    cout << "Give me your " << number << ". number: ";
    cin >> input;

... the >> operation fails (without consuming any input) because the next data waiting to be read cannot be interpreted as an int.

CodePudding user response:

char Letter= ' ';
cin >> Letter;
int input = 0;
cin >> input;

You need to look into how cin works. Letter is a char and input is an int. you ask for a char from cin and then for an int. You need to understand/learn how cin/cout work when used with the >> operator for different kind of variables (int and char in your case).

CodePudding user response:

Since you are trying to prompt the user to enter numbers you need to include a function that pauses execution flow and waits for user input. One example is _getch() included in <conio.h>

Here is an example of your modified code:

#include <iostream>
#include <conio.h>

using namespace std;

int Variable() {
    char Letter = ' ';
    cout << "Your Name: ";
    cin >> Letter;
    cout << "Your first Letter is : " << Letter << endl;

    return 0;
}

int userInput(int number)
{
    char input;
    cout << "Give me your #" << number << " number: \n";
    input = _getch();
    cout << "i: " << input << endl;
    return input;
}


int main() {

    Variable();

    int input1 = 0;
    int input2 = 0;

    input1 = userInput(1) - '0';
    input2 = userInput(2) - '0';

    cout << input1 << endl;    
    cout << input2 << endl;
    cout << "Result: " << (input1 * input2) << "\n\n";

    return 0;
}
  • Related