Change date from numerical to literal

I have a program for a class that i am really struggling with. Unfortunately the pre-req classes i took, were taught by a professor dying of cancer. It is sad, but he was in no way fit to teach at this point and i learned absolutely nothing. Class wasn't worth going to, even though i did go. So i struggle with some basic concepts, sorry if i seem "dense".

The assignment is make a program that uses arrays to take in numerical date like 12/19/2012 and turn it into December Nineteenth Two Thousand and Twelve. The assignment is geared to help us use arrays. We also need to account for the leap year in February, i have the formula for that but, am currently lost in my attempts to finish this. This is the code i have:



1
2
3
4
5
6
7
8
9
10
11
12
13
#include<iostream>
#include<iomanip>
#include<fstream>
using namespace std;

int main()
{[code]
	int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
	
       string month[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; 
	

       string todays[] = {"First", "Second", "Third", "Fourth", "Fifth",  "Sixth", "S[code] 
eventh", "Eighth", "Ninth", "Tenth", "Eleventh", "Twelveth", "Thirteenth",[/code] "Fourteenth", "Fifteenth", "Sixteenth", "Seventeenth", "Eighteenth", "Nineteenth", "Twentieth", "Twenty First", "Twenty Second", "Twenty Thirt", "Twenty Fourth", "Twenty Fifth", "Twenty Sixth", "Twenty Seventh", "Twenty Eighth", "Twenty Ninth", "Thirtieth", "Thiry First"};


int m;
int y;
int d;

cout << "Please enter a date starting with the month: " << endl;

cin >> m;

if(m < 1 || m > 12)
{
cout << "Invalid month entry!" << endl;
cout << "Enter valid month: " << endl;
cin >> m;
}


cout << "Please enter a date: " << endl;
cout << "Month: " << endl;
cin << m << endl;
cout << "Day: " << endl;
cin >> d; << endl;
cout << "Year: " << endl;
cin << y << endl;

if( (y%4) == 0 && (y!= 100) || (y%400) == 0)
days[1] = 29;
else
days[1] = 28;

if(y < 2000 || y > 2090)
cout << "Invalid year!" << endl; << "Please enter a valid year: " << endl;


//if(d <= days[d])

cout << month[m-1] << "," << todays[d-1] << y << endl;

system("pause");

}[/code]

I currently get over 100 errors when i compile. Does anyone have any tips or help for me? I am just utterly lost and i can't find anything useful on forums or google that applies to this. Any help is appreciated! Thanks!

<3Andrea
You always use the extraction operator (>>) with an istream and not the insertion operator (<<). Why are you attempting to use endl with an istream?

endl is an ostream manipulator which outputs a newline and flushes the stream. cin doesn't output anything


1
2
if(y < 2000 || y > 2090)
    cout << "Invalid year!" << endl; << "Please enter a valid year: " << endl;

is equivalent to:

1
2
3
4
if(y < 2000 || y > 2090)
    cout << "Invalid year!" << endl; 

<< "Please enter a valid year: " << endl;


#include <string> is required.
Please fix the code tags :)

1
2
3
4
5
6
7
cout << "Please enter a date: " << endl;
cout << "Month: " << endl;
cin << m << endl;
cout << "Day: " << endl;
cin >> d; << endl;
cout << "Year: " << endl;
cin << y << endl;


The input operator << doesn't work with cin. I'm guessing you wanted to do cin >> m ?
What is this part supposed to do? You already have code to input the month so it seems redundant
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
#include<iostream>
#include<iomanip>
#include<fstream>
using namespace std;

int main()
{
    int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

  string month[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};


  string todays[] = {"First", "Second", "Third", "Fourth", "Fifth",  "Sixth", "Seventh", "Eighth", "Ninth", "Tenth", "Eleventh", "Twelveth", "Thirteenth", "Fourteenth", "Fifteenth", "Sixteenth", "Seventeenth\
", "Eighteenth", "Nineteenth", "Twentieth", "Twenty First", "Twenty Second", "Twenty Thirt", "Twenty Fourth", "Twenty Fifth", "Twenty Sixth", "Twenty Seventh", "Twenty Eighth", "Twenty Ninth", "Thirtieth", "\
Thiry First"};


  int m;
  int y;
  int d;

  cout << "Please enter a date starting with the month: " << endl;

  cin >> m;

  if(m < 1 || m > 12)
    {
      cout << "Invalid month entry!" << endl;
      cout << "Enter valid month: " << endl;
      cin >> m;
    }


  cout << "Please enter a date: " << endl;
  cout << "Month: " << endl;
  cin << m << endl;
  cout << "Day: " << endl;
  cin >> d; << endl;
  cout << "Year: " << endl;
  cin << y << endl;

  if( (y%4) == 0 && (y!= 100) || (y%400) == 0)
    days[1] = 29;
  else
    days[1] = 28;

  if(y < 2000 || y > 2090)
    cout << "Invalid year!" << endl; << "Please enter a valid year: " << endl;


  //if(d <= days[d])                                                                                                                                                                                            

  cout << month[m-1] << "," << todays[d-1] << y << endl;

  system("pause");

}


line 36, 40; you are using insertion operator (<<) instead of extraction (>>)
line 38 the extra << endl;
line 48 remove the extra ";" after first "endl"

This should take care of compilation errors.

For logical errors
You are reading month twice once lines 22-31 and then again lines 35-36
Then there is a message "Please enter valid year:" but no input of year again (here the code looks incomplete, and you may in process of adding the input, just wanted to note)

Putting if for validation like
1
2
3
4
5
6
7
8
cin >> m;

if(m < 1 || m > 12)
{
cout << "Invalid month entry!" << endl;
cout << "Enter valid month: " << endl;
cin >> m;
}


will validate the input only once use

1
2
3
4
5
6
7
8
cin >> m;

while(m < 1 || m > 12)
{
    cout << "Invalid month entry!" << endl;
    cout << "Enter valid month: " << endl;
    cin >> m;
}


instead
In addition to what codewalker has stated, I think it would be easier and cleaner to break your program down into functions (you can pass arrays to a function as argument). For example:

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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158


#include <iostream>
#include <fstream>
using namespace std;

/**
This function uses while loops to get the month, day, and year while checking to make
sure they are valid integers the returns them in an array
**/
int * getUserDate(int date[]) {
     int month=0;
     int day=0;
     int year=0;
     
     //input buffers
     int m=0;
     int d=0;
     int y=0;
     
     cout << "Please enter a numerical date starting with the month: \n";
     
     //get the month from the user
     while(month == 0) {
         cout << "Month: ";
         cin >> m;
         //check if the m input buffer for a valid integer
         if(!cin.fail()) {
                        //is the m input buffer in the range of 1 to 12?
                        if(m >= 1 && m <= 12) {
                             month=m;
                             date[0]=month;
                        }
                        //m input buffer is a valid integer but not in the range of 1 to 12
                        else {
                             cin.clear();
                             cin.ignore();
                             cout << "Invalid month entry!\n"; 
                        }
         }
         //month is not a valid integer
         else {
              cin.clear();
              cin.ignore();
              cout << "Invalid month entry!\n";
         }
     }
     
     //get the day from the user
     while(day == 0) {
         cout << "Day: ";
         cin >> d;
         //check if the d input buffer for a valid integer
         if(!cin.fail()) {
                        //is the d input buffer in the range of 1 to 31?
                        if(d >= 1 && d <= 31) {
                             //I have omitted logic check for odd and even months since I am not sure if you want to use an if statement to check this or write another function, I will let you choose.
                             day=d;
                             date[1]=day;                                  
                        }
                        //d input buffer is a valid integer but not in the range of 1 to 31
                        else {
                             cin.clear();
                             cin.ignore();
                             cout << "Invalid day entry!\n"; 
                        }
         }
         //day is not a valid integer
         else {
              cin.clear();
              cin.ignore();
              cout << "Invalid day entry!\n";
         }
     }
     
     //get the year from the user
     while(year == 0) {
         cout << "Year: ";
         cin >> y;
         //check if the y input buffer for a valid integer
         if(!cin.fail()) {
                        //is the y input buffer in the valid year range?
                        if(y >= 2000 && y <= 2090) {      
                             //Once again I am not sure if you want to use an if statement or write another function to check for leap year so I have omitted the logic checking for that. 
                             //You will have to decide that if the user did enter 29 on a non leap year if you want to consider that as invalid or assume user ignorance and fix it for them. 
                             year=y;
                             date[2]=year;
                        }
                        //y input buffer is a valid integer but not in the specified range
                        else {
                             cin.clear();
                             cin.ignore();
                             cout << "Invalid year entry!\n"; 
                        }
         }
         //day is not a valid integer
         else {
              cin.clear();
              cin.ignore();
              cout << "Invalid day entry!\n";
         }
     }  
     return date;    
}

/**
This function receives a date array and returns the date in a string literal format
**/
string formatUserDate(int date[]) {
       
       int m = date[0];
       int d = date[1];
       int y = date[2];
       
       string formattedDate = "";

       string months[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; 	
       string todays[] = {"First", "Second", "Third", "Fourth", "Fifth",  "Sixth", "Seventh", "Eighth", "Ninth", "Tenth", "Eleventh", "Twelveth", "Thirteenth", "Fourteenth", "Fifteenth", "Sixteenth", "Seventeenth", "Eighteenth", "Nineteenth", "Twentieth", "Twenty First", "Twenty Second", "Twenty Thirt", "Twenty Fourth", "Twenty Fifth", "Twenty Sixth", "Twenty Seventh", "Twenty Eighth", "Twenty Ninth", "Thirtieth", "Thiry First"};
       
       //set month to its literal value in the months array minus 1 since arrays always start at element 0
       formattedDate.append(months[m-1]);
       formattedDate.append(" ");
       
       //set day to its literal value in the months array minus 1 since arrays always start at element 0
       formattedDate.append(todays[d-1]);
       formattedDate.append(" ");
       
       /**
       Since you have not yet worked out your logic for the year, I don't want to give you the answer for this. For now  I just put in a generic statement.
       HINT: You could probably set up an array of prefixes/postfixes (i.e one through nine,teen,twenty,thirty,fourty instead of having to create a 99 element array like you did for the days
       **/
       formattedDate.append("Two Thousand and XX");
       
       return formattedDate;       
}


//the main function
int main(){
    
    //declare date array for [mm],[dd],[yyyy]
    int date[2];
    
    //declare string to hold the literal date that will be displayed back to the user
    string literalDate;
    
    //get date from the user
    getUserDate(date);
    
    //get literal date format
    literalDate = formatUserDate(date);
    
    //print literal date to the user
    cout << literalDate << "\n";
    
    system("pause");
}
Last edited on
Topic archived. No new replies allowed.