Why does it do what it does

If you run this code and type in 1/5 upon the prompt
the program outputs the same values for the remaining trials instead of prompting for another value
Why does it do this?

using namespace std;

int main(){
double n, gal,liters;
cout<<"Enter amount of gallons: ";
liters = gal*3.785;
cout<<gal<<" gallons in liters is "<<liters<<"


return 0;}


  • if you're actually entering 1/5, cin >> gal will go into a fail state upon reading the / as gal is a double and / is not. you might try adding something like below to give it a quick check.

    [code]... bits of code
    cin >> gal;

    if(cin.fail()) {
    // take some action;

    ... bits of code[/code]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


In this Discussion