Validating user input with const. array

I have a programming assignment that I can't seem to figure out. The program requires us to prompt the user to enter a month, day and year in the driver application and create an instance with those values. If invalid, the instance should be created with the default values 1,1 and 2001.
I know that I would need a for loop to go through the user input when they enter a month (which would be a string), to compare it to the constant array of month's I already have.

Any help would be much appreciated!
Last edited on
Is this what you are asking for?

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.h>
#include <conio.h>
#include<stdio.h>
void main()
{
clrscr();
int i,j,mm; char month[3], conststring[12][3]={'J','a','n',
					       'F','e','b',
					       'M','a','r',
					       'A','p','r',
					       'M','a','y',
					       'J','u','n',
					       'J','u','l',
					       'A','u','g',
					       'S','e','p',
					       'O','c','t',
					       'N','o','v',
					       'D','e','c'};
/*You need not to make array of full names of month.
Let the user enter January but the program reads only Jan and gives 1*/
cout<<"Enter a month: "; gets(month);
for(mm=0;mm<12;mm++)
{ for(i=0,j=0;j<3;j++)
  if(month[j]==conststring[mm][j])  i++;
  if(i==3) {mm++; break;}
}
cout<<"The month you have entered is: "<<mm;
getch();
}
Last edited on
something along those lines. the code that is given to me is

enum DateFormat {mdy1, mdy2, ymd1, ymd2};
const int MIN_YEAR = 1900;
const int MAX_YEAR = 2020;
const string monthStr[ ] = //alternative: const char monthStr[] [15]=
{"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November",
"December"};

const string monthStrAbbrev [ ] = //not strictly necessary, but helpful
{"jan", "feb", "mar", "apr", "may", "jun",
"jul", "aug", "sep", "oct", "nov",
"dec"};

const int monthDays [] =
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

I kind of understood your code but am still a little confused as to what exactly is going on in your for loop.
1
2
3
4
5
for(mm=0;mm<12;mm++)
{ for(i=0,j=0;j<3;j++)
  if(month[j]==conststring[mm][j])  i++;
  if(i==3) {mm++; break;}
}



conststring[mm][j]      month[j]
┌────┬────┬────┐      ┌───┬───┬───┐  
│ 0,0│ 0,1│ 0,2│      │ 012 │
├────┼────┼────┤      └───┴───┴───┘ 
│ 1,0│ 1,1│ 1,2│
├────┼────┼────┤
│ 2,0│ 2,1│ 2,2│
├────┼────┼────┤
│ 3,0│ 3,1│ 3,2│
├────┼────┼────┤
│ 4,0│ 4,1│ 4,2│
├────┼────┼────┤
│ 5,0│ 5,1│ 5,2│
├────┼────┼────┤
│ 6,0│ 6,1│ 6,2│
├────┼────┼────┤
│ 7,0│ 7,1│ 7,2│
├────┼────┼────┤
│ 8,0│ 8,1│ 8,2│
├────┼────┼────┤
│ 9,0│ 9,1│ 9,2│
├────┼────┼────┤
│10,0│10,1│10,2│
├────┼────┼────┤
│11,0│11,1│11,2│
├────┼────┼────┤
│12,0│12,1│12,2│
└────┴────┴────┘

So for each value of mm the value of j iterates from 0 to 2.

Hence for one value of mm, the loop checks if the value of month[j] is equal to conststring[mm][j] or not, three times.

That is, the entered month is checked along the constant string twelve times and whenever a month matches the entered value the loop termintes(for(mm=0;mm<12;mm++), this loop)


I hope this was helpful ......... in case of any confusion, feel free to ask it:)
Last edited on
Topic archived. No new replies allowed.