1

I've tried re-defining these variables in every imaginable way possible to try and get this line to work. I'm just going to give one example here to represent what's troubling me.

double const FRAME_COST = 45.00;
string input;
char yes,
     no;
int frames;


cout << "Do you want additional frames? Type yes/no:  ";
cin  >> input;

    if (input == yes){
       cout << "How many?"
       cin >> frames;
       frames = frames * FRAME_COST;
       }

// The problem is in **the if statement**
// I need to use a string not a bool (according to my professor)
// I can't get the compiler to recognize the **if statement**
// I realize this isn't practical, but he always throws curve balls.
manuell
  • 7,528
  • 5
  • 31
  • 58
judonomi
  • 75
  • 1
  • 1
  • 8

8 Answers8

13

Your current program has undefined behavior, because yes and no are character variables that have not been initialized, and you are using one of them in a comparison.

To fix, remove the declarations of yes and no (you do not need them), and use a string literal instead:

if (input == "yes") {
    ...
}

Note: your comparison may be too strict, because it is case-sensitive. It will take a yes, but it would not take a Yes or a YES as an answer. To address this you may want to convert the input string to lower case before the comparison.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
4
const string YES = "yes";
const string NO = "no";
const double FRAME_COST = 45.0;


int main()
{
    string input;
    double frames;
    cout << "Hello World" << endl; 
    cin >> input;

    if(input == YES)
    {
        cout << "How many?" << endl;
        cin >> frames;
        frames = frames * FRAME_COST;
        cout << frames << endl;
   }

   return 0;
}
IdahoSixString
  • 643
  • 10
  • 18
3

Just having a char named "yes" and another char name "no" is not sufficient, especially as you never actually gave them any values. I think you meant to write:

if (input == "yes") {
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
3

input == yes needs to be input == "yes" the quotes let the compiler know it's a string and not an identifier. I also think this might be helpful.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Ryan T. Grimm
  • 1,307
  • 1
  • 9
  • 17
2

You need to do the comparison with a string or character array.

if (input == yes)

This line does nothing as yes is a character pointer that is never initialized. It should be

if (input == "yes")

And you do not need the yes variable (alternatively, you can declare a constant string with the values to check: e.g. const std::string YES("yes");)

Note, you should probably also account for case-sensitivity.

Additionally, you are multiplying an integer frames by a double FRAME_COST (presumably to get the total cost?). This will result in a truncated integer value since you are storing it in an int. If you want the cost to be in dollars and cents, you should store it in a double or float:

double cost = frames * FRAME_COST;
Zac Howland
  • 15,777
  • 1
  • 26
  • 42
1

yes and no should be string constants (if you want to make them perfectly match with the input), either const std::string or const char* (or auto) but you have to assigh a value.

double const** FRAME_COST = 45.00;
string input;
const char* yes = "yes"
const char* no = "no";
int frames;


cout << "Do you want additional frames? Type yes/no:  ";
cin  >> input;

    if (input == yes){ // exact comparison (maybe add a space trim of input?)
       cout << "How many?"
       cin >> frames;
       frames = frames * FRAME_COST;
       }
Klaim
  • 67,274
  • 36
  • 133
  • 188
0

Instead of creating an if statement for only one input, is there a way of doing it with multiple inputs for one if statement without having to create several if statements?

for example...

string input;
cout << "Are you Bob?";
if (input == "yes", "no", "maybe"){

cout << "Sure...";

}else {
cout << "CANNOT COMPUTE";
}

Every time I try this, the input can be anything, and it will act as if I said "yes", "no", or "maybe".

Bob
  • 1
0
#include <iostream>
#include<cmath>;

using namespace std;
int main() 
{
double const FRAME_COST = 45.00;
string input;
int frames;


cout << "Do you want additional frames? Type yes/no:  ";
cin  >> input;

    if (input == "yes"){
       cout << "How many?";
       cin >> frames;
       frames = frames * FRAME_COST;
       cout<<frames<<endl;}
    else 
       {cout<<"please enter yes for additional frames";
       }
   
//ALL ERRORS ARE SOLVED;
//ENJOY
//FOR MORE HELP YOU CAN CONTACT ME WHATSAPP +923034073767.
// The problem is in **the if statement**
// I need to use a string not a bool (according to my professor)
// I can't get the compiler to recognize the **if statement**
// I realize this isn't practical, but he always throws curve balls.
return 0;
}
  • Though we thank you for your answer, it would be better if it provided additional value on top of the other answers. In this case, your answer does not provide additional value, since another user already posted that solution. If a previous answer was helpful to you, you should vote it up instead of repeating the same information. – Toby Speight Dec 12 '22 at 15:17