Keep running program and 'int' problem

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 02-07-2008
willrs2
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.

Will
• 02-07-2008
willrs2
Never mind about the second problem. I fixed it by declaring it a 'long double'.
• 02-07-2008
NeonBlack
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.
• 02-07-2008
swgh
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
• 02-07-2008
NeonBlack
swgh I'm pretty sure this is his homework.
• 02-07-2008
swgh
Oh sorry I did not complete the program for him I just mentioned a way to use a user controlled loop thats all.
• 02-07-2008
NeonBlack
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.
• 02-07-2008
carlorfeo
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; }```
• 02-07-2008
willrs2
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?
• 02-07-2008
carlorfeo
Code:

`while (yn = 1)`
this is an assignment, not a comparation

comparation is done using ==
Code:

`while (yn == 1)`
• 02-07-2008
carlorfeo
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.
• 02-07-2008
willrs2
It runs just fine the way it is in my previous post, but it won't exit.

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.
• 02-07-2008
willrs2
I gotta go for now, maybe the day. Thanks for the help so far.
• 02-07-2008
cyberfish
Quote:

note the separated blocks of lines and where variable declarations should be put
IMHO that is more of a personal preference. It is required in C but not C++. Some people recommend declaring variables just before they are used in C++.
• 02-07-2008
carlorfeo
you are right cyberfish, I often declare variables not at beginning too.

By the way, in that case these declarations stay well out of the loop ;)
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last