I am new to coding and was wondering why my code is not acting the way I intended. I build the code as a training program to help me practice what i have learned. The code seems to work fine, as expected, when I comment out lines 19 - 23,the multi line comment section, but when I don't that's when it starts to act unexpectedly.
#include <iostream>
int askUserNumber ()
{
std::cout << "Please input a number: " << std::endl; //ask user to input number
int userNumber; //define integer to store input
std::cin >> userNumber; // get input from user
return (userNumber); // return input to caller
}
void printUserNumber (int userNumber)
{
std::cout << "Your number input was: " << userNumber << std::endl; //print input to user screen
}
int main ()
{
/*
std::cout << "Please input your name: " << std::endl; // ask user name
int userName; // variable to store user name
std::cin >> userName; // get user name
*/
printUserNumber(askUserNumber()); // call to askUserNumber to get user input to print to screen
return(0);
}
Results(Without Commenting out code):
Please input your name:
Corey
Please input a number:
Your number input was: "someGarbageNumber"
It does't let me input a number at that part and it just prints out a random number.
Results(With Commenting out code):
Please input a number:
7
Your number input was: 7
This time it runs perfectly and there are no problems. I don't understand why the three lines asking for the users name cause it to not allow me to input a number when the function call for printUserNumber(askUserNumber()) gets to the point that it prints "Please input a number:" and doesn't allow me to input one. Thank you for any responses. I hope I wrote my question in a good format, and explained my problem in a clear and straight forward way. Hopefully the results will section will also allow for some clarity. Looking forward to a solution thanks again.