Been all through books and tutorials trying to learn how to code the while loop, char, switch and bool. in one program Loop is stuck. If wrong letter is entered, should ask user to reprompt, otherwise the function returns the number, but will not leave the loop. The problem may be the test condition- not sure how to correct or what to do next

#include<iostream>

char months (char);
 using namespace std;

 int main()
 {

 char  Season,time;
        Season = months(time);
        
        cout << endl;
        cout <<Season;
  
  cout<<endl<<endl;
  cout << " Press [enter] to exit" <<endl;
  cin.ignore(); //needed because of keyboard input
  cin.get ();
  return 0;
}
  char months (char monthNumber)
   {
    char Q;
    char season= Q;
    bool valid =false;
     
    do{
        cout << endl;
    
       cout << " Enter  a letter ";
        cin >>  monthNumber;
        cout << endl;
         
         while( monthNumber=='Q')
           {cout << " Invalid month, enter another month ";
          cin >>  monthNumber;
          cout << endl;}
     
     } while ('Q' != valid);
         
          
    switch ( monthNumber)  {

       case 'F': case 'f': 
       monthNumber = 2;  valid = true;           
         cout << endl <<endl;
         break;

 

       case 'S':case 's':           
       monthNumber = 9;  valid = true; 
        cout   << endl; 
         break;

       case 'O': case 'o':
        monthNumber = 10;  valid = true;                 
         cout   << endl;
         break;

       case 'N': case 'n':          
         monthNumber = 10;valid = true;
         cout   << endl;
         break;

       case 'D':case 'd':             
          monthNumber = 12;  valid = true; 
         cout << endl;
         break;
       return  monthNumber; 
         
       
   }    
       

 cout<<endl<<endl;
  cout << "Press [enter] to exit" <<endl;
  cin.ignore(); //needed because of keyboard input
  cin.get();
  return 0;
}

You pretty much have it as while 'Q' is false.
'Q' is 'Q' not true or false.
You want it as while valid is false and valid becomes true when a valid season is picked.

You pretty much have it as while 'Q' is false.
'Q' is 'Q' not true or false.
You want it as while valid is false and valid becomes true when a valid season is picked.

I rewrote the code I belive this was what you were talking about. Not sure whether to use valid, monthNumber or (Season)which is assigned whatever is in monthNumber and returned to main. Just really confused about the test condition.

#include<iostream>

using namespace std;

int main()
{
  bool valid =false;
  char monthNumber; 
   do{
        cout << endl;
    
       cout << " Enter  a letter ";
        cin >>  monthNumber;
        cout << endl;
         
         while( valid )
           {cout << " Invalid month, enter another month ";
          cin >>  monthNumber;
          cout << endl;
          cout << monthNumber;}    
     } while (! valid);
         cout << monthNumber;
          
    switch ( monthNumber)  {

       case 'F': case 'f': 
       monthNumber = 2;  valid = true;           
         cout << endl <<endl;
         break;
         
    }

All you do is set valid to false at the beginning, never change it anywhere else in the code.

Think again -- what is the exit condition you need for the while and do-while loops.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.