help Fahrenheit to Celsius

Hello,

I started to learn C++ in the last days with the info on this webiste, and for practicing I started to solve problems from codeabbey. I have a problem translating my thoughts to C++ codes, anyway i tried to solve problem which asking to convert Fahrenheit to Celsius, for example : i need to write how many numbers I want to convert, and than convert them and round them up (1.5=2; 1.3=1; 1.7=2). So when I did it, it converted only the last number, what do I do wrong here?



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
 #include <iostream>

using std::cin ;
using std::cout ;
using std:: endl;

int main()
{
	int n ;
	float num1 ;
	int i =0;

	cout << "the n values you want to convert : " << endl;
	cin >> n ;
	
	cout << " Enter the F values you want to convert : " << endl ;
	while ( i < n) 
		{	
			cin >> num1 ;
			i++;
		}

		cout << endl;
		
	for (i = 0; i < n ; n++)
			{	
					cout <<	" the converted numbers are : " << ceil (num1-32)/1.8 <<  endl ;
					
			}
			cout << endl;

}
Line 19: You do cin to num1 on every iteration of the loop. What do you think happens to the previous value? Hint: You overwrite it.
Hi
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
#include <iostream>
#include <cmath>
using std::cin ;
using std::cout ;
using std:: endl;

int main() {
	int n ;     // count of values
	float num1 ;  // value itself
	int i =0;

	cout << "the n values you want to convert : " << endl;
	cin >> n ;    // input count of values

	cout << " Enter the F values you want to convert : " << endl ;
	while ( i < n) {
		cin >> num1 ;       // U input only one value on ur variable, after value of ur variable refreshed. Becouse you will not write the previous value
		i++;              
		cout <<	" the converted numbers are : " << ceil (num1-32)/1.8 <<  endl ;  // based on this u need to converting this value now
	}

	cout << endl;

	return 0;
// storege few element need to use array or vector, in your case only 1 variable, value which is overwritten with each iteration (the passage through the cycle)
}
Last edited on
Ok, thanks for the answers, I made poor mistake but I think I got the solution.
Just one problem I got warning when I round it up (line 27-28), what the reason for it and how I can fix at?

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
#include <iostream>

using std::cin ;
using std::cout ;
using std:: endl;
const float size = 6;

int main()
{
	int b = 1;
	int i =0 ;
	float arr[6] ;
	
	cout << "please enter" << 6 << "numbers" ;
	for (i = 0; i < 6; i++)
		cin >> arr [i];
	
	cout << "Here are the numbers to convert: " << endl;
	for (b = 1; b < 6; b++)
	{
		cout <<  arr [b] << "\t" << endl ;
	}

	cout << "The numbers converted to Celsius are: " << endl;
	for (b = 1; b < 6; b++)
	{
		int result = floor ((0.5+ (arr [b] -32) / 1.8) );
		cout << result << "\t" ;
			
	}
}
Last edited on
Right now, the compiler doesn't know what 'floor' is. You need to include <cmath> - that's where 'floor' is defined.

http://www.cplusplus.com/reference/cmath/
Last edited on
I still get warning of possible loss of data (line27-28), maybe I round the answer in a wrong way?
int result = floor ((0.5+ (arr [b] -32) / 1.8) );
Should be :
int result = static_cast<int>(floor ((0.5+ (arr [b] -32) / 1.8)));
Thankes! it worked. Can you explain what the problem was and how you fixed it? (so I wont make this mistake again)
floor() returns a double. Converting a double into an int results in a warning.
http://en.cppreference.com/w/cpp/language/static_cast
Why not just use round()?


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
#include <iostream>
#include <vector>

using namespace std;

int main()
{
	float input;
	int result;
	int size;
	vector<float> vec;

	cout << "How many number would you like to convert? ";
	cin >> size;

	for (int i = 0; i < size; i++){
		cout << "Please enter number " << i+1 << ": ";
		cin >> input;
		vec.push_back(input);
	}

	cout << "\nHere are the results: \n";
	for (int b = 0; b < size; b++)
	{
		result = round((0.5 + (vec[b] - 32) / 1.8));
		cout << vec[b] << " Fahrenheit is " << result <<" Celsius \n";
	}

	system("pause");
}
I didnt know that cmd, but yea it's better way.
Topic archived. No new replies allowed.