Howdy, Stranger!

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

Categories

Why does it do what it does

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

[code]
#include
#include
using namespace std;

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

";}

return 0;}
[/code]

Comments

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