Program Run Non-Stop

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <conio>
int main ()
{
	int choice=0;
   cout<<"\nWELCOME TO CYBER BREAKFAST\n";
   cout<<"\n1: Nasi Lemak with Kopi O";
   cout<<"\n2: Roti Canai with Teh Tarik";
   cout<<"\n3: Scramble Edd with Iced Milo";
   cout<<"\n4: Mixed Cereals with Strawberry Shake";
   cout<<"\n5: No thank you, I'm overweight..!\n\n";

   while (choice<1||choice>5)
   {
   cout<<"Enter your choice: ";
   cin>>choice;
   }

   switch(choice)
   {
   case 1: cout<<"\nThat will be RM6.50";
   			break;

   case 2: cout<<"\nThat will be RM4.50";
   			break;

   case 3: cout<<"\nThat will be RM5.00";
   			break;

   case 4: cout<<"\nThat will be RM8.50";
   			break;

   default: cout<<"\nYou look just great..!";
   }

   cout<<"\nPlease take a seat and make yourself comfortable.";
   cout<<"\n\nEnd Of Program";
   getch();
   return 0;
}


Hi, may i know how to make this code continue to ask for input like when I insert "7" it will go to this while loop while (choice<1||choice>5) ...but how if I want to input character such as "a"? Because when I怀input character, the program will run infinitely...I don't want that...thanks...
1
2
3
4
char choice = 0;
while (choice < 'a' || choice > 'z')
{
}
Thanks Stewbond, but can I have both inside the program? Character and Integer, since the code u give me is the replacement to the integer one...TQ...
If cin is unable to extract a number from the input stream in line 16, cin will put itself into a fail state. You can check cin's state to see if it encountered something it didn't expect:

13
14
15
16
17
 while ( cin && (choice<1 || choice >5) )
{
     cout << "Enter your choice: " ;
     cin >> choice ;
}


Hi,

I'm not quite sure what you're trying to do. Could you explain it again?

