-
switch function
hi my program keeps skiping my switch function any ideas??
Code:
int main (void)
{
people datab;
store tdata;
char any;
char choice;
char choice2;
float count = 0;
float count2 = 0;
int c;
tdata.clearary();
cout<<"\nThis program will allow the user to create and then control a database. \n";
cout<<"what would you like to do? \n To enter a new record press n \nTo delete a record press d \nTo view a full list of the users with details press f \nTo view serch options press s\n to quit press q :- ";
cin>>choice;
while(count == 0)
{
choice = getchar ();
switch(choice)
{
case 'n':
{
system("cls");
tdata.load();
tdata.enter(1);
tdata.save();
count = 1;
break;
}
case 'd':
{
//delete a record ??
count = 1;
break;
}
case 'f':
{
system("cls");
tdata.list();
count = 1;
break;
}
p.s this is just a snipit also using quincy
-
Choice will be '\n' the first time in the loop. You're not handling that case.
Kurt
-
-
will read a char and leave '\n' in the buffer.
Code:
choice = getchar ();
getchar will return '\n'.
Kurt
-
-
-
but wouldnt that stop me getting the users answer
-
Code:
choice = getchar ();
Should reads the users choice as well.
BTW it is never a good idea to mix c-style with c++ style IO.
Kurt
-
could you fix this for me as well as that worked fine
Code:
char k = 'y';
while (k == "y");
-
If you use doublequotes, the char inside will be treated as a C-style string. That's wrong, although the compiler might be extra smart about it. Use single quotes for single chars.
-
-
That shouldn't compile.
try
Code:
char k = 'y';
while (k == 'y'); // this would be an infinite loop. Do you really want the ;
Kurt
-
thanks i didnt notice that
-
k i got it mostly working but for some reason the first two questions print in a single line
Code:
void people::enter(int i)
{
char k = 'y';
while (k == 'y')
{
cout << "Enter the name of the person ";
gets(np[i].name);
cout << "Enter the phone number of the person ";
gets(np[i].phone);
cout << "Enter the full address of the person ";
gets(np[i].address);
cout << "Enter the Email address of the person";
gets(np[i].email);
cout << "enter the name of the party that the person belongs to ( conservative, labour, liberal, other)";
gets(np[i].party);
cout << "Do you wish to enter another member ? (y/n)";
cin >> k;
}
}
any idea
-
Let me guess. Before calling that function you are using getchar() to read a character.
You will have to get rid of a leftover '\n' in the input buffer.
cin.ignore() could help.
Kurt