while loop not working - on invalid letter is entered. thanks

#include<iostream>

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;

}
}
while(letter < season)

Both letter and season are of type char, and only letter is given a value before the condition is evaluated. Ad you are not manually assigning season a value it might always be "less than" letter. However, you shouldn't really do conditions like that anyway.

To show this further if you assign season the value of 'b' and then input the letter 'a' when prompted, as the ascii value of a is smaller than that of the vlaue of 'b', you will enter your while loop.

Also, the variables you created in main are pointless and you should change your function to not take any arguements, they are not needed.
I rewote the code and it is not shutting down, but it is still an indifinite loop. I thinl my test conditions are incorrect. Not sure where ot go 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're doing it again. You create a variable withouth initializing it and then pass it as a n argument to the function, that doesn't even use that variable. I think you need to ree-read the tutorial on functions.

Also, never do:

1
2
char Q;
char season= Q;


'Q' has no value so you're assigning to 'season' a garbage value. Also, if you then give 'Q' a value, it will not be given to 'season'.

Sorry but theres nothing more that I can say without wasting both our times, go to here: http://www.cplusplus.com/doc/tutorial/functions/ ... and read this tutorial then come back to your code (although its probably better to start from scratch). i'm sure you will be able to figure it out. Feel free to ask anything once you've done that.
Topic archived. No new replies allowed.