Endless loop for no good reason!!

Sharky Forums


Results 1 to 3 of 3

Thread: Endless loop for no good reason!!

  1. #1
    Tiger Shark Thraner's Avatar
    Join Date
    Sep 2001
    Location
    Parma, OH USA
    Posts
    809

    Angry Endless loop for no good reason!!

    Guys, I see no reason why this should go into an endless loop when the user enters a bad value.. Any ideas? I guess what I mean to say is, if it fails the IF statement, it just keeps repeating the error message, without asking the user for another input.



    #include <iostream>
    #include <string>
    using namespace std;


    void main()
    {
    //Title
    cout << "Currency Conversion\n";

    //Skip a line
    cout << "\n";

    /*Declare the variables, which later will contain the
    equivalents to a US Dollar */
    float auDollars = 0.0;
    float euEuros = 0.0;
    float gbPounds = 0.0;
    float caDollars = 0.0;
    float jaYen = 0.0;

    //Declare a variable for a user to enter an amount of US Dollars
    float userDollars = 0.0;
    bool calculationDone = false;

    //Declare a constant value for US Dollars
    const float USDOLLARS = 1.0;

    //Compute the equivalents to US Dollars
    auDollars = float(USDOLLARS * 1.78380);
    euEuros = float(USDOLLARS * 0.998301);
    gbPounds = float(USDOLLARS * 0.636951);
    caDollars = float(USDOLLARS * 1.55837);
    jaYen = float(USDOLLARS * 124.527);

    // Output the equivalents to a US Dollar
    cout << "$1 US = " << auDollars << " Australian Dollars" << endl;
    cout << "$1 US = " << euEuros << " European Euros" << endl;
    cout << "$1 US = " << gbPounds << " Great British Pounds" << endl;
    cout << "$1 US = " << caDollars << " Canadian Dollars" << endl;
    cout << "$1 US = " << jaYen << " Japanese Yen" << endl;


    //Error-check the amount of US Dollars the user has entered
    while (calculationDone == false)
    {
    cout << "Please enter an amount of US Dollars" << endl;
    userDollars = 0.0;
    cin >> userDollars;

    if (userDollars > 0)
    {
    //do and output calculations
    cout << userDollars << " US Dollars = " << (userDollars * auDollars) << " Australian Dollars.";
    calculationDone = true;
    }
    //end if
    }
    //end while

    }
    Thraner

  2. #2
    Reef Shark
    Join Date
    Dec 2001
    Location
    Henrietta, New York, USA
    Posts
    264
    I don't know all the in's and outs of iostream, using cin and cout, But what I'd reccomend is creating an if statement

    Psuedo code:
    string check;
    read line from common in and place it into check;
    if check is not a number set userDollars to 0;

  3. #3
    Registered User peterlak's Avatar
    Join Date
    Dec 2002
    Location
    Bolton, Ontario, canada
    Posts
    8

    Re: Endless loop for no good reason!!

    Try makeing the following modicication to your code.

    Originally posted by Thraner
    Guys, I see no reason why this should go into an endless loop when the user enters a bad value.. Any ideas? I guess what I mean to say is, if it fails the IF statement, it just keeps repeating the error message, without asking the user for another input.



    //Error-check the amount of US Dollars the user has entered
    while (calculationDone == false)
    {
    cout << "Please enter an amount of US Dollars" << endl;
    userDollars = 0.0;

    do {
    cin >> userDollars;
    } while(isNumeric(userDollars));
    /* your going to have to write the function isNumeric your self. */

    if (userDollars > 0)
    {
    //do and output calculations
    cout << userDollars << " US Dollars = " << (userDollars * auDollars) << " Australian Dollars.";
    calculationDone = true;
    }
    //end if
    }
    //end while

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •