In the below code, I'm running into an error when I try to get the user to input their name. My program just skips it over and goes right over to making the function calls without allowing the user to enter their name. Despite the error, my program is compiling. I'm not sure what's going wrong as I wrote that part based off other examples I found on here. Any suggestions?
#include <iostream>
#include <string>
#include <time.h>
using namespace std;
char showMenu();
void getLottoPicks(int[]);
void genWinNums(int[]);
bool noDuplicates(int[]);
const int SIZE = 7;
int main()
{
int userTicket[SIZE] = {0};
int winningNums[SIZE] = {0};
char choice;
string name;
srand(time(NULL));
do
{
choice = showMenu();
if (choice == '1')
{
cout << "Please enter your name: " << endl;
getline(cin, name);
getLottoPicks(userTicket);
genWinNums(winningNums);
for (int i = 0; i < SIZE; i++)
cout << winningNums[i];
}
} while (choice != 'Q' && choice != 'q');
system("PAUSE");
return 0;
}
Added the code for showMenu:
char showMenu()
{
char choice;
cout << "LITTLETON CITY LOTTO MODEL:" << endl;
cout << "---------------------------" << endl;
cout << "1) Play Lotto" << endl;
cout << "Q) Quit Program" << endl;
cout << "Please make a selection: " << endl;
cin >> choice;
return choice;
}
And getLottoPicks (this part is very wrong and I'm still working on it):
void getLottoPicks(int numbers[])
{
cout << "Please enter your 7 lotto number picks between 1 and 40: " << endl;
for (int i = 0; i < SIZE; i++)
{
cout << "Selection #" << i + 1 << endl;
cin >> numbers[i];
if (numbers[i] < 1 || numbers[i] > 40)
{
cout << "Please choose a number between 1 and 40: " << endl;
cin >> numbers[i];
}
if (noDuplicates(numbers) == false)
{
do
{
cout << "You already picked this number. Please enter a different number: " << endl;
cin >> numbers[i];
noDuplicates(numbers);
} while (noDuplicates(numbers) == false);
}
}
}
After doing
cin >> choice;
insidechar showMenu()
, if a user inputs1[ENTER]
, thechar
consumes 1 character from cin, and the newline stays inside the stream. Then, when the program gets togetline(cin, name);
, it notices that there's still something insidecin
, and reads it. It's a newline character, sogetline
gets it and returns. That's why the program is behaving the way it is.In order to fix it - add
cin.ignore();
insidechar showMenu()
, right after you read the input.cin.ignore()
ignores the next character - in our case, the newline char.And a word of advice - try not to mix
getline
withoperator >>
. They work in a slightly different way, and can get you into trouble! Or, at least remember to alwaysignore()
after you get anything fromstd::cin
. It may save you a lot of work.This fixes the code:
from looking at code
showMenu
function has problem. and it's not returning asccii equivalent of'1'
that is: 31 integer. try printing value returned byshowmenu
. you will get thatUPDATE: It is because
cin
in delimited by' '
(whitespace) andgetline
by'\n'
character, so when enter name and press entercin
inshowmenu
will consume whole string except'\n'
fromistream
and that is read bygetline
. to see this when it ask for choice enter string like1 myname
(1 whitespace myname)and press ENTER will display name. nowcin
will read 1 inchoice
andmyname
in name bygetline
.