else if, no/yes question with loop.

Hello. I'm really... really new to c++ and coding. I got the assigment to do one simple calculator, but first there is this one part.

The program should look like this:

Do you want to calculate again your course grade? y/n (User's answer:) N
Invalid answer/input, start again!

Do you want to calculate again your course grade? y/n (User's answer) r
Invalid answer/input, start again!

Do you want to calculate again your course grade? y/n (User's answer) Y
Invalid answer/input, start again!

Do you want to calculate again your course grade? y/n (User's answer) n
// Program ends without message

Do you want to calculate again your course grade? y/n (User's answer:) k
//Program continues here with more questions


The idea is that if the user answers anything else execpt y or n, you get "Invalid answer/input, start again!" and the program loops the question again until the user answer answers y or n. Program works well, but I Don't know how to make it loop and stop the loop when 'y' is answered. I need while, but I just get infinite loops. :(

Code:
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 answer; // User's answer.
cout << "Do you want to calculate again your course grade? y/n ";
cin  >> vastaus;

    if ( answer != 'y' or answer != 'n' ) {
       cout << Invalid answer/input, start again! << endl;
}

    if ( answer = 'n' ) {
       return 0;
}

    if ( vastaus = 'y' ) {
      //Program should continue asking new questions.
}

}

Last edited on
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;
}
Last edited on
Yeah sorry, there was some typos. Only real one was that return 0;

And thanks for tips, got this "loop" part working.

But now I got other problem.

When I input 'y' it works fine, it continues the code from there.
When I input 'n' it works fine, it quits the program

But for example if I input something like 'yes', it first does the code parts that follow if ( answer == 'y' ) but after that it does the else part two times as there is two letters (e, s) that aint 'y' or 'n'.

Other example. User's answer: 'dont'. After that it does two times the else part "Invalid input.." as there is two times something that ain't 'y' or 'n' and after that it quits the program as there is 'n'.

So is there some way to prevent this? Let's say if someone answers 'yes' it just looks it as invalid answer. So you must answer 'y' or 'n' to get program continue or quit. Every other option/word/sentence, goes to the else part.
char is a single char so if you're putting in multiple chars you'll get unexpected results. I'd advise using strings to prevent this, you couldn't also work in things like "yes", "no", "dunno", etc...

#include <string>

using std::string

getline(cin, answer);

are what you need.
Okey thank you Muckle ewe. That must mean that we're excepted to use just char, cause we have pretty strict limits with each work that you must use the specific commands, even they might not be the best ones.

Now my program seems to be running for most parts well, here is the example what it looks like when you use it:


./gradecalculator // Opens program

Do you want to calculate again your course grade? n

./gradecalculator

Do you want to calculate again your course grade? N
Invalid input, try again!

Do you want to calculate again your course grade? E
Invalid input, try again!

Do you want to calculate again your course grade? y
Give your points from the first (1.) assignment: 19
Give your points from the second (2.) assignment: 23
Give your points from the third (3.) assignment: 15
Give your points from the fourth (4.) assignment: 18
Give your bonus points: 9
Give your exam points: 67
Your total points are: 98
Do you want to calculate again your course grade? n




The bolded and cursive letters are user's answers. And it calculates the total points with not so complex formulas.

But when there is the part Your total points are: 98 , I have a problem.

If the total points are automatically between 0 and 100, the program should just display the result.

If the total points are over 100, the program should display number 100 to user.
If the total points are less than 0, the program should display number 0 to user.

Here is my thinking in code format. ( I have skipped all the other lines, cause the code works as supposed without this part.)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Earlier part of the code above here.

int final_points = 0; // The numbers shown to the user.
int total_points = ( assignment_points + bonus_points + exam_points ) 

// The points before looking if the points are above 100 or under 0.

if ( total_points < 0 )
{
final_points = 0;
}

else if ( total_points > 100 )
{
final_points = 100;

else 
{
total_points = final_points
}

cout << "Your total points are: "
        <<  final_points << endl;



Warnings I get: statement has no effect.
And no matter what numbers user inputs, it gives the answer as 0.

P.S. I'm sure that the total_points can be less than 0 and over 100.
P.S.2. I don't have any double or floating numbers, only int if that has effect.

Last edited on
Topic archived. No new replies allowed.