Loop Help

I am currently in my 2nd semester of my CS classes and my teacher only provides help to a select few and does not give an option to help outside of class so I was hoping I could get some help on doing loops. I currently have to create a loop where you have the user enter a degree in celsius and it will output it in fahrenheit and do this 4 seperate times. I have gotten some progress but I feel like I am getting no where. Any help/hints would be helpful.
This is what I have so far(the First couple of cout's are format for class information then the format for the outputted information should be in
first iteration Celsius Fahrenheit

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

int main()
{
float cels, fahr;
char Fahrenheit, Celsius, Iterations;
const int MIN_NUMBER = 1, MAX_NUMBER = 4;
int iter = MIN_NUMBER;

cout << "\t CSCI 193 Program Two, Fall 2012\n";
cout << "______________________________________________________________________________\n";
cout << "Celsius Values entered by user and their equivalent Fahrenheit Values:\n";
cout << "Iterations Celsius Fahrenheit\n";
cout << "=================================\n";
while (iter <= MAX_NUMBER)
{
cout << "Please enter the Celsius Degree you would like to convert: ";
cin >> cels;
fahr = cels * 9 / 5 + 32 ;
cout << iter << "\t" << cels << "\t" << fahr << endl;
}
return 0;
}



Again thanks for any help provided.
Last edited on
I believe I need to move the initial question above the format also.
Please put your code in code tags its the <> symbol under format. It makes it easier for us to read and correct.
Okay sorry. Still newbie. Constructive criticism is excepted as well. From what I saw in other posts people don't take it very well.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <cmath>
#include <string>
#include <iomanip>
using namespace std;

int main()
{
	float cels, fahr;
	char Fahrenheit, Celsius, Iterations;
	const int MIN_NUMBER = 1, MAX_NUMBER = 4;
	int iter = MIN_NUMBER;

	cout << "\t CSCI 193 Program Two, Fall 2012\n";		//Class Format
	cout << "______________________________________________________________________________\n";  
	cout << "Celsius Values entered by user and their equivalent Fahrenheit Values:\n";       //Looped question
	cout << "Iterations Celsius Fahrenheit\n";  //format for output
	cout << "=================================\n";
	while (iter <= MAX_NUMBER)
	{
		cout << "Please enter the Celsius Degree you would like to convert: ";  //Looped question
		cin >> cels;
		fahr = cels * 9 / 5 + 32 ;
		cout << iter << "\t" << cels << "\t" << fahr << endl;
	}
	return 0;
}
Its cool, well, there are 3 different kinds of loops, do, while, and for. there is also do while. so it depends on what you need. If you need it to output 4 times, then a for loop might be best

1
2
3
4
for(int i = 0; i < 4; i++)
{
   //code to output 4 times here
}
Last edited on

After cout << iter << "\t" << cels << "\t" << fahr << endl;

write something that will update the while condition.

Like iter = iter +1;

something like that.
CH1156: Yea I need it to output 4 times but not until after I have asked for 4 different degrees.

EasyGoing: I'll try that now.
EasyGoing: That fixed the continous looping thanks!! Probably just formatting I need to get now.
Do you have to do error checking?
Awesome I fixed the formatting and it works great now! Just gotta add something to not close the command window automatically. Think I saw something when I first came into the forums though.

Thanks again!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <cmath>
#include <string>
#include <iomanip>
using namespace std;

int main()
{
	float cels, fahr;
	char Fahrenheit, Celsius, Iterations;
	const int MIN_NUMBER = 1, MAX_NUMBER = 4;
	int iter = MIN_NUMBER;

	while (iter <= MAX_NUMBER)
	{
		cout << "Please enter the Celsius Degree you would like to convert: ";  //Looped question
		cin >> cels;
		fahr = cels * 9 / 5 + 32 ;
		cout << iter << "\t" << cels << "\t" << fahr << endl;
		iter = iter +1;
	}
	cout << "\t CSCI 193 Program Two, Fall 2012\n";		//Class Format
	cout << "______________________________________________________________________________\n";  
	cout << "Celsius Values entered by user and their equivalent Fahrenheit Values:\n";       //Looped question
	cout << "Iterations Celsius Fahrenheit\n";  //format for output
	cout << "=================================\n";
	
	return 0;
}
What do you mean by error checking?
If the user enters bad data..

Like instead of inputting a degree in Celsius the user inputted letters and/or special characters.

Does your program have a way of stopping the user from continuing until he corrects his mistake? or would it just go into error mode?
I got ahead of myself. Its only outputting one Iteration when its suppose to be doing 4. The first Iteration also shows up as 5 on the chart.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <cmath>
#include <string>
#include <iomanip>
#include <limits>
using namespace std;

int main()
{
	float cels, fahr;
	char Fahrenheit, Celsius, Iterations;
	const int MIN_NUMBER = 1,
			  MAX_NUMBER = 4;
	int iter = MIN_NUMBER;

	while (iter <= MAX_NUMBER)
	{
		cout << "Please enter the Celsius Degree you would like to convert: ";  //Looped question
		cin >> cels;
		fahr = cels * 9 / 5 + 32 ;
		iter = iter + 1;
	}
	cout << "\t CSCI 193 Program Two, Fall 2012\n";		//Class Format
	cout << "______________________________________________________________________________\n";  
	cout << "Celsius Values entered by user and their equivalent Fahrenheit Values:\n";       //Looped question
	cout << "Iterations Celsius Fahrenheit\n";  //format for output
	cout << "=================================\n";
	cout << iter << "\t" << cels << "\t" << fahr << endl;
	iter++;
	system ("PAUSE");

	return 0;
}
Shouldn't the cout << iter << "\t" << cels << "\t" << fahr << endl;

between lines 21 and 22.

and the iter++ on line 29 is pointless, no?
If I move the cout << iter << "\t" << cels << "\t" << fahr << endl;
between 21 and 22 then it gives me the answers right after I ask. Trying to get it in a graph after I ask 4 times. Should I do the For loop like Ch1156 said earlier?

Also removed the iter++ cause like you said it made no difference.
"Yea I need it to output 4 times but not until after I have asked for 4 different degrees."

I think you could use two arrays to store your values before doing the outputs. One array to store the Celsius values & another to store the Fahrenheit values.
line 21 can be written as iter++;

It is the increment operator.

Another one is += which can use to increase by a particular amount as in:

MyNum += 5; //increments MyNum by 5

There are lots of different assignment oparators, that work in a similar fashion.
Last edited on
Your program is supposed to loop, so you should be using an array (like leftcoast says).

Basically:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
const int SIZE = 4;  
double celsius[SIZE];

for (int i = 0; i < SIZE; i++)
{
  cout << "Please enter celsius value number " << i +1 << ": ";
  cin << celsius[i];
  cin.ignore(80, '\n'); 
}

cout << "Thanks :) Here are the numbers converted to farenhiet:\n";

for (int i = 0; i < SIZE; i++)
{
  cout << i + 1 << ") " << celsius[i] * 9 / 5 + 32 << endl;
}

//  Another way to pause:
cout << "Press enter to continue...";
cin.get();


Alright after I turned this in the teacher told me that I was not suppose to do it like that. Instead he failed to mention that he wanted it to be a output to a .dat file. My current setup for this is as follows. Not sure how to get it to go into the forementioned chart.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <cmath>
#include <string>
#include <iomanip>
#include <limits>
#include <fstream>

using namespace std;

int main()
{
	float cels, fahr;
	char Fahrenheit, Celsius, Iterations;
	int MIN_NUMBER = 1,
		MAX_NUMBER = 4;
	int iter;
	for (int iter = MIN_NUMBER; iter <= MAX_NUMBER; iter++)
	{
		cout << "Please enter the Celsius Degree you would like to convert: ";  //Looped question
		cin >> fahr;
		cels = (fahr - 32) * 5 / 9 ;											//Cels to Fahr Conversion
		cout << iter << "\t" << cels << "\t" << fahr << endl;
	}
	cout << "Now writing data to the file.\n";
	ofstream outputFile;														//Outputting to File
		outputFile.open("outfile.dat");
			outputFile << "\tCSCI 193 Program Two, Fall 2012\n";
			outputFile << "_____________________________________________________________________\n";
			outputFile << "Fahrenheit Values entered by user and their equivalent Celsius Values\n";
			outputFile << "Iterations\t Fahrenheit\t Celsius\n";
			outputFile << "=====================================================================\n";
			outputFile << iter << "  \t" << fahr << "\t\t" << cels << endl;
			outputFile.close();
	cout << "Done.\n";
	system ("PAUSE");

	return 0;
}
Topic archived. No new replies allowed.