Why is this not working

Hi all, could you tell me why this simple statement it not working, I am checking for char Y or y
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>
using namespace std;
int main()

{
	char ans;
	cout<<"test";
	cin>>ans;
	bool charvalid=false;
	do
	{
		if(ans!='Y'|| ans!='y')
		{
			cout<<"This is not a valid answer! \n";
			cout<<"Please try again using (Y/N) Only: ";
			cin>>ans;
		}
		else;
		{
			charvalid=true;
		}
	}while(charvalid);
	return 0;
}
Switch the || in the if statement to &&.

You're checking to see if either statement is true, and a char can't be equal to to values.
I don't understand what your goal is ? I compiled it. What is the goal what is it not doing?
Maybe your else ? else; Loose the ;
Thank you, so much. Would you be happy to test some code out for my from my program as i am having problems.

thanks
Michael
Another good idea is to convert the char to upper case using std::toupper (need to #include locale to use it) - then you won't have to test it twice.

I personally hate construct like:

if(ans!='Y' && ans!='y')

especially if someone extends them to cope with other things like operators +-*/. This is best handled with a switch.
my goal, was to check if the user enter Y or y if they didnt then they need to go back, and redo it to proceed further in the program
Did it work when you fixed the else?
no when i add it to my entire program, it wont work. I will get a messgae saying that when i press Y its invalid and then after i use y it will proceed.

But if i restart the program and choose y i still get an error message
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include<iostream>
using namespace std;
int main()

{
	char ans;
	cout<<"test";
	cin>>ans;
	bool charvalid=false;
	do
	{
		// if(ans!='Y'|| ans!='y') // Incorrect
		if(ans!='Y'&& ans!='y')
		{
			cout<<"This is not a valid answer! \n";
			cout<<"Please try again using (Y/N) Only: ";
			cin>>ans;
		}
		// else; // Incorrect
		else
		{
			charvalid=true;
		}
	}while(charvalid);
	return 0;
}


Myself and jlillie89 have already shown you how to fix it. If you're still having problems, you'll need to repost your code.
I have added this code, to my larger program. here is the link too my code, i have put it on another post

http://www.cplusplus.com/forum/beginner/87480/

When i select option 2, and press Y i get the error message, if then press y, it will proceed to next line of code, however if i restart the program and go to option 2 and press y i get another error message

???? This work?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include<iostream>
using namespace std;



int main()
{
	char ans;
	 
	
	cout << "test ";
	cin >> ans;
	ans = toupper(ans);

	while( ans != 'Y')
	{
		cout << "This is not a valid answer! \n";
		cout << "Please try again using (Y/N) Only: ";
		cin >> ans;
		ans = toupper(ans);
	}
	system("pause");
	
	return 0;
}
on its own yes, as soon as i put it in my program then the console screen froze
Well, as in it is waiting for something? Like _ ? Let's see it dude maybe I can help no promises I will look though if you post soon. The program I mean like where your putting it.
Last edited on
Thanks guys for your help i really appreciate it:

http://www.cplusplus.com/forum/beginner/87480/

Thats the link to my full code, now the problem i have is placed into a function called validate() which is at the bottom. Now i have read that global variables are evil, which at the moment i am using loads. Could this be the problem?
Also with the code, i am trying to use if for a menu....so

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
int main()
{
	int choice;
	char ans;
	bool charvalid=false;
	//======================Menu===================================
	do
		{
			
		
		banner();
		//Prints out menu option
			cout<<"Welcome to the conference booking system:\n\n";
			cout<<"Option 1: Book A Seat\n";
			cout<<"Option 2: View Seating Plan\n";
			cout<<"Option 3: Exit\n\n";
			cout<<"Please choose an option: "; 			
			cin>>choice;
			switch(choice)
				{//Switch Open
				
					case 1://book seat
						{
							system ("cls");
							seatingPlan();
							bookSeat();
							break;
						}
					case 2://View seat plan
						{
							system ("cls");
							seatingPlan();
							seatPlan[row-1][convert-65];
							cout<<"Would you like to view the delegates, that have booked? (Y/N): ";
							cin>>ans;
							if(ans=='Y' && ans=='y')
							{
							delegates();
							}
							break;
						}
					case 3: //exit
						{
							cout<<"";
							return 0;
							break;
						}
					default: cout<<"Thank you for using This Booking System\n";
					break;

				}//switch closed
					cout<<"Do you want to return to main menu (Y/N):  ";
					cin>>ans;
					system ("cls");
	}while (ans='Y'|| ans=='y');// Loops back to the main menu 


how could i get this code to work with the menu

Thanks
I did something like similar to this with a program I had to write. Maybe this will help you.

First is a function to get the input from the user.
I used something like this. Let me say there are easier way's to do this I assume.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//Function to display Menu and get selection
char displayMenu() 
{	
	//Variables
	char choice = ' ';

	//User menu
	cout << "Phone Directory Program " << endl;
	cout << endl;
	cout << "Options" << endl;
	cout << "[A]dd and entry to the phone directory. " << endl;
	cout << "[D]elete an entry from the phone directory. " << endl;
	cout << "[U]pdate an entry from the phone directory. " << endl;
	cout << "[L]ist the entire phone directory. " << endl;
	cout << "[E]xit the menu" << endl;
	//Get choice
	cout << "Please enter option selection. " << endl;
	cin >> choice;
	
	//Convert to uppercase
	choice = toupper(choice);
	
	//Control structure to return char to main.
	
	switch(choice)
	{
		case 'A': return 'A';
		break;
		case 'D': return 'D';
		break;
		case 'U': return 'U';
		break;
		case 'L': return 'L';
		break;
		case 'E': return 'E';
		default:  return 'F';
	}
}


Then in main I just switched on what came back from the function above.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
int main()
{	
	//Variables
	ifstream fin("input.txt");
	int newSize = 100;
    infoType tele[NSIZE];
	char selection = ' ';
	
    //Read from file
    readData(fin, tele, newSize );	
	
  //Main Algorithm
    do
	{
		system("cls");
		selection = displayMenu();
	
		//Function selection
	switch(selection)
	    {
		case 'A': update =  optionA(tele,newSize );
		break;
		case 'D': update = optionD(tele,newSize);
		break;
		case 'U': update =  optionU(tele,newSize );
		break; 	
		case 'L': optionL(tele,newSize );
		break;
		case 'E': update = EXIT;   
			break;	
		case 'F': cout << "Invaid choice " << endl;
		  Sleep(2000);  break;
		
	}
	
  }    while(update!= EXIT );
 
	system("pause");
 
 return 0;

}


Maybe this can help to give you an idea.
Topic archived. No new replies allowed.