-4
#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.

JaMiT
  • 14,422
  • 4
  • 15
  • 31
Skaltag
  • 5
  • 2
  • 3
    Use a debugger to step through the code. Notice that the `cin >> input` fails. Because `evin` is not a valid integer. – Raymond Chen Oct 22 '21 at 13:32
  • 2
    *"I dont understand why the program skips the userInput function."* -- your output indicates that the function is *not* skipped, as that is the source of the lines `Give me your 1:` and `Give me your 2:`. More precisely, your confusion lies in a single line within that function. (Precision is valuable when debugging.) – JaMiT Oct 22 '21 at 13:33
  • Maybe you wanted the person to enter their first name and not a single character. If so `char Letter= ' ';` should be `std::string firstName;` and update the code to use the new variable. With that said names are a little difficult because if the person enters a space everything after the space will be left in the buffer for the next input causing the same problem you had with your current code. I mention that because some people have first names like `Mary Jo` – drescherjm Oct 22 '21 at 13:33
  • 1
    Please try to ask questions in a better way: the questions title is generic and not useful for people who may search for the same thing in the future and you did not describe any efforts you made to solve the issue. – pattakosn Oct 22 '21 at 13:44
  • 1
    If you want to answer your own question, you may do so. However, you should not answer by editing your question. Answer by posting an answer. – JaMiT Oct 23 '21 at 06:24

3 Answers3

2

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.

Marek R
  • 32,568
  • 6
  • 55
  • 140
1

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.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
0
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).

pattakosn
  • 365
  • 4
  • 13
  • You have rephrased the question. Do you have an answer, an explanation of "how cin/cout work when used with the >> operator for different kind of variables"? Answering a question with "you have to know the answer" is not really an answer. – JaMiT Oct 22 '21 at 13:36
  • That's an interesting perspective. You are saying that if the OP had done enough research to suspect where the issues are coming from, then the question should be closed for lack of effort. However, since the OP put in **less** effort than that, you believe the question should get an answer (rather than putting that guidance in a comment). – JaMiT Oct 23 '21 at 06:10
  • Either the OP knew where the problem came from but did not bother trying to find a solution/inform us of his efforts (in which case I do not want to spoon feed him) or (s)he geniounsly did not know in which case I know it is very difficult to search on your own unless someone helps/guides you. I did guide her, I just did not give him the answer as the other answers did. – pattakosn Oct 25 '21 at 12:04