-3

(This is my first question on Stack Overflow, so please let me know if I can word it better.)

I have a program that lets you open either a calculator or a "number generator". You pick which one to open by entering C or N into the console. The code is pretty complex for me as I just started learning C++ and so I wasn't really able to scope out any issues with it. Also, I have defined all the variables shown and included .

However, no matter what I enter at the beginning (even if it's an invalid answer, like Q or 123), it opens the calculator.

void init()
{
    std::cout << "Open calculator or number generator? ( C or N ): ";
    std::cin >> firstpick;
}

int main()
{
    init();
    if (firstpick == 'c' || 'C')
        calculator();
    else if (firstpick == 'n' || 'N')
        numbergen();
    else if (firstpick != 'c' && 'n' && 'C' && 'N')
        std::cout << "ERROR: Invalid answer to init";

    return 0;
}

The expected result is to allow me to open either C or N and to have the program return an error when I enter an invalid answer, but it always opens C no matter what.

  • 1
    Your code simply will not compile, so its behaviour is moot. –  Dec 27 '18 at 21:52
  • 9
    `if (firstpick == 'c' || 'C')` is always true – UnholySheep Dec 27 '18 at 21:53
  • 3
    More relevent: `'C'` is always true. – Mooing Duck Dec 27 '18 at 21:53
  • 1
    Also, which C++ book are you learning from that suggests you write code that looks anything like this? –  Dec 27 '18 at 21:54
  • 3
    change that to `if (firstpick == 'c' || firstpick == 'C')` and also change the other if statements in the same way. you cannot chain || or && the way you are doing it. – Chris Rollins Dec 27 '18 at 21:54
  • 2
    Change `if (firstpick == 'c' || 'C')` to `if (firstpick == 'c' || firstpick == 'C')` and other conditions accordingly. – πάντα ῥεῖ Dec 27 '18 at 21:55
  • None of these suggestions work if `firstpick` is not defined. –  Dec 27 '18 at 21:55
  • @NeilButterworth good catch. if his code is compiling, then perhaps he has it declared as a global variable which is probably what he did since he's attempting to use it from two different functions. – Chris Rollins Dec 27 '18 at 21:57
  • @Chris Maybe. Maybe not. It's not a good idea to guess when you could get the OP to post the actual code. –  Dec 27 '18 at 21:58
  • I mean, I don't see `#include ` either which is another reason this code wont compile – Chris Rollins Dec 27 '18 at 21:59
  • @Chris The point of my original comment exactly. Who knows what he has actually written? –  Dec 27 '18 at 22:00
  • Sorry for the confusion; the entire script refused to fit in the code section for some reason. I did include and define all the variables. Babak Naffas solved my issue. – kineticcrusher Dec 27 '18 at 22:08

4 Answers4

1

firstpick == 'c' || 'C' will always return true.

firstpick == 'c' may evaluate to false but the 2nd half of the statement is just 'C' which, as a none-zero value, will evaluate to true.

Change your logic to if (firstpick == 'c' || firstpick == 'C') and follow the same pattern for the number generator logic.

Babak Naffas
  • 12,395
  • 3
  • 34
  • 49
0

Your code is

if (firstpick == 'c' || 'C' != 0)

to get your effect you need to do...

if (firstpick == 'c' || firstpick == 'C')
mksteve
  • 12,614
  • 3
  • 28
  • 50
0

You write your if functions wrongly.

|| logical OR separates two logical conditions of which at least one should be true to produce true value as result.

In your case conditions checked by the first "if" are:

  1. firstpick == 'c' and
  2. 'C'

So your if is going to produce true if 1. or 2. or both are true.

Of course as you can see now 2. is always true so the whole if always returns true.

Try change your ifs like this:

if (firstpick == 'c' || firstpick == 'C')

And it should work better.

Zegar
  • 1,005
  • 10
  • 18
0

You did not define what firstpick is so the code you presented cant compile. You also need to return a char from your initialization function. This code below should do what you need. Also you need to "or" individual comparisons in your if statements.

#include<iostream>
char init()
{
    std::cout << "Open calculator or number generator? ( 1 or 2 ): ";
    char option;
    std::cin >> option;
    return option;
}

int main()
{
    int firstpick=init();
    if (firstpick == 'c'|| firstpick == 'C')
        std::cout<<"calculator\n";
    else if (firstpick == 'n' || firstpick == 'N')
        std::cout<<"Num gtenerator\n";
    else
        std::cout << "ERROR: Invalid answer to init\n";

    return 0;
}
Nirvedh Meshram
  • 439
  • 6
  • 13