M
mattcshort
Ok here's another program I have somewhat working. It runs fine if I
don't cause it to invalidate my input, but if I enter a number greater
than 32 when it goes to calculate the high and low months I get a
memory error. There's a sample at the bottom that shows it works with
valid input.
The error: The instruction at "xxxxxxx" referenced memory at "xxxxxx".
The memory could not be "read".
I'm pretty sure it has something to do with the way I am validating my
input, but one thing I am trying to avoid is having it says what valid
input IS every time it asks. An if statement would clear that up, but I
thought it redundant to have the while loop validating AND the if
statment validating.
Thanks,
Matt
Here's my code so far:
/*
Program 6: Program number 2 on page 473
C++ 171-01
Program Description:This program lets the user enter the total rainfall
for each of 12 months into an array.
Variables: count for counters,
month is the array for the months,
high is the subscript of the month array
total is the total rainfall,
average is the average rainfall
name is the of the array of months within
size is the size of the array
highLow is the test value for the function
Functions: displayMessage: asks for the rainfall for each month
totalAmount calculates the total amount of rainfall
averageAmount calculates the average rainfall
displayValue calculates the high and low months
*/
#include <iostream>
using namespace std;
double displayMessage (char name[]);
double totalAmount (double amount[], int size);
double averageAmount (double amount[], int size);
int displayValue(double amount[], int size, int highLow);
int main()
{
char month[12][10] = { "January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"};
double amount[12];
int high;
double total, average;
for (int count = 0; count <12; count++)
{
amount[count] = displayMessage (month[count]);
}
total = totalAmount(amount, 12);
cout << "\nThe total rainfall for the year is: " << total << endl;
average = averageAmount (amount, 12);
cout << "The average rainfall for the year is: " << average << endl;
high = displayValue(amount, 12, 1);
cout << endl << "The month with the highest rain is " << month[high]
<< " with the amount of " << amount[high] << endl;
high = displayValue(amount, 12, 0);
cout << endl << "The month with the lowest rain is " << month[high] <<
" with the amount of " << amount [high] << endl;
return 0;
}
double displayMessage (char name[])
{
double rainfall;
while (rainfall < 0.0 || rainfall > 32.0)
{
cout << "Enter the rainfall for " << name << " : "; cin >>
rainfall;
cout << "\nRainfall must be between 0 and 32\n";
}
return rainfall;
}
double totalAmount (double amount[], int size)
{
int total =0;
for (int count =0; count < size; count++)
total += amount[count];
return total;
}
double averageAmount (double amount[], int size)
{
double total = 0;
double average;
for (int count =0; count < size; count++)
total += amount[count];
average = total/size;
return average;
}
int displayValue(double amount[], int size, int highLow)
{
int count;
int highest;
int big=0;
int lowest;
int high;
if (highLow == 0)
{
lowest = amount[0];
for (count=1; count < size; count++)
{
if (amount[count] < lowest)
{
lowest = amount[count]; high = count;
}
}
}
else
{
highest = amount[0];
for (count = 1; count < size; count++)
{
if (amount[count] > highest)
{
highest = amount[count];high=count;
}
}
}
return high;
}
/*
Enter the rainfall for January : 2
Enter the rainfall for February : 2
Enter the rainfall for March : 2
Enter the rainfall for April : 2
Enter the rainfall for May : 2
Enter the rainfall for June : 2
Enter the rainfall for July : 2
Enter the rainfall for August : 2
Enter the rainfall for September : 2
Enter the rainfall for October : 2
Enter the rainfall for November : 1
Enter the rainfall for December : 30
The total rainfall for the year is: 51
The average rainfall for the year is: 4.25
The month with the highest rain is December with the amount of 30
The month with the lowest rain is November with the amount of 1
Press any key to continue
*/
don't cause it to invalidate my input, but if I enter a number greater
than 32 when it goes to calculate the high and low months I get a
memory error. There's a sample at the bottom that shows it works with
valid input.
The error: The instruction at "xxxxxxx" referenced memory at "xxxxxx".
The memory could not be "read".
I'm pretty sure it has something to do with the way I am validating my
input, but one thing I am trying to avoid is having it says what valid
input IS every time it asks. An if statement would clear that up, but I
thought it redundant to have the while loop validating AND the if
statment validating.
Thanks,
Matt
Here's my code so far:
/*
Program 6: Program number 2 on page 473
C++ 171-01
Program Description:This program lets the user enter the total rainfall
for each of 12 months into an array.
Variables: count for counters,
month is the array for the months,
high is the subscript of the month array
total is the total rainfall,
average is the average rainfall
name is the of the array of months within
size is the size of the array
highLow is the test value for the function
Functions: displayMessage: asks for the rainfall for each month
totalAmount calculates the total amount of rainfall
averageAmount calculates the average rainfall
displayValue calculates the high and low months
*/
#include <iostream>
using namespace std;
double displayMessage (char name[]);
double totalAmount (double amount[], int size);
double averageAmount (double amount[], int size);
int displayValue(double amount[], int size, int highLow);
int main()
{
char month[12][10] = { "January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"};
double amount[12];
int high;
double total, average;
for (int count = 0; count <12; count++)
{
amount[count] = displayMessage (month[count]);
}
total = totalAmount(amount, 12);
cout << "\nThe total rainfall for the year is: " << total << endl;
average = averageAmount (amount, 12);
cout << "The average rainfall for the year is: " << average << endl;
high = displayValue(amount, 12, 1);
cout << endl << "The month with the highest rain is " << month[high]
<< " with the amount of " << amount[high] << endl;
high = displayValue(amount, 12, 0);
cout << endl << "The month with the lowest rain is " << month[high] <<
" with the amount of " << amount [high] << endl;
return 0;
}
double displayMessage (char name[])
{
double rainfall;
while (rainfall < 0.0 || rainfall > 32.0)
{
cout << "Enter the rainfall for " << name << " : "; cin >>
rainfall;
cout << "\nRainfall must be between 0 and 32\n";
}
return rainfall;
}
double totalAmount (double amount[], int size)
{
int total =0;
for (int count =0; count < size; count++)
total += amount[count];
return total;
}
double averageAmount (double amount[], int size)
{
double total = 0;
double average;
for (int count =0; count < size; count++)
total += amount[count];
average = total/size;
return average;
}
int displayValue(double amount[], int size, int highLow)
{
int count;
int highest;
int big=0;
int lowest;
int high;
if (highLow == 0)
{
lowest = amount[0];
for (count=1; count < size; count++)
{
if (amount[count] < lowest)
{
lowest = amount[count]; high = count;
}
}
}
else
{
highest = amount[0];
for (count = 1; count < size; count++)
{
if (amount[count] > highest)
{
highest = amount[count];high=count;
}
}
}
return high;
}
/*
Enter the rainfall for January : 2
Enter the rainfall for February : 2
Enter the rainfall for March : 2
Enter the rainfall for April : 2
Enter the rainfall for May : 2
Enter the rainfall for June : 2
Enter the rainfall for July : 2
Enter the rainfall for August : 2
Enter the rainfall for September : 2
Enter the rainfall for October : 2
Enter the rainfall for November : 1
Enter the rainfall for December : 30
The total rainfall for the year is: 51
The average rainfall for the year is: 4.25
The month with the highest rain is December with the amount of 30
The month with the lowest rain is November with the amount of 1
Press any key to continue
*/