Also, when you input choice (cin>>choice;, a "better" way to do it would be to write: choice=getch();

This means that when the user presses a key, that key is stored inside the variable. As choice is an integer, it cannot hold characters. Inputting a character will therefore likely cause a crash or error. A simple way around this is to make choice an std::string This means letters or numbers can be inputted. However, a switch statement can only, to my knowledge, hold integers, so you would have to do if statements. You then have to compare them with a string literal, like this
1
2
3
if (choice=="1") {
    //execute code...
}


Then at the end, if it wasn't one of your choices, tell it to display a message and go to the beginning, with:
1
2
3
else {
    std::cout<<//error message;
}


A goto statement wouldn't be amiss here, to keep it running smoothly (see the final code at the bottom for where to put it)

Then encompass this with a while (true) statement to keep it going infinitely. The resulting code might look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

#include <iostream>
#include <conio.h>

int main () {
   std::string choice=0;
   while (true) {
       std::cout<<"\nWELCOME TO CYBER BREAKFAST\n";
       std::cout<<"\n1: Nasi Lemak with Kopi O";
       std::cout<<"\n2: Roti Canai with Teh Tarik";
       std::cout<<"\n3: Scramble Edd with Iced Milo";
       std::cout<<"\n4: Mixed Cereals with Strawberry Shake";
       std::cout<<"\n5: No thank you, I'm overweight..!\n\n";
       enterChoice:
       std::cout<<"Enter your choice: ";
       choice=getch();

       if (choice=="1") {
          std::cout<<"\nThat will be RM6.50";
       }

       else if (choice=="2") {
          std::cout<<"\nThat will be RM4.50";
       }

       else if (choice=="3") {
          std::cout<<"\nThat will be RM5.00";
       }

       else if (choice=="4") {
          std::cout<<"\nThat will be RM8.50";
       }

       else if (choice=="5") {
          std::cout<<"\nYou look just great!";
       }

       else {
          std::cout<<"Please enter a valid character.";
          goto enterChoice;
       }

       std::cout<<"\nPlease take a seat and make yourself comfortable.";
   }
   std::cout<<"\n\nEnd Of Program";
   getch();
   return 1;
}


That makes it run infinitely, take input that it then checks to see if it matches any of your numbers, 1-5, and if it doesn't, takes you back up to the top and lets you input again.

Hope this is what you were after, if not then I'm sorry.

Thanks,
hnefatl
OR you can do is in your while-loop use two more logical - OR Operators and define the use of character 'a' and 'z'.

 
      while (choice < 1 || choice > 5 || choice>='a' || choice <='z')



Hope this helps......

Ans also to note that in your original programming, which u have posted as/in your question, if the user enters '5' as the input then it would display the 'default' statement(s). So I suggest you get a line which may look like this:

1
2
case 5 : (statement)  ;
         break;
Please, disregard everything Hnefatl wrote.

Mixing stream and non-standard console i/o is never a good idea. He seems to be misguided about the values int and char variables can hold. goto is not something a beginner should use (rarely a veteran, either.) And his code will not compile.
I apologize for my solution, I had a power-cut which prevented me from seeing the other answers posted by members, so misunderstood the original problem.

Apologetically,
hnefatl
Last edited on
Thanks!
@hnefatl
I could actually use char inside switch loop...and what is choice=getch(); actually? Not really understand...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <conio>
int main ()
{

   char choice=0;
   cout<<"\nWELCOME TO CYBER BREAKFAST\n";
   cout<<"\n1: Nasi Lemak with Kopi O";
   cout<<"\n2: Roti Canai with Teh Tarik";
   cout<<"\n3: Scramble Edd with Iced Milo";
   cout<<"\n4: Mixed Cereals with Strawberry Shake";
   cout<<"\n5: No thank you, I'm overweight..!\n\n";

   while (choice<'a'||choice>'z')
   {
   cout<<"Enter your choice: ";
   cin>>choice;
   }

   switch(choice)
   {
   case 'a': cout<<"\nThat will be RM6.50";
   			break;

   case 'b': cout<<"\nThat will be RM4.50";
   			break;

   case 'c': cout<<"\nThat will be RM5.00";
   			break;

   case 'd': cout<<"\nThat will be RM8.50";
   			break;

   default: cout<<"\nYou look just great..!";
   }

   cout<<"\nPlease take a seat and make yourself comfortable.";
   cout<<"\n\nEnd Of Program";
   getch();
   return 0;
}


====================================================

cire, can I know why cannot use goto code? It seem like make a complexity become easier because only need to use goto abc; and abc: which can direct go back to the location I want...

is that string can read int and char? I not really understand much about string, double and so on...can u help? TQ...
====================================================

@absawis

I tried ur method, but the it will still go infinitely if i press "a" for example...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <conio>
int main ()
{
	int choice=0;
   cout<<"\nWELCOME TO CYBER BREAKFAST\n";
   cout<<"\n1: Nasi Lemak with Kopi O";
   cout<<"\n2: Roti Canai with Teh Tarik";
   cout<<"\n3: Scramble Edd with Iced Milo";
   cout<<"\n4: Mixed Cereals with Strawberry Shake";
   cout<<"\n5: No thank you, I'm overweight..!\n\n";

   while (choice < 1 || choice > 5 || choice>='a' || choice <='z')
   {
   cout<<"Enter your choice: ";
   cin>>choice;
   }

   switch(choice)
   {
   case 1: cout<<"\nThat will be RM6.50";
   			break;

   case 2: cout<<"\nThat will be RM4.50";
   			break;

   case 3: cout<<"\nThat will be RM5.00";
   			break;

   case 4: cout<<"\nThat will be RM8.50";
   			break;

   default: cout<<"\nYou look just great..!";
   }

   cout<<"\nPlease take a seat and make yourself comfortable.";
   cout<<"\n\nEnd Of Program";
   getch();
   return 0;
}
Hi,

If using a char inside your loop works, then yes, you could. I used int because my compiler refused an std::string in the switch statement, so I used an int because I knew it worked. Sorry for causing confusion with that.

choice=getch(); means that when the user press a key (the getch() part, it is stored in the variable choice. It's just like writing choice=5, or choice=0, but it takes the return value of the function getch() and puts it into the variable. Normally functions return an integer, or true/false; for an integer return value, you need an integer variable to hold it. Likewise, for a boolean return value (true/false, yes/no, on/off) you need a bool variable. That's the same for any function, so if you make a function and have it return a number, you can write name of variable=name of function();, and whatever the function returns will be stored in the variable.

Also, if I may elaborate as to why not to use goto, it is because it makes code hard to read; imagine you make a program 1000 lines long, and when you read it through, you have to keep going back to search for the goto link you created earlier! It makes it harder to understand where the program is going. I use them occasionally when I'm really stuck as to how to do something, but they should be used as little as possible, preferably never. Instead, you should use loops well, to create the same desired effect, as they are far easier to understand as long as you lay your code out correctly, which it looks like you already do, having seen your code.

I hope I've helped you understand functions and getch() a little better.

Have a look at this article: I'm sure it explains this topic far better than I: http://www.cplusplus.com/doc/tutorial/functions/

Please tell me if I've made a mistake.

Thanks,
hnefatl
Last edited on
 
while((choice < '1' || choice > '5') && (choice < 'a' || choice > 'z')) //Fixed condition 


why cannot use goto code?
Bad practice, don't use them, you will never need to use one in class assignments... ever, so why start?
Last edited on
Thanks!

@clanmjc
1. I tried to change intinto charas u use '1' and '5', is this correct way to change it? I don't know about the meaning of '1' but for 'a' I know it shows it as char...

2. Ur code I don't know why still not works...but i try the below code, it works...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <conio>
int main ()
{
	char choice=0;
   cout<<"\nWELCOME TO CYBER BREAKFAST\n";
   cout<<"\n1: Nasi Lemak with Kopi O";
   cout<<"\n2: Roti Canai with Teh Tarik";
   cout<<"\n3: Scramble Edd with Iced Milo";
   cout<<"\n4: Mixed Cereals with Strawberry Shake";
   cout<<"\n5: No thank you, I'm overweight..!\n\n";

   while ((choice<'1'||choice>'5')&&(choice>='a'||choice<='z'))
   {
   cout<<"Enter your choice: ";
   cin>>choice;
   }

   switch(choice)
   {
   case '1': cout<<"\nThat will be RM6.50";
   			break;

   case '2': cout<<"\nThat will be RM4.50";
   			break;

   case '3': cout<<"\nThat will be RM5.00";
   			break;

   case '4': cout<<"\nThat will be RM8.50";
   			break;

   default: cout<<"\nYou look just great..!";
   }

   cout<<"\nPlease take a seat and make yourself comfortable.";
   cout<<"\n\nEnd Of Program";
   getch();
   return 0;
}


Can u pls explain this whole line? I don't know how it run actually...TQ...
while ((choice<'1'||choice>'5')&&(choice>='a'||choice<='z'))

Well, that condition is almost certainly a mistake. Entering a capital letter or a punctuation mark will cause the program to skip directly to the end. You could use

while ( choice< '1' || choice > '5')

I would probably stick with choice being an int and checking the state of the input stream.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
int main ()
{
   int choice=0;
   cout<<"\nWELCOME TO CYBER BREAKFAST\n";
   cout<<"\n1: Nasi Lemak with Kopi O";
   cout<<"\n2: Roti Canai with Teh Tarik";
   cout<<"\n3: Scramble Edd with Iced Milo";
   cout<<"\n4: Mixed Cereals with Strawberry Shake";
   cout<<"\n5: No thank you, I'm overweight..!\n\n";

   while (choice < 1 && choice > 5)
   {
        if ( !cin ) // input failed
        {
            cin.clear()  //reset input stream state
            
            // discard the stuff that put the input stream
            // into a bad state
            cin.ignore(256, '\n') ; 

             if ( !cin ) // utoh.  Still in a bad state.
                  break ;  // let's get out of here.
             
        }

        cout<<"Enter your choice: ";
        cin>>choice;
   }

  //  ...
}

Thanks cire, for ur code, I cant enter any input, is there any mistake on the code below?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <conio>
int main ()
{
   int choice=0;
   cout<<"\nWELCOME TO CYBER BREAKFAST\n";
   cout<<"\n1: Nasi Lemak with Kopi O";
   cout<<"\n2: Roti Canai with Teh Tarik";
   cout<<"\n3: Scramble Edd with Iced Milo";
   cout<<"\n4: Mixed Cereals with Strawberry Shake";
   cout<<"\n5: No thank you, I'm overweight..!\n\n";

   while (choice < 1 && choice > 5)
   {
        if ( !cin ) // input failed
        {
            cin.clear() ; //reset input stream state

            // discard the stuff that put the input stream
            // into a bad state
            cin.ignore(256, '\n') ;

             if ( !cin ) // utoh.  Still in a bad state.
                  break ;  // let's get out of here.

        }

        cout<<"Enter your choice: ";
        cin>>choice;
   }

  //  ...



getch();
   return 0;
}
Yes. Yes there is.

Line 13 should be:

while ( choice < 1 || choice > 5)

Obviously choice cannot be both less than 1 and greater than 5 at the same time, so the while loop was never entered.

Sorry for that.
As a side note, shouldn't you be either have using namespace std; or be using std::?

I didn't know any ANSI standard compilers could run such code.

Just a thought.

Thanks,
hnefatl
@cire

Thanks...now able to insert code but even I input 1-5, the output cannot be shown, may I know how to solve that? Coz actually I'm not quite understand about ur code...many code such as cin.clear() ; and cin.ignore(256, '\n') ;, I nvr used it b4...

@hnefatl, Borland C++ can run that without using namespace std;...

I only use using namespace std; when running my program using VSC++, which compiler r u using?
Code::Blocks; It was my first compiler, and I stuck with it :)
Topic archived. No new replies allowed.