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;
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,";
elseif (day == 2 || day == 22)
outAnswer << day1 << "nd,";
elseif (day == 3 || day == 23)
outAnswer << day1 << "rd,";
else
outAnswer << day1 << "th,";
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";
breakdefault:
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.
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"
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;
}