-3

I'm just starting to learn C++ for school, and I'm having trouble completing a school project requiring the use of if/else statements. The project requires code that works as: example of working code My code looks like this:

#include <iostream>

using namespace std;

int ontime;
int zip;
int work;

int main()

{
cout << "Welcome to the DIG3873 System." << endl;
cout << "Did the student submit the exam on time (Y/N)? ";
cin >> ontime;

if (ontime == 'Y' || 'y') {
    cout << "Did the student zip the file (Y/N)? ";
    cin >> zip;

    if (zip == 'Y' || 'y') {
        cout << "Did the student's code work as requested (Y/N)? ";
        cin >> work;

        if (work == 'Y' || 'y') {
            cout << "Congratulations, YOU PASS. "<< endl;
        } else if (work == 'N' || 'n') {
            cout << "YOU FAIL " << endl;
        } else {
            cout << "Please enter a valid response. " << endl;
        }
    } else if (zip == 'N' || 'n') {
        cout << "YOU FAIL " << endl;
    } else {
        cout << "Please enter a valid response. " << endl;
    }
} else if (ontime == 'N' || 'n') {
    cout << "YOU FAIL " << endl;
} else {
    cout << "Please enter a valid response. " << endl;
}
}

Unfortunately, it's not working as I would have hoped. When it runs, it lets me answer the first statement, then just drops all the other cout statements and a bunch of "YOU FAIL"s and ends the program. We haven't learned anything beyond if/else statements, so I was pretty lost looking at similar coding issues where people recommended using loops instead. Sorry for such a beginner's issue with not understanding if/else statements, thanks!

Olivia
  • 3
  • 2
  • 1
    `if (ontime == 'Y' || 'y') {` must be changed to `if (ontime == 'Y' || ontime == 'y') {`, you can't do comparisons as you wrote, they don't work as you expected. – πάντα ῥεῖ Sep 13 '18 at 16:45
  • 2
    Obviously you want to learn from a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to learn the language basics. – πάντα ῥεῖ Sep 13 '18 at 16:47
  • 1
    I'm trying to find a duplicate, but in the meantime: `if (ontime == 'Y' || 'y')` means "if ontime is `Y` or if `y`"; that is, `'y'` is evaluated as a separate condition, which is always true. – melpomene Sep 13 '18 at 16:47
  • 1
    do yourself a favour and write code in small steps and do testing. You could have noticed that there is something wrong with only the very first `if` in your code. Its is much easier to test and debug little code than it is to test and debug lots of code – 463035818_is_not_an_ai Sep 13 '18 at 16:56
  • In general you shouldn't use `cin >>` for user input. If you have to use `>>`, always check whether it succeeded: `if (cin >> ontime) { /* ok */ } else { /* something went wrong, ontime is not set */ }` – melpomene Sep 13 '18 at 16:58
  • 1
    You should convert the character to upper case or lower case before comparing. See `tolower` and `toupper`. Something like `ontime = tolower(ontime); if (ontime == 'y')`. – Thomas Matthews Sep 13 '18 at 17:10

1 Answers1

1

The expression zip == 'Y' || 'y' will always be true.

That's because the only comparison that's made is zip == 'Y'. The expression is really (zip == 'Y') || ('y'). That is, you test if zip is equal to 'Y'; Or if 'y', just 'y', no comparison or anything. And since 'y' is non-zero it's true.

You need to explicitly compare zip with both values: zip == 'Y' || zip == 'y'.

The same is valid for your other conditions as well.


You also have another problem, and that is you not actually reading characters but integers. If you have int variables and are using the formatted input operator >>, the input will attempt to parse the input as an integer.

To read characters you need to use char.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Thanks - unfortunately, after fixing that issue throughout the code, now no matter what I type in for the first response the code spits back out "Please enter a valid response" – Olivia Sep 13 '18 at 16:52
  • @Olivia Think about the *types* you attempt to read. Updated my answer. – Some programmer dude Sep 13 '18 at 16:56