Date format conversion

I need help with my assignment. This program is suppose to take in the date
mm dd yyyy and convert it a date such as March 3rd, 1999 while using switch case statements. I get a whole bunch of run-time check failures saying that my variables are being used without being initialized. Am I doing this totally wrong? I could use any suggestions. Thanks

1. Have user input date in numeric format mm dd yyyy.
2. Open "answer.txt" file for output.
. Use swicth statement for months
. Example: (month) case 1: if (day > 1 && day < 31) outAnswer << "January"

*/

#include <iostream>
#include <fstream>


using namespace std;

int main()
{
ofstream outAnswer; // Output to a file
outAnswer.open("answer1.doc"); // Saves output to txt file
cout << "Enter date in numerical format separated by spaces:";



// month calculations



int month, day, year; // integer variables

char month1, day1; //output variables

cin >> month, day, year; // user inputs numeric date format


switch(month)
{
case 1: if (day <=31)
outAnswer << month1 << "January";

else outAnswer << month1 << "invalid # january days";
break;
case 2: if ((day >= 1) && (day <=28)) {
outAnswer << month1 << "Febuary";
}
else outAnswer << month1 << "invalid # Febuary days";
break;
case 3: if ((day >= 1) && (day <=31)) {
outAnswer << month1 << "March";
}
else outAnswer << month1 << "invalid # March days";
break;
case 4: if ((day >= 1) && (day <= 30)) {
outAnswer << month1 << "April";
}
else outAnswer << month1 << "invalid # April days";
break;
case 5: if ((day >= 1) && (day <= 31)) {
outAnswer << month1 << "May";
}
else outAnswer << month1 << "invalid # may days";
break;
case 6: if ((day >= 1) && (day <=30)) {
outAnswer << month1 << "June";
}
else outAnswer << month1 << "invalid # of June days";
break;
case 7: if ((day >= 1) && (day <= 31)) {
outAnswer << month1 << "July";
}
else outAnswer << month1 << "invalid # of july days";
break;
case 8: if ((day >=1) && (day <= 31)) {
outAnswer << month1 << "August";
}
else outAnswer << month1 << "invalid # of August days";
break;
case 9: if ((day >= 1) && (day <= 30)) {
outAnswer << month1 << "September";
}
else outAnswer << month1 << "invalid # of September days";
break;
case 10: if ((day >=1) && (day <= 31)) {
outAnswer << month1 << "October";
}
else outAnswer << month1 << "invalid # of October days";
break;
case 11: if (( day >=1) && (day <= 30)) {
outAnswer << month1 << "November";
}
else outAnswer << month1 << "invalid # of November days";
break;
case 12: if (( day >=1) && (day <=31)) {
outAnswer << month1 << "December";
}
else outAnswer << month1 << "invalid # of December days";
break;
default : outAnswer << month1 << "invalid Month #";
break;
}

if (day == 1 || day == 21 || day ==31)
outAnswer << day1 << "st,";
else if (day == 2 || day == 22)
outAnswer << day1 << "nd,";
else if (day == 3 || day == 23)
outAnswer << day1 << "rd,";
else
outAnswer << day1 << "th,";

outAnswer << month1 << day << day1 << year << endl;





return 0;
}
Last edited on
I have been trying to figure out what you are doing here exactly.
this mess here I would add an incompassing if around the code below on a good file open
1
2
3
4
5
6
7
8
9
10
11
12
ofstream outAnswer;	 // Output to a file	
outAnswer.open("answer1.doc");	// Saves output to txt file

if{outAnswer.good())
{
      //  my other code
      outAnswer.close(); // close the file....
}
else
{
      cout << "I had a problem open the output file!!" << endl;
}


other issues I see go around this:
1
2
3
4
5
6
7
8

