This code shouldn't even run is this really what you used to get the output above?
there shouldn't be a # before using namespace std;
vastaus is not declared, replace vastaus with answer.
There's no " " around Invalid answer/input, start again!
Put return 0 before the last }.
But also, assuming those are actually fixed (which, except for the return 0 part, must have been to get the code to run)
You need to replace the = in the if (answer = 'n') part with ==, same with the 'y' part. = is assignment, == checks for equality.
Replace or with && (and)
or ( || in syntax ) will execute if one of the options is true. In this case, you had that the answer was 'n'. The way the if statement will read this is...
1. check if answer is not equal to 'y' -> answer entered was 'n', which is not equal to 'y', hence do cout << Invalued answer...
There is no check that answer != 'n'.
Now if we used && ( and ) instead, it would go like.
1. check if answer is not equal to 'y' -> yup 'n' is not equal to y.
2. check if answer is not equal to 'n' -> answer was equal to 'n', hence do not run the cout << Invalid... statement.
An alternative to the 3 if statement is...
1 2 3 4 5 6 7 8 9 10 11 12
|
if (answer == 'n')
{
return 0;
}
else if (answer == 'y')
{
// do something
}
else
{
cout << "Invalid input..." << endl;
}
|