Arrary Coding

If anyone can help i am unsure how to make an array for the months so that when you input the number the actual month is put out in its place. ???

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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{     
    string FirstLine="";
    //need an int variable
    int choicevariable = 0; 
   
    
   cout << "Welcome to The Time Machine" << endl << endl;
   cout << "What is your Name?";
   
   getline(cin, FirstLine); 
  
  //display user's name
   cout << "HI " << FirstLine << endl << endl;
  cout << "Select an option to get started:" << endl;
   
   
   // i get stuck trying to figure out how to loop this part i know what parts i have to loop i am just not sure how.
   while (choicevariable < 1 || choicevariable > 3)
   {
     
   cout << "1. Enter a Specific Date" << endl;
   cout << "2. Select a Time Period" << endl;
   cout << "3. Wildcard - I'm Feeling Lucky" << endl << endl;
   
   //get the user's choice using your int variable and cin
  
       cout << "Enter your choice: ";
       cin >> choicevariable;
       
       cin.clear();
     cin.ignore(100, '\n');
     } 
    
   
   
  
  
    
   //display thier choice
   cout << "#" << choicevariable << endl;
   int Month = 0;
   int Day = 0;
   int Year = 0;
     
    while (Month < 1 || Month > 12)
    {   
        cout << "What is the Month?" << endl;
        
        cin >> Month;
        }
    while (Day < 1 || Day > 7)
    {       
        cout << "What is the Day?" << endl;
        
        cin >> Day;
        }
          
        
        cout << "What is the Year?" << endl;
        
        while(!(cin >> Year)){
             
        cin.clear();
        cin.ignore(100, '\n');
        }
        cout << " Ok, We will send you to" << endl;  
        
        cout << "Ending Program";
   
    
    if (choicevariable == 2)
    
    
     {
            int timePeriod;              
        cout << "Choice from one of the time periods:" << endl << endl;
        
        cout << "1. Prehistoric Dinosaur Era" << endl;
        cout << "2. Pirate Era" << endl;
        cout << "3. Five Days ago" << endl;
        cout << "4. Mideval" << endl;
        
        cin >> timePeriod;
        cout << " Ok, We will take you to" << timePeriod << endl; 
        
        cout << "Ending Program";                      
   }                   
   
   if (choicevariable == 3)
   
      {
       cout << "Ok, I'll choose where to send you" << endl;                
      }                
      
      
      
   else if (choicevariable > 3)
    
    cout << "Choice Not Valid" << endl;
    cout << "Ending Program" << endl;                     
    
    system("PAUSE");
    return EXIT_SUCCESS;
}
Use a switch statement and an enumeration to select the month.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
enum MonthEnums
{
    JAN = 1,
    FEB = 2,
    ...
}

// Inside your function
string monthstr;
switch (Month)
{
    case JAN:
        monthstr = "January";
        break;
    case FEB:
        ...
}


This way, you can even keep track of invalid input, such as if user enters something 0 or below or 13 and above using a default case.
can i use this way without changing any of the code ? It has to do what it does polus the array and i keep getting errors with adding the string and switch part ?
Why not just use a lookup table:
1
2
3
4
5
6
7
8
9
10
11
    string monthNames[12] = {
    "January", "February", "March", "April",
    "May", "June", "July", "August",
    "September", "October", "November", "December" };
    
    string name;
        
    if (Month>0 && Month<13)
        name = monthNames[Month-1];
    else
        name = "Invalid Month";
You'll have to add some code. and you're probably better off writing a separate function to convert an integer month value to a string.
Topic archived. No new replies allowed.