1

Below is my code:

char name;
bool isValid = true;
int mode;

cout << "Enter name:" << endl;
cin >> name;

do
{
cout << "Choose a mode" << endl;
cin >> mode;

switch (mode)
{
case 1:
    iniCharacter (name, 110, 100, 100);
    break;
case 2:
    iniCharacter (name, 100, 110, 100);
    break;
case 3:
    iniCharacter (name, 100, 100, 110);
    break;
default:
    isValid = false;
    cout << "Invalid mode, ";
    break;
}
}while (!isValid);

But when I run the above code, the following was the output:

[output]Please enter name:

[input] test

[output] Invalid mode

[output] Invalid mode

[output] Invalid mode

[output] Invalid mode

[output] Invalid mode


...

Why did the code result in a loop even when I didn't started to input value to mode?

Shouldn't the program wait for the user to input "mode"?

fmendez
  • 7,250
  • 5
  • 36
  • 35
User2012384
  • 4,769
  • 16
  • 70
  • 106
  • 1
    The name will be more than one character when the user types it, only the first character goes to your `name` variable (because you declared it a char) and the rest of the characters sit in a buffer and they aren't `int` type so it keeps complaining... see http://stackoverflow.com/questions/257091/how-do-i-flush-the-cin-buffer – amdn Mar 29 '13 at 14:16
  • I have changed the type to string, but the output is still the same – User2012384 Mar 29 '13 at 14:19
  • I've tested your program with name a string instead of char on GNU/Linux with gcc and it works, except if you enter a mode less than 1 or greater than 3 you get in an infinite loop, because it complains that `invalid mode`, set isValid to false, and never sets it to true again even if you enter valid input. – amdn Mar 29 '13 at 14:29

5 Answers5

1

You may need to set isValid to true at the top of the loop to account for when you enter valid input after a failed attempt:

isValid = true;
Mark Wilkins
  • 40,729
  • 5
  • 57
  • 110
1

Change type of 'name' to 'string'

fizzer
  • 13,551
  • 9
  • 39
  • 61
0

very simple

just change the name declaration to

char name[10]  // assuming your name would be 10 characters long.

and the whole program will run just fine.

Hope it helps.

GeekyCoder
  • 428
  • 3
  • 8
0

Its leaving \n in the buffer Try while(cin.get()!='\n'); before do loop

Suvarna Pattayil
  • 5,136
  • 5
  • 32
  • 59
0

Use this below code which will work-

char name[100] = {0};
bool isValid = true;
int mode;

cout << "Enter name:" << endl;
cin >> name;

do
{
cout << "Choose a mode" << endl;
cin >> mode;

switch (mode)
{
case 1:
    iniCharacter (name, 110, 100, 100);
    break;
case 2:
    iniCharacter (name, 100, 110, 100);
    break;
case 3:
    iniCharacter (name, 100, 100, 110);
    break;
default:
    isValid = false;
    cout << "Invalid mode, ";
    break;
}
}while (!isValid);

REASON:

  • Since you declared the name as single char and user enter more than single character i.e. "test", first char is assigned to name and rest are placed in the buffer as it is and because those are not integers cin doesn't care to ask you at console. Hence that int i.e. mode will have its initial garbage value only.

  • Also second output was however displayed i.e."Choose a mode" but your console buffer was flooded with that error and hence you were not able to see even that too.

Vishwanath Kamath
  • 340
  • 2
  • 3
  • 14