Thread: Keep running program and 'int' problem

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    11

    Thumbs up Keep running program and 'int' problem

    I'm using this program in C++ to calculate wind chill:

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <math.h>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        cout << "Wind Chill Calculator...\n" ;
        cout << "By William Seed\n" ;
        int temp, vel;
        int ch;
        cout << "Enter the temperature:";
        cin >> temp;
        cout << "Enter the wind speed:";
        cin >> vel;
        ch = 35.75 + (.6215 * temp) - (35.75 * pow(vel,.16)) + (.4275 * temp * pow(vel,.16));
        cout << "The wind chill is: ";
        cout << ch;
        cout << "\n";
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    I have two problems:

    1. (most important) I want this program to keep running until I tell it to stop (using imput as yes or no.
    2. If I try to use a decimal for 'temp', then the program ends...

    I'm new, so no flaming, please.

    Thanks in advance,

    Will

  2. #2
    Registered User
    Join Date
    Feb 2008
    Posts
    11
    Never mind about the second problem. I fixed it by declaring it a 'long double'.

  3. #3
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    Your temperature, windspeed and chill variables should all be double or float.
    Since integers are whole numbers, using decimal points is inappropriate for the type int.

    To keep the program running, you need to use some kind of loop. When you finish everything, ask the user if she wants to run the program again, if she enters yes, go back to the top of the loop, if she says no, terminate the loop.

    It's up to you to figure out how to implement this.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  4. #4
    coder
    Join Date
    Feb 2008
    Posts
    127
    hi willrs2
    here is what you need for your loop implementation:
    http://www.cprogramming.com/tutorial/lesson3.html


    1st: I'd suggest you to divide your code in contextual blocks, in order to make it more readable, for us and for you too.

    2nd: if you need to print a line like
    Code:
    The wind chill is: <value>
    You may put all these data into a single row of code:
    Code:
    cout << "The wind chill is: " << ch << "\n";
    3rd: <iostream> provides a useful "endl" (end line) to let you avoid typing "\n" everytime:
    Code:
    cout << variable << endl;


    just an example:
    note the separated blocks of lines and where variable declarations should be put
    Code:
    int main(int argc, char *argv[])
    {
        int temp, vel;
        int ch;
    
        cout << "Wind Chill Calculator...\n" ;
        cout << "By William Seed\n" ;
    
        cout << "Enter the temperature:";
        cin >> temp;
    
        cout << "Enter the wind speed:";
        cin >> vel;
    
        ch = 35.75 + (.6215 * temp) - (35.75 * pow(vel,.16)) + (.4275 * temp * pow(vel,.16));
        cout << "The wind chill is: " cout << ch << endl;
    
        system("PAUSE");
        return EXIT_SUCCESS;
    }

  5. #5
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Or use a sentinal controlled loop:

    Code:
    std::cout << "Enter a number or enter -1 to end: ";
    std::cin >> number;
    
    while ( number != -1 )
    {
       std::cout << "\nSqaured: " << number * number << std::endl;
    
       std::cout << "Enter a number or -1 to end: ";
       std::cin >> number;
    }
    This snippet will continue to square the number untill the user enters -1 to exit the loop
    Double Helix STL

  6. #6
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    swgh I'm pretty sure this is his homework.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  7. #7
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Oh sorry I did not complete the program for him I just mentioned a way to use a user controlled loop thats all.
    Double Helix STL

  8. #8
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    Anyway, although the type of control swgh suggested can be useful, in my opinion there is a better loop for this case.
    Also, you should be careful when using != and == with floating point numbers.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  9. #9
    Registered User
    Join Date
    Feb 2008
    Posts
    11
    Thanks, all of you. I used a while loop. Thanks carlorfeo for the tips, and thanks swgh and NeonBlack for the loop idea... I completely forgot about it...

    Here is the code a the proggy working how I want it:

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <math.h>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        double temp, vel;
        double ch;
        char yn;
        while (yn = 1) 
        {
              cout << "Wind Chill Calculator...\n" ;
              cout << "By William Seed\n" ;
              cout << "Enter the temperature: ";
              cin >> temp;
              cout << "Enter the wind speed: ";
              cin >> vel;
              ch = 35.75 + (.6215 * temp) - (35.75 * pow(vel,.16)) + (.4275 * temp * pow(vel,.16));
              cout << "The wind chill is: ";
              cout << ch;
              cout << "\n";
              cout << "Enter 1 to rerun program: " ;
              cin >> yn ;
              cout << "\n\n";
        }
    
        system("PAUSE");
        return EXIT_SUCCESS;
    }

    I have one more question, though... It's not that important, but if I enter anything besides 1, it still reruns the program. How could I fix that?
    Last edited by willrs2; 02-07-2008 at 05:23 PM.

  10. #10
    coder
    Join Date
    Feb 2008
    Posts
    127
    Code:
    while (yn = 1)
    this is an assignment, not a comparation

    comparation is done using ==
    Code:
    while (yn == 1)

  11. #11
    coder
    Join Date
    Feb 2008
    Posts
    127
    anyway your loop won't work since you tell the program to loop while yn is 1.
    Is it correct?

    edit:
    sorry, I didn't read the code well enough
    the real matter is that the loop won't start because, when you run the program, yn is not 1: its value has not been declared.

    Using while (yn = 1) worked beacause the statement (yn = 1), which is an assignment, will always result true.
    Last edited by carlorfeo; 02-07-2008 at 05:35 PM.

  12. #12
    Registered User
    Join Date
    Feb 2008
    Posts
    11
    It runs just fine the way it is in my previous post, but it won't exit.

    I only made two changes:

    I added in a second = to make it a comparison.

    and

    I set yn = 1 just before the while statement and after the char yn; statement, and it doesn't allow me to rerun the program at all.
    Last edited by willrs2; 02-07-2008 at 05:42 PM.

  13. #13
    coder
    Join Date
    Feb 2008
    Posts
    127
    It runs just fine the way it is in my previous post, but it won't exit.
    Of course, as I told you: it runs fine because while (yn = 1) assigns 1 to yn every loop cycle.
    Running the program, even typying in any different number, yn will be changed to 1 again.

    I set yn = 1 just before the while statement and after the char yn; statement, and it doesn't allow me to rerun the program at all.
    You can't rerun the program because you compare (yn == 1) when yn is a char.
    A char is interpreted as a character, not a numeric value, so the comparing won't be true when you type in 1.

    Two solutions:
    1: Declare yn as an int or a bool.

    2: Compare yn with a char -> (yn == "r")
    and of course change the sentence to "Enter 'r' to rerun program: "

  14. #14
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    Quote Originally Posted by carlorfeo View Post

    2: Compare yn with a char -> (yn == "r")
    and of course change the sentence to "Enter 'r' to rerun program: "


    (yn=='r')
    actually. Single quotes for char and double quotes are for string.
    Last edited by abh!shek; 02-07-2008 at 09:13 PM.

  15. #15
    Registered User
    Join Date
    Feb 2008
    Posts
    11
    I gotta go for now, maybe the day. Thanks for the help so far.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debug Error Really Quick Question
    By GCNDoug in forum C Programming
    Replies: 1
    Last Post: 04-23-2007, 12:05 PM
  2. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  3. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  4. My Allegro Program will not run...
    By Vicious in forum Game Programming
    Replies: 17
    Last Post: 06-14-2002, 12:49 AM
  5. debug program
    By new_c in forum C Programming
    Replies: 3
    Last Post: 03-18-2002, 11:50 PM