I NEED HELP WITH THIS CODE


my array for Celsius is working, but I can't figure out how to do the array to show the correct Fahrenheit conversion for each Celsius entered.
( my formula for conversion is wrong, I just put that to get it run)

#include <iostream>
using namespace std ;

int main ()
{
int x ,
arraysize = 10 ,
num_temps = 0;
double temps [ arraysize] ;
fahrenheit [ arraysize ] ;

do
{
cout << " how many temperatures (max 10)" ;
cin >> num_temps ;

for (int y = 0 ; y < num_temps ; y ++)
{
cout << " enter temp in celsius " << y + 1 ;
cin >> temps [y] ;
}

for (int y=0 ; y< num_temps ; y ++)
{
fahrenheit[arraysize]= 5.0 * temps [y] ;

cout << " celsius " << "\t" << "fahrenheit" << endl;

}
for (int y=0 ; y< num_temps ; y ++ )
{


cout << temps[y] << "\t" << fahrenheit[arraysize] << endl ;
}


cout << " do you want to converter another temp ( 1 for yes or 2 to quit)" ;
cin >> x ;

system ("cls");




} while ( x== 1) ;




return 0 ;
}
Hi there.
First of all this fahrenheit[arraysize] is wrong, because in programing we start counting from 0, which means that if your array size is 5 for instance then the count will be 0 for the first cell, 1 for the second cell, 2 for the third cell, 3 for the forth cell, 4 for the fifth cell so when you do fahrenheit[arraysize] you want to access the 6th place in your array but it doesn't exist. Change it to fahrenheit[arraysize-1].

Secondly, here:
1
2
3
4
5
6
7
for (int y=0 ; y< num_temps ; y ++)
{
fahrenheit[arraysize]= 5.0 * temps [y] ;

cout << " celsius " << "\t" << "fahrenheit" << endl;

}


you convert the Celsius values and store them only in the last element of the array, which means you overwrite the last value each time. You need to store each conversion in a separate cell.

Hope that helps.
Last edited on
Hello tiareharo,

Please use code tags. It makes your code easier to read and also adds line numbers to better refer to your code.
Hint: edit your original post and highlight your code and press the <> button to the right of the window under format.

Your first lines where you define int x, it works better as:
1
2
const int arraysize = 10; // arraysize needs to be a constant value for defining the arrays. 
int x{ 0}, num_temps =0;

and what type of variable is fahrenheit?

In your for loop where it says: fahrenheit[arraysize]= 5.0 * temps [y] ; it should be written: fahrenheit[y]= temps [y] * 1.8 + 32 ;. This way the array "fahrenheit" will match calculations using the "temps" array. And the following cout is not necessary.

Hope that helps,

Andy
Topic archived. No new replies allowed.