judonomi
judonomi

Reputation: 75

C++ string variables with if statements

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.

Upvotes: 1

Views: 129731

Answers (8)

Muhammad Afaq
Muhammad Afaq

Reputation: 1

#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;
}

Upvotes: 0

Bob
Bob

Reputation: 1

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".

Upvotes: 0

Ryan T. Grimm
Ryan T. Grimm

Reputation: 1325

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.

Upvotes: 3

IdahoSixString
IdahoSixString

Reputation: 643

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;
}

Upvotes: 4

Klaim
Klaim

Reputation: 69682

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;
       }

Upvotes: 1

Zac Howland
Zac Howland

Reputation: 15872

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;

Upvotes: 2

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726559

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.

Upvotes: 14

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385144

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") {

Upvotes: 3

Related Questions