4 Replies - 603 Views - Last Post: 06 May 2011 - 09:27 AM Rate Topic: -----

#1 Grillakis  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 04-May 11

Issue with If statement in my program

Posted 05 May 2011 - 04:13 PM

I am having problem with my if statement. I tried this with the while loop, without the while loop and without the else but still having same issue. I am converting weight (kg) to pounds (lbs). The problem is when I run my program and I put in a negative number (just testing here) three straight times -3, -2, -1 and the 4th time I press 0, it just goes to a blank new line and it will not allow me to enter anything else. Here's my code:

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    double weight;
    double pounds;

    cout << "Enter a person's weight(kg) " << endl;
    cin >> weight;
    while(1)
    {
        if (weight < 0)
        {
            cout << "Please Try Again." << endl;
            cin >> weight;
        }
        else if (weight >= 0)
        {
            pounds = weight * 2.2;
        }
    }

   // pounds = weight * 2.2;

    cout << "\tWeight (kg)\t" << "Weight (lbs)" << endl;
    cout << "\t-----------     ------------" << endl;
    cout << setw(14) << fixed << setprecision(2) << weight << " kg"
         << setw(6) << "= " << setw(7) << fixed << setprecision(2)
         << pounds << " lbs" << endl;




    return 0;
}
 


Is This A Good Question/Topic? 0
  • +

Replies To: Issue with If statement in my program

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Issue with If statement in my program

Posted 05 May 2011 - 04:55 PM

Take the calculation out of the loop, and make the loop just
while( weight < 0 ) {
    cout << "Please Try Again." << endl;
    cin >> weight;
}

Then do the calculation.
Was This Post Helpful? 0
  • +
  • -

#3 Grillakis  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 04-May 11

Re: Issue with If statement in my program

Posted 05 May 2011 - 05:16 PM

View Postr.stiltskin, on 05 May 2011 - 04:55 PM, said:

Take the calculation out of the loop, and make the loop just
while( weight < 0 ) {
    cout << "Please Try Again." << endl;
    cin >> weight;
}

Then do the calculation.


Thank you very much for helping me. But why wasn't it working the way I had it? Isn't it the same or similar to the way you had me change it?
Was This Post Helpful? 0
  • +
  • -

#4 sungchoiok  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 44
  • View blog
  • Posts: 140
  • Joined: 18-July 10

Re: Issue with If statement in my program

Posted 05 May 2011 - 06:21 PM

Your code was not working because when weight was greater than 0, it went into an infinite loop. If you had put a break, like this,

    while(1)
    {
        if (weight < 0)
        {
            cout << "Please Try Again." << endl;
            cin >> weight;
        }
        else if (weight >= 0)
        {
            pounds = weight * 2.2;
            break;
        }
    }



then, it would have had the same effect.

But as you can see, r.stiltskin's is much shorter. It just checks if the input is valid, then if it is not, makes it loop again. If it is valid, the while statement is broken, since weight is not less than 0.

This post has been edited by sungchoiok: 05 May 2011 - 06:22 PM

Was This Post Helpful? 0
  • +
  • -

#5 Grillakis  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 04-May 11

Re: Issue with If statement in my program

Posted 06 May 2011 - 09:27 AM

View Postsungchoiok, on 05 May 2011 - 06:21 PM, said:

Your code was not working because when weight was greater than 0, it went into an infinite loop. If you had put a break, like this,

    while(1)
    {
        if (weight < 0)
        {
            cout << "Please Try Again." << endl;
            cin >> weight;
        }
        else if (weight >= 0)
        {
            pounds = weight * 2.2;
            break;
        }
    }



then, it would have had the same effect.

But as you can see, r.stiltskin's is much shorter. It just checks if the input is valid, then if it is not, makes it loop again. If it is valid, the while statement is broken, since weight is not less than 0.


I understand now. Thank you for that clarification sungchoiok. I really appreciate it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1