1

Expected behavior is to take the name as input and run the while loop again but instead it just goes to the next line and does nothing.

#include <iostream>
using namespace std;   
int main()
{
    int ch,mb;
    bool y=true;
    char name;
    cout<<"Enter 1,2 or 3 : ";
    do{
             cin>>ch;
             switch(ch)
             {
                 case 1:
                     cout<<" \n\t 1.ENTER CUSTOMER NAME :";    
                     cin>>name;     
                     continue;
                 case 2:
                     cout<<" \n\t 2.ENTER MOBILE NUMBER:";
                     cin>>mb;
                     continue;
                 case 3:
                     y=false;
            }
      }while(y!=false);
}
Mat
  • 202,337
  • 40
  • 393
  • 406
  • I think you need break and not continue there. – Delta_G Apr 30 '20 at 17:18
  • Move `cout<<"Enter 1,2 or 3 : ";` to inside the loop? – 001 Apr 30 '20 at 17:19
  • "*but instead it just goes to the next line and does nothing.*" what does this mean? What does going to the next line mean? What does doing nothing mean? What (exactly) do you expect to see, and what (exactly) are you actually seeing? – JohnFilleau Apr 30 '20 at 17:20
  • 1
    Also, `char name;` That should be a `char []` or even better, `std::string`. I am assuming `name` is more than one character. – 001 Apr 30 '20 at 17:22
  • @JohnnyMopp: In addition if OP try to type more characters, next character would go for `std::cin >> ch;` so infinite failed parsing... – Jarod42 Apr 30 '20 at 17:32
  • @Delta_G They would do the same thing. – Asteroids With Wings Apr 30 '20 at 17:33
  • @Jarod42 Misread, thank you. – François Andrieux Apr 30 '20 at 17:34
  • OK. I've never seen an example of continue used in a switch. I see now that it is continuing the do-while and not working on the switch at all. – Delta_G Apr 30 '20 at 17:39
  • @Jarod42 Yes, there are definitely issues with the input code. `name` could have spaces. Phone numbers often have dashes `-` (in the US anyway). OP should look into `std::getline()`. – 001 Apr 30 '20 at 17:42

2 Answers2

0

Use break; instead of continue; break will move execution to the end of the switch.

Edit: I think the issue is that you are just not seeing your prompt again. so move it inside your loop.

Lawrence Ward
  • 549
  • 1
  • 5
  • 17
  • 1
    In this case `continue` should work as well as a `break`. – 001 Apr 30 '20 at 17:21
  • `continue` should just check the loop conditional and re-execute it. In this case, while break is the correct, I don't think it's causing OP's error. – JohnFilleau Apr 30 '20 at 17:21
  • _"break will move execution to the end of the switch"_ which contains nothing, so the loop will begin anew.... just as if you'd written `continue`..... – Asteroids With Wings Apr 30 '20 at 17:35
0

It doesn't "do nothing"; it waits for the next input. Because that's what you told it to do!

If you want to get another prompt, move the cout<<"Enter 1,2 or 3 : "; line inside the loop.


By the way, a char is one byte so that's not really appropriate for reading a whole customer name; furthermore, you should avoid using ints for telephone numbers.

Asteroids With Wings
  • 17,071
  • 2
  • 21
  • 35