switch(month)
{
case 1: if (day <=31)	
            outAnswer << month1 << "January";

            else outAnswer << month1 << "invalid # january days";
            break;


From what I see of what you are trying to do:
1
2
3
4
5
6
7
8
9
10
11
case 1: if(day <=31)
            {
                   outAnswer << "January "; // don't forget the trailing space on January
            }
            else
            {
                   // I would set up a boolean like "bool goodDate = true;" near the other variables.
                   goodDate = false;
                   cout << "Number of days for January was invalid" << endl;
            }
            break;


A little further down where:
1
2
3
4
5
6
7
8
if (day == 1 || day == 21 || day ==31)
     outAnswer << day1 << "st,";
else if (day == 2 || day == 22)
     outAnswer << day1 << "nd,";
else if (day == 3 || day == 23)
     outAnswer << day1 << "rd,";
else
     outAnswer << day1 << "th,";


my version would look like:
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
if(goodDate == true)
{
      outAnswer << day;
      switch(day)
      {
              case 1:
              case 21:
              case 31:
                      outAnswer << "st";
                      break;
              case 2:
              case 22:
                      outAnswer << "nd";
                      break;
              case 3:
              case 23:
                      outAnswer << "rd";
                      break
              default:
                      outAnswer << "th";
        } // switch(day)
        
        outAnswer << ", " << year << endl;
} // end goodDate

// when I get here I should have the correct conversion in the file.
// don't forget to close the file. when your done in it. 



I hope the code pieces point you in the right direction.
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

#include <iostream>
#include <fstream>
#include <cmath>


using namespace std;

int main()
{
ofstream outAnswer; // Output to a file
outAnswer.open("answer1.txt"); // Saves output to txt file
cout << "Enter date in numerical format separated by spaces:";



// month calculations



int month, day, year, year1; // integer variables

cin >> month >> day >> year; // user inputs numeric date format



switch(month)
{
case 1: if ((day <=0) || ( day <=32))
outAnswer << "January ";

else cout << "invalid # january days";
break;
case 2: if ((day >= 0) || (day <=29)) {
outAnswer << "Febuary ";
}
else cout << "invalid # Febuary days";
break;
case 3: if ((day >= 0) || (day <=32)) {
outAnswer << "March ";
}
else cout << "invalid # March days";
break;
case 4: if ((day >= 0) || (day <= 31)) {
outAnswer << "April ";
}
else cout << "invalid # April days";
break;
case 5: if ((day >= 0) || (day <= 32)) {
outAnswer << "May ";
}
else cout << "invalid # may days";
break;
case 6: if ((day >= 0) || (day <=31)) {
outAnswer << "June ";
}
else cout << "invalid # of June days";
break;
case 7: if ((day >= 0) || (day <= 32)) {
outAnswer << "July ";
}
else cout << "invalid # of july days";
break;

case 8: if ((day >=0) || (day <= 32)) {
outAnswer << "August ";
}
else cout << "invalid # of August days";
break;

case 9: if ((day >= 0) || (day <= 31)) {
outAnswer << "September ";
}
else cout << "invalid # of September days";
break;

case 10: if ((day >=0) || (day <= 32)) {
outAnswer << "October ";
}
else cout << "invalid # of October days";
break;

case 11: if (( day >=0) || (day <= 31)) {
outAnswer << "November ";
}
else cout << "invalid # of November days";
break;

case 12: 
	
if (( day >=0) || (day <=32)) {
outAnswer << "December ";
}
else cout << "invalid # of December days";
break;

default : cout << "invalid Month #";
break;
}

if (day == 1 || day == 21 || day ==31)
outAnswer << day << "st, ";
else if (day == 2 || day == 22)
outAnswer << day << "nd, ";
else if (day == 3 || day == 23)
outAnswer << day << "rd, ";
else
outAnswer << day << "th, ";
{
if (year <= 50)
	year1 = 2000 + year;
else if (year >= 50 || year <= 99)
	year1 = 1900 + year;
else
	cout << "Invalid Year #" << endl;
}

outAnswer << year1 << endl;
return 0;
} 

There you go bud, see you in class. Only minor errors ill explain them to you.
Thanks Azagaros and Pbdemon101 for your suggestions. I only had enough time to try Pbdemon's way, but I still got those run time check failures that said variable day and year were uninitialized. Output gave the correct month but everething else was a bunch of nonsense numbers: April-858993460th,-858991460.
// Robert Alvarado
//Engr 21 Monday 6:30-9:40
//Assignment 3
// March 21, 2011
// Purpsoe: have user imput date in numeric form mm dd yy and have program output to month day year format.

/* Pseudocode
1. Have user input date in numeric format mm dd yy.
2. Open "answer.txt" file for output.
3. Use swicth statement for months
4. Example: (month) case 1: if (day > 1 && day < 31) outAnswer << "January"

*/

#include <iostream>
#include <fstream>
#include <cmath>

using namespace std;

int main()
{
ofstream outAnswer; // Output to a file
outAnswer.open("answer1.txt"); // Saves output to txt file
cout << "Enter date in numerical format separated by spaces:";



// month calculations



int month, day, year, year1; // integer variables

cin >> month, day, year; // user inputs numeric date format


switch(month)
{
case 1: if ((day >=0) || (day <=32))
outAnswer << "January";

else cout << "invalid # january days";
break;
case 2: if ((day >= 0) || (day <=29)) {
outAnswer << "Febuary";
}
else cout << "invalid # Febuary days";
break;
case 3: if ((day >= 0) || (day <=32)) {
outAnswer << "March";
}
else cout << "invalid # March days";
break;
case 4: if ((day >= 0) || (day <= 31)) {
outAnswer << "April";
}
else cout << "invalid # April days";
break;
case 5: if ((day >= 0) || (day <= 32)) {
outAnswer << "May";
}
else cout << "invalid # may days";
break;
case 6: if ((day >= 0) || (day <=31)) {
outAnswer << "June";
}
else cout << "invalid # of June days";
break;
case 7: if ((day >= 0) || (day <= 32)) {
outAnswer << "July";
}
else cout << "invalid # of july days";
break;
case 8: if ((day >=0) || (day <= 32)) {
outAnswer << "August";
}
else cout << "invalid # of August days";
break;
case 9: if ((day >= 0) || (day <= 31)) {
outAnswer << "September";
}
else cout << "invalid # of September days";
break;
case 10: if ((day >=0) || (day <= 32)) {
outAnswer << "October";
}
else cout << "invalid # of October days";
break;
case 11: if (( day >=0) || (day <= 31)) {
outAnswer << "November";
}
else cout << "invalid # of November days";
break;
case 12: if (( day >=1) || (day <=32)) {
outAnswer << "December";
}
else cout << "invalid # of December days";
break;
default : cout << "invalid Month #";
break;
}

if (day == 1 || day == 21 || day ==31)
outAnswer << day << "st,";
else if (day == 2 || day == 22)
outAnswer << day << "nd,";
else if (day == 3 || day == 23)
outAnswer << day << "rd,";
else
outAnswer << day << "th,";
{
if (year <= 50)
year1 = 2000 + year;
else if (year >= 50 || year <=99)
year1 = 1900 + year;
else
cout << "Invalid year #" << endl;
}

outAnswer << year1 << endl;


return 0;
}
Last edited on
I changed your .doc file to a .txt file. Make sure you are looking in NotePad file for the document not word.
I did that already without noticing your change because I hated waiting for Microsoft Word to open.
Its working on my computer =/
Topic archived. No new replies allowed.