Cin not working inside a function

Hi,

I have a little program I created just to practice but for some reason the compiler is skipping the cin "costPerGallon" inside "calculateCostPerMile" function.

Any idea why?

Does this has to do with the if statement I'm using?



#include <iostream>
#include <cmath>
#include <string>

using namespace std;

float gallons, miles, mpg, costPerGallon, costPerMile;
bool decision = "yes";
void calculateCostPerMile();

int main ()
{

cout << "Miles per Gallon Calculator" << endl;
cout << endl;
cout << "Please enter the number of gallons of gas your car can hold? " << endl;
cin >> gallons;
cout << endl;

cout << "Please enter the number of miles driven with a full tank? " << endl;
cin >> miles;
cout << endl;

mpg = miles / gallons;

cout << "You car will give you " << mpg << " miles per gallon " << endl;
cout << endl;

cout << "Would you like to calculate the cost for a trip? Please say 'Yes' or 'No' to continue..." << endl;

cin >> decision;

if (decision == true)
{
calculateCostPerMile();
}
else
{

cout << "Ok, thank you for using our calculator. Please hit enter to quit! " << endl;

}

return 0;
}

void calculateCostPerMile()
{
cout << "Please enter gas price ";
cin >> costPerGallon; // the compiler is skiping this cin, why?

cout << costPerGallon;
}


Thanks a lot!
closed account (zwA4jE8b)
bool only takes 2 values, "true" or "false" or '1' or '0'. Not yes and no.
however true is just an alias for 1 and false an alias for 0.


if you want to use other values
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
char ch_in;
bool decision;

.....

cout << "Would you like to calculate the cost for a trip? Please say 'Y' or 'N' to continue..." << endl;
cin >> ch_in;
if (ch_in == 'Y' or ch_in == 'y')
decision = true;
else
decision = false;

if (decision == true)
 calculateCostPerMile();


alternatively

1
2
3
4
5
6
7
8
9
char ch_in;

.....

cout << "Would you like to calculate the cost for a trip? Please say 'Y' or 'N' to continue..." << endl;
cin >> ch_in;
if (ch_in == 'Y' or ch_in == 'y')
 calculateCostPerMile();
Last edited on
Thanks a lot for the clarification about the bool. I apreciate that you actually showed me to samples.

Now, I went with the second example, but now the question is how can I use the word 'yes' or 'no' instead of just 'y' and 'n'?

I tried using an array but it didn't work, I get an error that says "Comparison between pointer and integer "

char decision [4];

cin >> decision;

if (decision == 'Yes' or decision == 'yes')
{
calculateCostPerMile();
}



Again Thanks a lot for your help!
Last edited on
Use this code:
1
2
3
4
5
6
7
8
string decision;

cin >> decision;

if (decision == "Yes" or decision == "yes")
{
calculateCostPerMile();
} 

Usualy when it comes to comparison, strings are better than char pointers.
Last edited on
First of all thanks a lot for your help.

Hmm, for some reason it didn't work.

this is the error I'm getting... Invalid operands to binary expression ('string'(aka 'basic_string <char>') and 'int')

Thanks
closed account (zwA4jE8b)
did you
1
2
3
4
#include <string>
...

std::string decision;
@fstigre_ that's odd! I tried your code with the decision a string, and it all worked!
here's my 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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <cmath>
#include <string>

using namespace std;

float gallons, miles, mpg, costPerGallon, costPerMile;
string decision;
void calculateCostPerMile();

int main ()
{

cout << "Miles per Gallon Calculator" << endl;
cout << endl;
cout << "Please enter the number of gallons of gas your car can hold? " << endl;
cin >> gallons;
cout << endl;

cout << "Please enter the number of miles driven with a full tank? " << endl;
cin >> miles;
cout << endl;

mpg = miles / gallons;

cout << "You car will give you " << mpg << " miles per gallon " << endl;
cout << endl;

cout << "Would you like to calculate the cost for a trip? Please say 'Yes' or 'No' to continue..." << endl;

cin >> decision;

if (decision == "Yes" or decision == "yes")
{
calculateCostPerMile();
}
else
{

cout << "Ok, thank you for using our calculator. Please hit enter to quit! " << endl;

}

return 0;
}

void calculateCostPerMile()
{
cout << "Please enter gas price ";
cin >> costPerGallon;

cout << costPerGallon;
}
Thank you all for your help.


@viliml
I found the problem, I was using single quotes instead of double quotes when evaluating the yes string.

if (decision == 'Yes' or decision == 'yes')

I changed it to
if (decision == "Yes" or decision == "yes")

and it did the trick!

You guys are awesome, Thanks a lot
Last edited on
Topic archived. No new replies allowed.