| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Newbie simple program issue
Hi,
i have a problem with a simple section of programming i am writing. I am using console in and out (still new to C++ )Code:
int main(void)
{
int choice = 0;
do//Menu loop//
{
cout << "xxxxxxxxxxx...........\n";
cout << "xxxxxxxxxxx(enter corresponding number):";
cout << "\n";
cout << "1. xxxx\n";
cout << "2. xxxx\n";
cout << "3. xxxx\n";
cout << "\n";
int choice ;
cout << "Enter choice:";
cin >> choice;
}
while (choice < 0 ||choice > 4);//loop conditions//
{
switch (choice)
{
case 1:
cout << "Opening port, press power shortly" << endl;
break;
case 2:
cout << "Opening port, press power shortly" << endl;
break;
case 3:
cout << "Opening port, press power shortly" << endl;
break;
default:
cout<<"Invalid option!" <<endl;
break;
}
return 0; }
As you can see, i have just include the switch options not their functions. program compiles perfectly and runs. But when i get to the stage where i have to enter in a "choice" either 1, 2 or 3 i do so and hit enter but the program exits without errors. Surely Opening port, press power shortly should be output? Really doing my head in, sure the answer is sooooooooo obvious but not for newbiw like me ![]() Many thanks people ![]() |
|
#2
|
||||
|
||||
|
I think this might answer your question, just before the return 0 at the bottom put either==>
cin.get(); system("PAUSE"); or just declare another variable at the top and put another cin>>statement; all of these should work in pausing the program until the user enters some form of information, depending on what compiler you are using. If you are using an older compiler you will need to include the library #include <stdlib.h> at the top to use system(). Hope this helps ya out a little =Þ OH ya, and in your "while" condition I would probably use a 1 rather than 0 because if you set it to less than 0 the user can still enter a 0 and be correct but just a thought try it out and see what you get =Þ both ways and youll see what I mean. |
|
#3
|
|||
|
|||
|
Thankyou very very much. Legend
![]() |
|
#4
|
||||
|
||||
|
np anytime I can help, which isnt very often =) Just a newb myself.
GeO |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Newbie simple program issue |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|