For Loops

I am a beginning C++ programmer trying to figure out how what to include in the test update section of a for loop that prompts the user to enter temperature in degrees Celsius, the number of conversions, and how much to increment.
I have been banging my head against wall on this and know the answer is probably simple.
I want the for loop section to run x number of times. Here is a sample of the for loop.
for (int tempCelsius = tempStart; tempCelsius <= tempEnd; tempCelsius+=advance){
cout << tempCelsius << "\t\t" <<((9.0/5.0) * tempCelsius + 32) << endl;

Any help would be appreciated. My problem is with the test update section of for loop.

Thanks in advance.
Last edited on
can you tell me what a "test update section" of a loop is please?
For sure my lingo is way off here.
for (initialization; test; update)
statement;

I am having problems programming the test portion to do what I want it to do.
The display should look like so. I would never ask for a hint if I hadn't spent an ungodly amount of time racking my brain.

//this is a sample run of what i want
Enter starting Celsius value: 0
Enter number of conversions: 4
Enter increment between values:5

Celsius Fahrenheit
---------------------------
0 32
5 41
10 50
15 59

I want it to run "x" amount of times but am stumped by something real easy I'm sure. The way I have the for loop setup now it will run from tempStart to tempEnd.
So your starting temp and end temp are consts? In that case, your current for loop will run all the way from 0 Celcius to whatever it is your end temp is right?


Then the "test" section of the for loop needs to include a condition for "number of conversions" (use a logic operator ie. &&). Throw in an update for your number of conversions in the "update" section of the for loop to end the loop separated with a comma operator.
Last edited on
No they are user inputs, but you are right whatever is entered in tempStart and tempEnd that is the range at present. I want the loop to run a specific amount of times not based on tempEnd. I didn't include that portion. I can run the program using everything as a constant like you said. In my program the user is prompted to enter temp, number of conversions, and increments per conversions.

cout << "Enter temperature in degrees Celsius:";
cin >> tempStart;
cout << "Enter the number of conversions:";
cin >> tempEnd;
cout << "Enter the increments between values:";
cin >> advance;

I will try your hint. I want to figure out myself cause I have learned quite a bit about for loops trying to figure this out but apparently not enough.



If the condition to break the for loop is not based on tempEnd (which means your current condition for "test" can just be removed), then it's based on "number of conversions". So just change that and you'll have what you want.

Have the user input tempStart, advance and "number of conversions". Your "update" section would still require updates for advance and "number of conversions", separated by a comma.

Consider this though that your user may attempt to break your program. What if I entered 999999999999999999 for "number of conversions"? How are you going to limit the user from doing that? Incidentally, for your other version of the program what if I entered 9999999999999999999999999 for tempEnd? Just something extra for you to add on if you want to.
Last edited on
Are you looking for something like this?

1
2
3
4
5
for (int i = 0; i < numIncrements; i++)
{
     int tempCelcius = tempStart + (i * advance);
     ...
}

Last edited on
@Olysold
I have tried different syntaxes for this and nothing works.
the for loop looks like this
for (tempCelsius = tempStart; tempCelsius <= tempEnd; tempCelsius +=advance)
cout << tempCelsius << "\t\t" << (( 9.0/5.0) * tempCelsius + 32);

Any number lower than tempCelsius won't allow the program to display anything.
Any number lower than tempCelsius won't allow the program to display anything.

What do you mean? Which variable are you talking about having a value "lower than tempCelsius"?

What are the values of tempStart, tempEnd and advance?

Edit: It would be so much easier to read your code if you would use code tags, and conventional indentation, like so:

1
2
3
4
for (tempCelsius = tempStart; tempCelsius <= tempEnd; tempCelsius +=advance)
{
  cout << tempCelsius << "\t\t" << (( 9.0/5.0) * tempCelsius + 32);
}
Last edited on
cout << "Enter temperature in degrees Celsius:";
cin >> tempStart;
cout << "Enter the number of conversions:";
cin >> tempEnd;
cout << "Enter the increments between values:";
cin >> advance;

//The user is prompted to enter in these numbers. Then based on these numbers I made the for loop.
for (tempCelsius = tempStart; tempCelsius <= tempEnd; tempCelsius +=advance)
cout << tempCelsius << "\t\t" << (( 9.0/5.0) * tempCelsius + 32);

//The display should looks as follows
Celsius Fahrenheit
--------------------------------------------
0 32
5 41
10 50
15 59
Um, surely "Enter the number of conversions:" is the wrong prompt for tempEnd. Shouldn't it it be something like "Enter the final temperature in degrees Celsius:"?

Because that's how you're using tempEnd - as the highest value in the range of temperatures. Not as the number of conversions.
@MikeyBoy
Right. Initially I didn't have it that way. The variable was tempConversions.
I changed it to tempEnd just so it would make sense to me because that is the only way I could get anything to display. I know why it is not working just can't come up with fix. Thank you for advice on displaying the code so it's user-friendly.
So now I'm confused. What happens if you enter the numbers 0, 15 and 5? Do you not get the output you expect?
@MikeyBoy
The numbers in the Celsius column increment from the starting point of the user input. Say tempStart is the user input. In the sample run 0 was the user input and the increment was 5 every cycle. The user entered 4 for number of times to run the loop. What I am trying to figure out is how to code the part that controls the number of times it is supposed to loop before breaking.
You will have to excuse me this is my first post on this forum. I appreciate all the help and apologize if I have wasted anyones time.
I think what I posted above is what you want. The number of times to run the loop is numIncrements.
@doug4
I haven't yet tried that. After I get out of my day job I am going to see if that works. Thanks doug4.
Thank you all for your help on this.
@doug4
I tried the above and it did finally work with some tweaking. I had my variables all messed up. When I used the generic variable i mixed in with my original user input variables it worked. I had to create a line within the loop to increment the tempCelsius also.
Show us what you ended up with. Line 3 in my example code should take care of incrementing tempCelcius.
@doug4
It did program works exactly how I wanted it to now. One more question how do I enclose my program in proper coding for future questions like what was done in above responses.





//This program demonstrates a simple for loop.
#include <iostream>
using namespace std;


int main()

{


//Initialize Variables
int tempStart = 0;
int tempEnd = 0;
int advance = 0;
int tempCelsius = tempEnd;
int numIncrements = 0;

double tempFahrenheit;





//user input to set all three starting points
cout << "Enter starting Celsius value: \n";
cin >> tempStart;
cout << "Enter the number of conversions: \n";
cin >> numIncrements;
cout << "Enter increment between values: \n";
cin >> advance;

//Display the table.
cout << "Celsius Fahrenheit\n"
<< "---------------------------\n";


//The for loop.
for (int i = 0; i < numIncrements; i++)
{
tempFahrenheit =( (9.0/5.0) * tempCelsius + 32);
cout << tempCelsius << "\t\t" << tempFahrenheit << endl;
tempCelsius += advance;
}



system ("pause");
return 0;

}
What you need is to enclose your code in code tags. Highlight your code and click the "<>" button on the Format menu.

First a bug. Your program worked fine because you ran it with a tempStart of 0. If you used a different tempStart, your program would still start at 0 because you never initialized tempCelcius to tempStart--it always starts at 0. You need to initialize tempCelcius = tempStart before you enter the loop.

I was confused when you said you needed to increment tempCelcius because my example code took care of that in line 3. In my code tempCelcius is a variable contained within the the scope of the body of the loop that gets calculated from tempStart, advance, and the loop counter each time through the loop. Since you did not pick up that line from my code, you needed to increment tempCelcius, a variable that is initialized outside the body of the loop. I had assumed you had picked up my line 3, so I didn't understand why you needed to modify tempCelcius again.
Topic archived. No new replies allowed.