English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

You are to write a C program that will prompt the user to enter the current month as a string in lowercase (like "september"). Then display the number of days in the month that they specified, knowing September, April, June, and November have 30 days, February has 28, and all the others have 31. If the user doesn't enter a valid month name, display an appropriate error message.




How would I start on this program?
How would you declare all the variables?

2007-02-27 17:02:16 · 4 answers · asked by Anonymous in Computers & Internet Programming & Design

4 answers

include the appropriate libraries.

make the months themselves variables (predefined)
Month1=Jan Month1d=31
then ask user to input month. Jan
how many days? if Janday == 31
cout>> Invalid Number of days

2007-02-27 17:19:00 · answer #1 · answered by nothin_nyce1 4 · 0 0

To write a program and excute it, you need not to work hard.
but what is your actaul intension to do this is important.
If you want to explore the features of C++, then you should go through the Object oriented Programming approach.
Simply you want to run then you can do it in procedural programming also.
I hope you are going for OOP concept.

Create a class say daysinmonth
in that create methods like

getinput
validateinput
displayoutput
reporterror
take one global variable called u_input;
char u_input[80];


getinput will just take the input from user

validateinput will check whether input is correct or not

displayoutput will display the output

and reporterror will display the error message.

Class daysinmonth
{
char u_input[80];
int days[12]={31,28,31,30,31,30,31,31,30,31,30,31};
char months[12][20]={"January",february","march","april","may","June","July","august","september","october","november","december"};
int month=-1;

void getinput()
{
cin< }

void validateinput()
{
int i;
for(i=0;i<12;i++)
{
if(strcmp(u_input,months[i])==0)
{
month=i;
break;
}
}

if(month=-1)
reporterror();
else
displayoutput();
}

reporterror()
{
//errormessage
}
void displayoutput()
{
cout>>"in ">>u_input>>" Month ">>days[month]>>" are there";
}

}


main()
{
daysinmonth dim;

dim.getinput();
dim.validateinput();

}

***********

2007-02-28 02:30:25 · answer #2 · answered by PC 4 · 0 1

//You need a variable for input:
char inputmonth[20];

//Then you need to do the input. However you do input is fine, scanf, gets, whatever floats your boat.

//Then you do a a case statement or a series of error messages. The default case will print the appropriate error message. IF you do the if statements, then use another variable as a flag for valid/invalid month.
// If (strcmp(inputmonth,"january")==0) printf("31\n");

2007-02-28 01:08:47 · answer #3 · answered by Vegan 7 · 1 1

Q1: Do you want the program in C++ or C?
Q2: Are you allowed to use arrays and structs?
Q3: Are you allowed to use functions?

Thanks!!


OK, assuming you need it in C++ and your answer is yes to the questions above, then the program should look like this:

// NOTE: I'm using dashes "--------" in the program below just
// to leave space and organize the program. So, don't include the
// dashes in your actual program

#include
#include
using namespace std;

struct Month
{
----- string name;
----- int days;
};

// The following line declears a function. It has to be in this location.
// The code for the function should be after main. You will see it
// below.

int searchForMonth(Month array[ ], string aMonth);

int main( )
{
------- string userInput = "";
------- int index = -1;
------- // You will know why I have the previous two variables later

------ // We have to create an array of months and initialize it:
------ Month array[12];
------ array[0].name = "january";
------ array[0].days = 31;
------ array[1].name = "febraury";
------ array[1].days = 28;
------ // Keep doing the previous to all the months of the year until
------ // you reach december which you have to do as follows:
------ array[11].name = "december";
------ array[11].days = 31;

------ // Now you are done storing the values and you can start
------ // asking the user for input as follows:

------ do
------ {
------------- cout << "Enter the name of the month or exit to exit: ";
------------- cin >> userInput;
------------- if (userInput != "exit")
------------- {
--------------------- index = searchForMonth(array, userInput);
--------------------- if (index == -1)
--------------------- {
--------------------------- cout << "Your input is invalid, try again!!";
--------------------------- cout << endl;
--------------------- }
--------------------- else
--------------------- {
--------------------------- cout << "The number of days is: "
--------------------------------- << array[index].days;
--------------------- }
------------- }
------- } while(userInput != "exit");

----- return 0;
}


// Now we need to write the code for the function "searchForMonth"

int searchForMonth(Month array[ ], string aMonth)
{
-------- for(int i = 0; i < 12; i++)
-------- {
-------------- if (array[ i ].name == aMonth)
-------------- {
-------------------- return i;
-------------- }
--------- }
// If you reach here, that means you couldn't find the month
// in the array. So return -1 to indicate the month was not found.

------ return -1;
}


I hope this helps!!

2007-02-28 01:31:49 · answer #4 · answered by Silver_Sword 3 · 1 1

fedest.com, questions and answers