I need help with my code!!!

March 1, 2010 at 13:33:43
Specs: Windows XP
#include <iostream.h>
int main () {
char month;

cout<< "Enter month ";
cin>> month;

if(month == "september" || month == "april" || month == "june" ||
month == "november")
cout<< "The month has 30 days. " << endl;
else
cout<< "The month doesn't have 30 days. " <<end1;

if(month == "february")
cout<< "The month has 28 days. " << endl;
else
cout<< "All other months have 31 days. "<<endl;

return 0;
}


See More: I need help with my code!!!

Report •

#1
March 1, 2010 at 15:50:32
Okay, now pretend I'm not standing over your shoulder, and I don't know what your error message is.

Report •

#2
March 1, 2010 at 16:20:07
A one char month?

Report •

#3
March 1, 2010 at 20:20:17
you've got two problems.

1) char month allocates 1 byte of storage space, so anything beyond one character isn't going to fit. if you wish to use month as a string, then allocate more space.

char month[12] = { 0 };

2) with the char data type, you can't compare two strings using == use strcmp instead.

if(strcmp( month, "may") == 0) { /* month equals "may" */ }

note: strcmp is case sensitive, so "MAY" will not test as "may." if case is important, convert your string to either all uppercase or lowercase letters first.

#include <ctype.h>
for(int i=0; (month[i] = toupper(month[i])) != '\0'; i++){}

if(strcmp(month, "MAY") == 0) { /* month equals "MAY" */ }

if you're using c++, you might consider using the string class since it will do what you're trying to do as your code is now.

#include <string.h>
or with namespace std
#include <string>

string month;

if(month == "may") { ... }

HTH


Report •
Related Solutions


Ask Question