How to name array elements in a for loop

Hi Everyone! First Post!!

I am writing a program that asks the user to input their mileage for each month. I have setup an array:

double Monthly[12] ;

And the code for the loop...

for (M=0; M<12; M++) {
cout << "Enter a monthly mileage amount: " ; // Creates the 12 input lines
cin >> Monthly[M] ;
}

I would like to have the months displayed on each line so that it reads "Enter the mileage for January: ". But I'm not sure how to "name" the array elements as the names of the month.

Thanks for your help!
Jason
create an array of 12 (const) char pointers: const char* month_name[12] = {"January", "February", ...};. Then cout << "enter mileage for " << month_name[M] << ": ";
Awesome, thanks for your help. But I do get some errors. The error (repeated) is on the cin >> line. I'm thinking I need to change how it is written, but I've never used const char* before so I'm not sure how.

Error = C2678: binary '>>' : no operator found which takes a left-hand operand of type 'std::istream' (or there is no acceptable conversion)

Code...

1
2
3
4
5
6
const char* monthName[12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"} ;

for (M=0; M<12; M++) {
   cout << "Enter mileage for " << monthName[M] << ":" ;  // Creates the 12 monthly input lines
   cin >> monthName[M] ;
   Annual = Annual + monthName[M] ;  }


Thanks again!!!
Why don't you use array of pairs or multimap instead?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <utility>
using namespace std;

int main()
{
        double Annual=0;
	pair<string,int> p[]={
							make_pair("January",0),
							make_pair("February",0),
							make_pair("March",0),
							make_pair("April",0),
							make_pair("May",0),
							make_pair("June",0),
							make_pair("July",0),
							make_pair("August",0),
							make_pair("September",0),
							make_pair("October",0),
							make_pair("November",0),
							make_pair("December",0)
						};

	for(int i=0;i<12;i++)
		{
                   cin>>p[i].second;//this your monthly mileage for each month
	           Annual+=p[i].second; 
                }
	return 0;
}
@Cre8ivPwr, with cin >> monthName[M] ; you're trying to read a string into monthName[M]. Though what you initially wanted to do is read a number into Monthly[M].

@chingi3, while a pair is an option (and so is writing your own struct) a map would be a waste of time and space. The code you posted though does not look any cleaner than OP's, so changing it wouldn't be of any benefit.
@hamsterman - Well, I do want to read numbers into Monthly[M], but I just wanted the cout in the loop to list the months for the user so they don't have to think about what month they are on.

Meaning, right now, the program looks like this...

Enter a monthly mileage amount:
Enter a monthly mileage amount:
Enter a monthly mileage amount:
Enter a monthly mileage amount:
Enter a monthly mileage amount:
Enter a monthly mileage amount:
Enter a monthly mileage amount:
Enter a monthly mileage amount:
Enter a monthly mileage amount:
Enter a monthly mileage amount:
Enter a monthly mileage amount:
Enter a monthly mileage amount:

But I would like it to read:

Enter mileage for January:
Enter mileage for February:
Enter mileage for March:
Enter mileage for April:
Enter mileage for May:
Enter mileage for June:
Enter mileage for July:
Enter mileage for August:
Enter mileage for September:
Enter mileage for October:
Enter mileage for November:
Enter mileage for December:

So the input would still be numbers going into Monthly[M]...I just want the entry lines in the loop to be labeled.

Any ideas?
Maybe I need 2 arrays? 1 for the month names and another for the monthly amounts the user submits?

But I'm not sure how to make them interact?
Okay, I did 2 different arrays and it works perfectly...below is the code. I'm very new to C++ so if anyone wants to look at my code and give suggestions (even just simple structure or spacing).

Thanks hamsterman for your help!!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/******************************************************/
/* File: Mileage Counter                              */
/*                                                    */
/* Created by: Jason Beers                            */
/* Date: December 12, 2010                            */
/*                                                    */
/* Program to take monthly mileage amounts and        */
/*   provide the average and annual amounts           */
/*                                                    */
/* Inputs: (keyboard)                                 */
/*   12 months of milage data (Monthly)               */
/*                                                    */
/* Output:                                            */
/*   Monthly Average & Annual Amounts (Avg & Annual)  */
/*                                                    */
/*                                                    */
/******************************************************/

#include "StdAfx.h"

using namespace std ;

#include <iostream>

#include <string>

void Intro()  // Intro to the program
{
	cout << " " << endl ;
	cout << "******************************************" << endl ;
	cout << "*                                        *" << endl ;
	cout << "*            Welcome Drivers!            *" << endl ;
	cout << "*                                        *" << endl ;
	cout << "*  This program is designed to provide   *" << endl ;
	cout << "*  you with the total amount of miles    *" << endl ;
	cout << "*   driven for a year as well as the     *" << endl ;
	cout << "*    average miles driven per month.     *" << endl ;
	cout << "*    Just enter the miles you drove      *" << endl ;
	cout << "*     for the last 12 months below.      *" << endl ;
	cout << "*                                        *" << endl ;
	cout << "******************************************" << endl ;	
	cout << " " << endl ;
	cout << " " << endl ;
}
int main()
{
  
        string monthName[12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"} ;  // Array to store month names
	double monthAmt[12] ;  // Array to store monthly amounts
	double Annual = 0 ;  // variable to find total annual amount
	double Avg = 0 ;  // variable to find average monthly mileage
	int M ;  // variable to perform the loop
	char Restart ; // variable allowing the user to restart the program if desired

	Intro () ;  // Writes the Intro into the program

	for (M=0; M<12; M++)
	    {
		cout << "Enter mileage for " << monthName[M] << ":" ;  // Creates the 12 monthly input lines
                cin >> monthAmt[M] ;
		Annual = Annual + monthAmt[M] ;
	    }

	Avg = Annual / 12 ;  // Calculates the average amount of miles driven for the year
	
	cout << " " << endl ;
	cout << " " << endl ;
	cout << "You drove " << Annual << " miles for the year " << endl ;  // displays the total miles driven
	cout << " " << endl ;
	cout << "You averaged " << Avg << " miles per month " << endl ;  // displays the average miles driven
	cout << " " << endl ;
	
	cout << "Would you like to run this program again? (enter 'y' for Yes; 'n' for No): " ; // Asks for restart or exit
	cin >> Restart ;
		if (Restart == 'y' || Restart == 'Y')  // Allows for lower or upper case letter
			{
			main () ;  // Rerun the program
			}
		else
			{
			return (0) ;  // Exit the program
			}
}
Topic archived. No new replies allowed.