Why does it do what it does - Programmers Heaven

Howdy, Stranger!

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


Why does it do what it does

pylerpyler Posts: 1Member
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;}


  • pseudocoderpseudocoder Posts: 693Member ✭✭✭
    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.