Hi, i m having many errors on my work and needs to fix the code below
I need to calculate and output the result of the hourly temperatures for each hour of the day (i.e., 24 temperatures) and use array in this context.
I m having over 30 errors and some of errors are;
header file
Error 1 error C2065: 'array' : undeclared identifier
Error 2 error C2062: type 'int' unexpected
#ifndef weather_h // Avoid duplicate compilations
#define weather_h
void GetTemperatures(array<int, NUM_TEMPERATURES>& Temperatures);
int ComputeAverageTemp(array<int, NUM_TEMPERATURES>& Tempeartures, int AverageTemp);
void DisplayTemperatures(array<int, NUM_TEMPERATURES>& Temperatures, int AverageTemp);
int Low(array<int, NUM_TEMPERATURES>& Tempeartures);
int High(array<int, NUM_TEMPERATURES>& Temperatures);
#endif
//main file//
#include<iostream> // for cin,cout
#include<limits> // Required for numeric_limits
#include<array>
#include<ctime>
#include"test.cpp"
#include"weather.h"
using namespace std;
int main()
{
// declare local constant(s), variable(s), and array(s)
array<int, 24> HourlyTemperatures;
const int NUM_TEMPERATURES = 24;
int Temperatures[NUM_TEMPERATURES];
int AverageTemp = 0;
char run;
cout << endl;
cout << "Press 'A' continue - Press 0 to quit.";
cin >> run;
do
{
GetTemperatures(array<int, NUM_TEMPERATURES>& Temperatures);
Sleep(1500);
AverageTemp = ComputeAverageTemp(array<int, NUM_TEMPERATURES>& Temperatures); //take the last parameter out of the ComputeAverageTemp function
Sleep(1500);
int lowTemp = Low(array<int, NUM_TEMPERATURES>& Temperatures);
Sleep(1500);
int highTemp = High(array<int, NUM_TEMPERATURES>& Temperatures);
Sleep(1500);
DisplayTemperatures(array<int, NUM_TEMPERATURES>& Temperatures, AverageTemp, lowTemp, highTemp); //update your header prototype for DisplayTemperatures
Sleep(3500);
} while (run == 0);
cout << " Okay - Another Time ";
return 0;
}
//test.cpp//
#include<iostream> // for cin,cout
#include<iomanip> // for formatting cout
#include<array>
#include"weather.h" // for linking to main cpp file
using namespace std;
void GetTemperatures(array<int, NUM_TEMPERATURES>& Temperatures)
{
int CurrentTemp = 0;
for (int i = 0; i < NUM_TEMPERATURES; i++)
{
cout << "Input hourly temp: ";
cin >> Temperatures[i];
while ((Temperatures[i] >130) || (Temperatures[i]< -50))
{
cout << "Out of range - Try Again";
--i;
}
}
}
// takes the values in the array and returns high, low, and mean
int ComputeAverageTemp(array<int, NUM_TEMPERATURES>& Tempeartures, int AverageTemp);
{
//average the values in the array
int sum = 0;
for (int i = 0; i < NUM_TEMPERATURES; i++)
{
sum += Temperatures[i];
}
AverageTemp = sum / NUM_TEMPERATURES;
return AverageTemp;
}
int High(array<int, NUM_TEMPERATURES>& Temperatures);
{
//find the highest value in the array
int highValue = Temperatures[0];
for (int i = 0; i<NUM_TEMPERATURES; i++)
{
if (Temperatures[i] > highValue)
{
highValue = Temperatures[i];
}
}
return highValue;
}
int Low(array<int, NUM_TEMPERATURES>& Temperatures);
{
//find the lowest value in the array
int lowValue = Temperatures[0];
for (int i = 0; i < NUM_TEMPERATURES; i++)
{
if (Temperatures[i] < lowValue)
{
lowValue = Temperatures[i];
}
}
return lowValue;
}
// displays the full run of values, the high, low, and mean
void DisplayTemperatures(array<int, NUM_TEMPERATURES>& Temperatures, int AverageTemp);
{
cout << fixed << setprecision(2);
cout << "Hour" << setw(9) << " Temperature\n";
for (int x = 0; x<10; ++x)
{
cout << setw(2) << "0" << x << ":00" << setw(7) << Temperatures[x] << endl;
}
for (x = 10; x<NUM_TEMPERATURES; ++x)
{
cout << setw(3) << x << ":00" << setw(7) << Temperatures[x] << endl;
}
Sleep(1500);
cout << setw(8) << "High" << setw(9) << highTemp << endl;
cout << setw(8) << "Low" << setw(9) << lowTemp << endl;
cout << setw(8) << "Average" << setw(9) << AverageTemp << endl;
return;
}
In your header file, you are missing the scope qualifier std:: in front of array.
In your main file, you are not calling the functions correctly. AverageTemp = ComputeAverageTemp(array<int, NUM_TEMPERATURES>& Temperatures); should be AverageTemp = ComputeAverageTemp(Temperatures);
I'm not sure if you have any more errors, but if there are I'm sure others can point them out. You should really post your code inside the code tags. [ code ] [ /code ] without the spaces.
Thank you fg109.
Once I input the way you wrote, there is no error for header file.
I still get about 30 errors from main and test.cpp.
In test cpp, do I needto change void GetTemperatures(array<int, NUM_TEMPERATURES>& Temperatures) to void GetTemperatures(std::array<int, NUM_TEMPERATURES>& Temperatures); ?
If so, do I also need to make the change for int ComputeAverageTemp, void DisplayTemperatures, int Low, int High too?
In your test.cpp, you have usingnamespace std; so you don't need to put std:: in front of array. You should post your errors, and put your source code inside the code brackets.
//Test.cpp//
Error 1 error C2447: '{' : missing function header (old-style formal list?)
Warning 5 warning C4091: '' : ignored on left of 'const int' when no variable is declared
23 IntelliSense: expected a declaration
//main.cpp//
Error 6 error C2143: syntax error : missing ';' before 'constant'
Error 7 error C2106: '=' : left operand must be l-value
Error 8 error C2065: 'Temperatures' : undeclared identifier
Error 9 error C2275: 'std::array<int,24>' : illegal use of this type as an expression
Error 10 error C3861: 'Sleep': identifier not found
Error 21 error C2660: 'DisplayTemperatures' : function does not take 4 arguments
//test.cpp//
#include<iostream> // for cin,cout
#include<iomanip> // for formatting cout
#include<array>
#include"weather.h" // for linking to main cpp file
usingnamespace std;
void GetTemperatures(array<int, NUM_TEMPERATURES>& Temperatures)
{
int CurrentTemp = 0;
for (int i = 0; i < NUM_TEMPERATURES; i++)
{
cout << "Input hourly temp: ";
cin >> Temperatures[i];
while ((Temperatures[i] > 130) || (Temperatures[i]< -50))
{
cout << "Out of range - Try Again";
--i;
}
}
}
// takes the values in the array and returns high, low, and mean
int ComputeAverageTemp(array<int, NUM_TEMPERATURES>& Tempeartures, int AverageTemp);
{
//average the values in the array
int sum = 0;
for (int i = 0; i < NUM_TEMPERATURES; i++)
{
sum += Temperatures[i];
}
AverageTemp = sum / NUM_TEMPERATURES;
return AverageTemp;
}
int High(array<int, NUM_TEMPERATURES>& Temperatures);
{
//find the highest value in the array
int highValue = Temperatures[0];
for (int i = 0; i<NUM_TEMPERATURES; i++)
{
if (Temperatures[i] > highValue)
{
highValue = Temperatures[i];
}
}
return highValue;
}
int Low(array<int, NUM_TEMPERATURES>& Temperatures);
{
//find the lowest value in the array
int lowValue = Temperatures[0];
for (int i = 0; i <NUM_TEMPERATURES; i++)
{
if (Temperatures[i] < lowValue)
{
lowValue = Temperatures[i];
}
}
return lowValue;
}
// displays the full run of values, the high, low, and mean
void DisplayTemperatures(array<int, NUM_TEMPERATURES>& Temperatures, int AverageTemp);
{
cout << fixed << setprecision(2);
cout << "Hour" << setw(9) << "Temperature\n";
for (int x = 0; x<10; ++x)
{
cout << setw(2) << "0" << x << ":00" << setw(7) << Temperatures[x] << endl;
}
for (x = 10; x<NUM_TEMPERATURES; ++x)
{
cout << setw(3) << x << ":00" << setw(7) << Temperatures[x] << endl;
}
Sleep(1500);
cout << setw(8) << "High" << setw(9) << highTemp << endl;
cout << setw(8) << "Low" << setw(9) << lowTemp << endl;
cout << setw(8) << "Average" << setw(9) << AverageTemp << endl;
return;
}
For error 1 (and presumably errors 2-5), get rid of all those semicolons after your function headers. These are not function prototypes. These are function definitions, and you should not make the compiler think that the function header and function body are separate.
I'm not sure what to make of the warnings.
main.cpp
Not sure what error 6 means.
Error 7 is because you used #define NUM_TEMPERATURES 24 in your header file. This is a preprocessor directive, and means that before compiling, your compiler will replace all NUM_TEMPERATURES with 24.
In other words, to the compiler line 16 is constint 24 = 24;.
Error 8, where did you get Temperatures from? You did not declare it in the code. You only declared something called HourlyTemperatures.
Error 9, in your main function, you are trying to assign values to variables by using function calls. Except that you are not doing it correctly. Look up the difference between function calls and function prototypes.
Error 10, the Sleep function is not defined in any of the libraries you included. It is a function from the windows.h library.
Error 21, I assume you already know the problem and how to fix it from your comment.
I was able to solve some errors based on your note. However, I still have got about 15 errors. I didnt understood which line's semicolon i should remove, so I m still getting same error.
Following are the errors i m getting.
Test.cpp
Error1-3 error C2059: syntax error : ')'
Error 4 error C2143: syntax error : missing ';' before '{'
Error 5-8 error C2447: '{' : missing function header (old-style formal list?)
Error 9 IntelliSense: expected a '{' introducing a lambda body
Error 10 IntelliSense: expected a type specifier
Error11 IntelliSense: expected an expression
Error12-15 IntelliSense: expected a declaration
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
//header file//
#ifndef weather_h // Avoid duplicate compilations
#define weather_h
#include <array>
#define NUM_TEMPERATURES 24
#define Temperatures
void GetTemperatures(std::array<int, NUM_TEMPERATURES>& Temperatures);
int ComputeAverageTemp(std::array<int, NUM_TEMPERATURES>& Temperatures, int AverageTemp);
void DisplayTemperatures(std::array<int, NUM_TEMPERATURES>& Temperatures, int AverageTemp);
int Low(std::array<int, NUM_TEMPERATURES>& Temperatures);
int High(std::array<int, NUM_TEMPERATURES>& Temperatures);
#endif
//test.cpp//
#include<iostream> // for cin,cout
#include<iomanip> // for formatting cout
#include<array>
#include"weather.h" // for linking to main cpp file
usingnamespace std;
void GetTemperatures(array<int, NUM_TEMPERATURES>& Temperatures)
{
int CurrentTemp = 0;
for (int i = 0; i < NUM_TEMPERATURES; i++)
{
cout << "Input hourly temp: ";
cin >> Temperatures[i];
while ((Temperatures[i] > 130) || (Temperatures[i]< -50))
{
cout << "Out of range - Try Again";
--i;
}
}
}
// takes the values in the array and returns high, low, and mean
int ComputeAverageTemp(array<int, NUM_TEMPERATURES>& Tempeartures, int AverageTemp);
{
//average the values in the array
int sum = 0;
for (int i = 0; i < NUM_TEMPERATURES; i++)
{
sum += Temperatures[i];
}
AverageTemp = sum / NUM_TEMPERATURES;
return AverageTemp;
}
int High(array<int, NUM_TEMPERATURES>& Temperatures);
{
//find the highest value in the array
int highValue = Temperatures[0];
for (int i = 0; i<NUM_TEMPERATURES; i++)
{
if (Temperatures[i] > highValue)
{
highValue = Temperatures[i];
}
}
return highValue;
}
int Low(array<int, NUM_TEMPERATURES>& Temperatures);
{
//find the lowest value in the array
int lowValue = Temperatures[0];
for (int i = 0; i <NUM_TEMPERATURES; i++)
{
if (Temperatures[i] < lowValue)
{
lowValue = Temperatures[i];
}
}
return lowValue;
}
// displays the full run of values, the high, low, and mean
void DisplayTemperatures(array<int, NUM_TEMPERATURES>& Temperatures, int AverageTemp);
{
cout << fixed << setprecision(2);
cout << "Hour" << setw(9) << " Temperature\n";
for (int x = 0; x<10; ++x)
{
cout << setw(2) << "0" << x << ":00" << setw(7) << Temperatures[x] << endl;
}
for (x = 10; x< NUM_TEMPERATURES; ++x)
{
cout << setw(3) << x << ":00" << setw(7) << Temperatures[x] << endl;
}
Sleep(1500);
cout << setw(8) << "High" << setw(9) << highTemp << endl;
cout << setw(8) << "Low" << setw(9) << lowTemp << endl;
cout << setw(8) << "Average" << setw(9) << AverageTemp << endl;
return;
}
You obviously didn't understand most of what I said in my last post. You should really review the basics.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
int add(int a, int b); // this is a function prototype
int main()
{
int one = 1;
int two = 2;
std::cout << add(one, two) << std::endl; // this is a function call
// notice you don't give variable type
return 0;
}
// this is a function definition
int add(int a, int b) // this is the function header, notice there is no semicolon
{
// the stuff in these brackets is the function body
return a + b;
}
What I said about the constint 24 = 24; meant that you should get rid of that line. That's because the left side of an assignment operation always has to be a variable. Also, you can never declare a variable with a name that starts with a number.