Newbie to c++

Pages: 12
Can anyone see why the while loop isnt going back to the start of my program? When I click 'Y' it continue my program where else it should loop back to the start.

Last edited on
Also when 'N' is inputted, the 'Y' code is printed out
cout << "Please add S, M or L onto you're Order!" << endl;

where else it should be

cout << "Thanks! Goodbye!\n";
Remove the semicolon that's on the same line as the if statements - that's ending the if right there. Also - the ! means that you're checking if the opposite of that statement is true. So if again is not 'Y', you're asking them to add on to their order. And if again is not 'N', you're giving the goodbye message.


1
2
3
4
5
		if (!(again == 'Y'));
		{
			cout << "Please add S, M or L onto your Order!" << endl;
			cin >> size;
		}


1
2
3
4
		if (!(again == 'N'));
		{
			cout << "Thanks! Goodbye!\n";
		}


@wildblue, I cant see where to remove the semicolon as you mentioned.

I fixed it, I had a missing colon in my if statement which caused my program to go to the y/n statement.
Last edited on
When I come to this section of my code, it only accept my 2nd input it to move onto the next statement, why is this?

1
2
3
cout << "Would you like to add onto your order (Y/N)?\n";
			cin.ignore();
			cin >> again;


e.g. "Would you like to add onto your order"
type 'Y'
type 'Y' again, moves on - I want it to accepted on the first input and not the 2nd.

Last edited on
If you are using the stream extraction operators (i.e. '>>'), you don't need to ignore beforehand. What is probably happening is that you last used getline, so there is nothing to ignore, and instead is ignoring your input. Either remove the cin.ignore() line or replace it with cin.sync() (which doesn't have that problem).
Ive somehow fixed it and it was nothing to do with that part of the code. Thanks for the advice though :)
Ive finally finished this Task, time to move onto the other task in this assignment :(
@ NT3, ive found the same problem again but at a different spot and ive tired your idea but I cant get it to work, requires me to type twice for an input.

this part, full code below this code - starting at line 106 to 119

1
2
3
4
5
6
7
8
9
10
11
12
13
cout << "Would you like to add onto your order (Y/N)?\n";
			cin >> again;
			if (again != 'Y' || again != 'N') {

			}
			else if (again == 'Y')
			{
				cout << "Please Enter the following information \n";
				cout << "Please add S, M or L onto your Order! \n";
				//cin.clear();
				//cin.ignore();
				cin.sync();
				cin >> size;




Last edited on
closed account (2UD8vCM9)
Change line 108 to
 
if (again != 'Y' && again != 'N') {


Problem with the or operator is you are Checking if it is Not Y or Not Y, it is always going to be either Not Y or Not Y so that condition will always return true. If you put the && it makes sure it's not either Y or N.
Last edited on
Nope, still wants me to type in SS, MM or LL twice before it loops back to the start.

heres a pic -

http://s27.postimg.org/4afzuapnn/error.jpg
Last edited on
closed account (2UD8vCM9)
Sorry I misunderstood the problem. I believe removing line 118 will fix it.
Line to remove:
 
cin >> size;
Sorry I misunderstood the problem. I believe removing line 118 will fix it.
Line to remove:

cin >> size;


You sir have fixed my problem, grats :)
Topic archived. No new replies allowed.
Pages: 12