how to declare a string that can vary

Hi there,
Im new to this and i really need help with c++
now here's teh question:
1: Write a C++ program that reads a month and a year and performs the following:
1. Find how many days are in a given month.
2. Check if the year is a leap year.

Note: Every year divisible by 4 is a leap year with 29 days in February.
Use the following specifications:
• Declare leapyear as a boolean and you should use it in your program.
• Use three letter abbreviations (i.e., Jan, Feb, Mar, etc) to indicate a given month.
• Use 4 digits to indicate a year (i.e., 2005, 1996).
• Use nested if statements

now i started a program but i thing im not declaring the month properly aka I dont know what data type i shoul use here's wat i began with:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int y,m;//y for year
bool leapyear;
char * month[] = { "jan", "feb",
"mar", "apr",
"may", "jun", "jul", "aug", "sep",
"oct", "nov",
"dec"};

cout<<"Enter a month: "<<endl;
cin>>month;

if(month=("jan"||"mar"||"may"||"jul"||"aug"||"oct"||"dec"))
cout<<"The no of days in"<<"jan"<<"is"<<"31"<<endl;

return 0;

}
ps-im aware its very amateur but as ive said im really new to this help wud be relly appreaciated

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
#include <iostream>
#include <string> //you include string, but you use char *'s??
using namespace std;
int main()
{
int y,m;//y for year
bool leapyear;
char * month[] = { "jan", "feb", //i would not bother with this...just declare a string to get the input
"mar", "apr",
"may", "jun", "jul", "aug", "sep",
"oct", "nov",
"dec"};

cout<<"Enter a month: "<<endl;
cin>>month; //change this to get a string

if(month=("jan"||"mar"||"may"||"jul"||"aug"||"oct"||"dec")) //you are probably meaing == instead =, but even so, the if is wrong...
//you have to do something like:
//if(month = "jan" || month = "mar" ...etc
//put in if statements or whatever
cout<<"The no of days in"<<"jan"<<"is"<<"31"<<endl; //What is this supposed to do...?

return 0;

}


Also, if you are new, I would suggest reading all of the tutorials on this site.
Last edited on
yea i tried reading the tutorials but this is due tomorrow and somehow i cudnt find wat i was lookin 4.
i had tried string in the beginning prbbly 4got
maybe im really complicating things i simply dont know how to declare a string that can vary
wen i declared it eg:

string month;//ok here wen i say month does it allocate 5 characters or wat ? cuz from wat ive read theyr tellin me it assigns only one character how can i assign 3 characters as the problem asks?


For line 21 I think ramorxy meant
cout<<"The no of days in "<< month <<" is "<< "31"<<endl;

when declaring a string it hasn't a fixed size
Last edited on
yeesss sorry for that i did mean month
This is an example:

1
2
3
4
5
string month;
month = "a";
month = "asdfh3i4hf";
month = "asd";
//month is now "asd" 
Ive done wat u've told me and it gives me the error:
error C2677: binary '||' : no global operator defined which takes type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or t
here is no acceptable conversion)
so we cant use relational operators with string??
Notice that '||' works for booleans, not for something else...
If you are using it like this: if ( month == ( "jan" || "mar" /*...*/ ) ), it does not make sense.
That line wold mean: if month is equal to ' "jan" or "mar" ', you want to say : if month is equal to "jan" of if it is equal to "mar"
which is if ( month == "jan" || month == "mar" )
If you want to know how operators work in C++ see: http://www.cplusplus.com/doc/tutorial/operators.html
Last edited on
#include <iostream>
#include <string>

using namespace std;

int main()
{
char *month31[] = {"Jan", "Mar", "May", "Jul", "Aug", "Oct", "Dec"};
char *month30[] = {"Apr", "Jun", "Sep", "Nov"};
string month;
int year;
bool leap;

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

cout << "Enter year : ";
cin >> year;

for ( int i = 0; i < 7; i++ ) {
if ( month == month31[i] ) {
cout << endl << " Month " << month31[i] << " has 31 days " << endl;
}
}

for ( int i = 0; i < 4; i++ ) {
if ( month == month30[i] ) {
cout << endl << " Month " << month30[i] << " has 30 days " << endl;
}
}

if ( year % 400 == 0 && year % 100 == 0 ) {
leap = true;
} else if ( year % 4 == 0 ) {
leap = true;
} else if ( year % 4 != 0 ) {
leap = false;
}

if ( leap == true ) {
if ( month == "Feb" ) {
cout << " Feb has 29 days " << endl;
}
cout << "Year is leap";
} else {
if ( month == "Feb" ) {
cout << " Feb has 28 days " << endl;
}
cout << "Year is not leap";
}

cout << endl;

return 0;
}





Topic archived. No new replies allowed.