Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

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?

[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 Posts: 673Member ✭✭
    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.