0

I have put in all the bool operators - If the user inputs and invalid letter the while loop is supposed to ask the user to try again until a correct letter is entered. I have programmed using a while loop, switch statement and char at the same time. If it was int I would be Ok. Will it work if I put letter in a static_ cast. Can I use a statice_cast beween the while brackets. I tried making the function a bool statement completely but that only shut the program down. Any advise on what to try next would be appreciated.

char months (char, char, char, char);
 using namespace std;

 int main()
 {

 char  Season,time,time1,time2,time3;
        Season = months(time, time1, time2, time3);
        
        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 letter, char letter1, char letter2, char letter3)
   {
    
    char season;
    
     
    cout << endl;
    cout << " Enter  a letter " ;
    cin >> letter;
    cout << endl;
     
       while (letter<season)
         {cout << " Invalid month, enter another month ";
          cin >> letter;
          cout << endl;
         }
             
    switch (letter)  {

       case 'F':                                     
       case 'f':             
         cout<< " 02 " << endl <<endl;
         break;

       case 'S':
       case 's':           
         cout<< " 09 "  << endl;
         break;

       case 'O':
       case 'o':            
         cout << " 10 "  << endl;
         break;

       case 'N':
       case 'n':          
         cout << " 11 " << endl;
         break;

       case 'D':
       case 'd':             
         cout << " 12 "  << endl;
         break;
         
         season = letter;
         return  season;

       case 'A':
       case 'a':
         cout << " Enter a second letter: ";
         cin >> letter1;
         cout << endl;

          switch (letter1){
           case 'P':
           case 'p':
           cout << " 04 "  << endl;
           break;

           case 'U':
           case 'u':
           cout << " 08 "  <<endl;
           break;

           default:
           cout << " Unknown month" << endl;
          }
           break;           //Break switch for April and August
           season = letter1;
          return  season;

      case 'J':  // January, June, or July
      case 'j':
        cout << " Enter a second letter: ";
        cin >> letter1;
        cout << endl;

      switch (letter1)   {  //Second switch for January, June and July
       case 'A':
       case 'a':
       cout << " 01 "  << endl;
       break;

       case 'U':
       case 'u':
       cout << " Enter a third letter: ";
       cin >> letter2;
       cout << endl;

        switch (letter2) {   //switch inside switch for June and July
         case 'N':
         case 'n':
          cout << " 06 "  << endl;
          break;

         case 'L':
         case 'l':
          cout << " 07 ";
          break;

          default:
          cout << " Unknown month";
         }
          break;    //break from inside switch

          default:
                cout << " Unknown month";
         }
          break;     //break from inner switch
         season = letter2;
         return  season;
  
   
        case 'M':
        case 'm':
         cout<< " Enter second and third letters ";
         cin>> letter2 >> letter3;
        switch (letter2) {
         case 'A':
         case 'a':
        switch (letter3) {
         case 'R':
         case 'r':
          cout << " 03 " << endl;
          break;

        case 'Y':
        case 'y':
         cout << " 05 " << endl;
         break;
          season = letter3;
         return  season;

        default:
        cout<< " Unknown Month";
       }
        break;

        default:
         cout << "Unknown Month";
       }
        break;

        default:
         cout<< " Unknown Month";
        break;
  
   }    
}
2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by VernonDozier
0

Any casting you do to get this to work will just be a band-aid to cover up a problem. This program doesn't need any casts.

Lines 63 and 64 appear to me to be impossible to reach.

Line 22 - season is uninitialized, but you use it in a comparison in line 30. You shouldn't do that. The result of that comparison will be completely random and I assume you don't want that. You could end up with an infinite loop in lines 30 to 34.

What exactly is this function supposed to return anyway?

Edited by VernonDozier: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.