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?
#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;
}
#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)
}
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?
#include <iostream>
#include <vector>
usingnamespace 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");
}