while

I started to learn C++ yesterday and I wanted to ask for help. This exercise is to make a program ask you to type in a letter until you type in the right one. I wanted it to be a letter "H" in this case. I know how to make it work using do while but I cannot figure out how to do it using 'while'. Please don't be too hard on me as I started yesterday. Thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

using namespace std;

int main()
{
  char letter;
  cout<<"Type in any letter: ";
  cin>> letter;

  while(letter)

  {
      cout<<"Type in any letter: ";
      cin>> letter;
      letter =! 'H';
  }

}
The loop condition should still be inside the parentheses after while.
like this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

using namespace std;

int main()
{
  char letter;
  cout<<"Type in any letter: ";
  cin>> letter;

  while(letter =! 'H')

  {
      cout<<"Type in any letter: ";
      cin>> letter;

  }

}
It's pretty much the same thing as a do while but rather the clause is on top and checked first before the loop begins.

Something worth mentioning is that on line 16 you are actually assigning letter to the flipped value of 'H' which in turns assigns letter to the value of 0. This is because anything that isn't 0 is true, thus the flipped value of any value that isn't 0/faslse will in turn be false. Which in turns ends your program because letter will now have a value of 0 i.e. false, thus causing your while loop to stop.

Also, I assume you meant to check if a value was not equal, if so change '=!' to '!='. Furthermore, use that in the while loop clause(the parentheses at the start that evaluate expressions/conditions) to check if letter is not equal to 'H' and you will solve your issue.
Last edited on
closed account (48T7M4Gy)
do ... while does everything in brackets before the loop condition is tested.
while ... do tests the condition before deciding to do everything in brackets.

You can put lots of "stuff" in the condition statement, like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

using namespace std;

int main()
{
  char letter = ' ';
  cout<<"Type in any letter: ";
   
   while(cin >>letter && letter != 'H')
   {
      cout<<"Type in any letter: ";
   }
   cout << "I'm finished now\n";

}

Type in any letter: a
Type in any letter: s
Type in any letter: H
I'm finished now
 
Exit code: 0 (normal program termination)
Last edited on
I removed letter =! 'H'; from line 16 and put letter != 'H' in inside the parentheses after while. and it works for me. Thanks for your help.
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;
int main()
{
  char letter;
  cout<<"Type in any letter: ";
  cin>> letter;
  while(letter != 'H')
  {
      cout<<"Type in any letter: ";
      cin>> letter;
  }
}
just for extra precaution add this

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;
int main()
{
  char letter='';//no garbage value now
  cout<<"Type in any letter: ";
  cin>> letter;
  while(letter != 'H')
  {
      cout<<"Type in any letter: ";
      cin>> letter;
  }
}


Last edited on
Topic archived. No new replies allowed.