Codarus
Codarus

Reputation: 447

Array Program Weird Output

I am trying to write a code to list gas mileage, and so far it works. The problem is that it does not work the way it should. The code below asks for input and then displays a list of said input along with the calculated MPG.

#include<iostream>
using namespace std;
//function prototype
void CalcAverage (int arr1[], float arr2[], int size);
//global variables
int mil [3];
float gal [3];
float mpg [3];
//main program
int main()
{
char month [7];
int day [3];
cout << "Please enter the starting month" << endl;
cin >> month;

cout << "Please enter starting day entry 1" << endl;
cin >> day [0];
cout << "entry 2" << endl;
cin >> day [1];
cout << "entry 3" << endl;
cin >> day [2];
cout << "entry 4" << endl;
cin >> day [3];

cout << "Please enter gallons entry 1" << endl;
cin >> gal [0];
cout << "entry 2" << endl;
cin >> gal [1];
cout << "entry 3" << endl;
cin >> gal [2];
cout << "entry 4" << endl;
cin >> gal [3];

cout << "Please enter miles entry 1" << endl;
cin >> mil [0];
cout << "entry 2" << endl;
cin >> mil [1];
cout << "entry 3" << endl;
cin >> mil [2];
cout << "entry 4" << endl;
cin >> mil [3];

CalcAverage (mil,gal,4);

cout << "Week of" << "\t\tGallons" << "\t\tMiles" << "\t\tMPG" << endl;
cout << "_ _ _ _ _ _ _" << "\t_ _ _ _ _" << "\t_ _ _ _"  << "_ _ _ _" << endl;
cout << month <<  " " << day [0] << "\t" << gal [0] << "\t\t" << mil [0] << "\t\t" << mpg [0] <<     endl;
cout << month <<  " " << day [1] << "\t" << gal [1] << "\t\t" << mil [1] << "\t\t" << mpg [1] <<     endl;
cout << month <<  " " << day [2] << "\t" << gal [2] << "\t\t" << mil [2] << "\t\t" << mpg [2] <<     endl;
cout << month <<  " " << day [3] << "\t" << gal [3] << "\t\t" << mil [3] << "\t\t" << mpg [3] <<     endl;

system("PAUSE");

}
//function definition
void CalcAverage (int arr1[], float arr2[], int size)
{
//statements
mpg [0] = mil [0] / gal [0];
mpg [1] = mil [1] / gal [1];
mpg [2] = mil [2] / gal [2];
mpg [3] = mil [3] / gal [3];

}

The code works and I do not get any errors. However, when the code displays the output, some of the results are switched or jumbled, or completely random (my 3rd gal input becomes my 1st mil input etc.) What is causing this problem, and how can I fix it?

Upvotes: 0

Views: 159

Answers (4)

elune
elune

Reputation: 338

change with this :

int mil [4];
float gal [4];
float mpg [4];

int day [4];

Upvotes: 1

rcs
rcs

Reputation: 7227

Note that when you declare an array like mil[3], the array has elements of mil[0], mil[1], and mil[2]. Thus it is not valid to use mil[3]. Change your array declaration to:

int mil [4];
float gal [4];
float mpg [4];

Also the day to use [4] instead of [3]

Upvotes: 2

John Kugelman
John Kugelman

Reputation: 362037

The arrays are all size 3 and you're trying to stuff 4 values into each. Increase their sizes by one:

int mil [4];
float gal [4];
float mpg [4];

...

int day [4];

Upvotes: 2

Crowman
Crowman

Reputation: 25936

You do things like:

cin >> day [3];

but you only have:

int day [3];

You're overwriting the end of your array and into the next one. You need to change that to int day[4], since day[3] is the fourth element, not the third.

Upvotes: 1

Related Questions