Jump to content

Arrays

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
hoku2000_99

hoku2000_99

    Newbie

  • Members
  • PipPip
  • 24 posts
I am working on this program that is suppose to display the number of days in a month.

The things I am suppose to do with this program is:
declare a 12-element, one-dimension int array named days.Assign the number of days in each month to the array. (Use 28 days for Feb.)

It is suppose to be coded so that it displays (on the screen) the number of days in the month corresponding to the number entered by the user. For example, when the user enters the number 1, the program should display 31 on the screen. The program should display an appropriate message when the user enters an invalid number.

When I compile it, I get no errors, its just when I run it, I dont get the right output. Can someone please lead into the right direction.

Thanks!




 //Ch11AppE02.cpp

//Displays the number of days in a month

//Created/revised by <your name> on <current date>


#include <iostream>


using std::cout;

using std::cin;

using std::endl;


int main()

{

    //declare variables and arrays

    int month = 0;

    int days[12]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

   

    //get user input

    cout << "Enter the month's number(1-12): " << endl;

    cin >> month;


    //displays error message for invalid input    

    cout << "Invalid input" << endl;


    //ask the user to enter data again

    cout << "Enter the month's number(1-12): " << endl;

    cin >> month;


    //accumulates the month

    for (int x = 0; x < 12; x= x + 1)

    //end for

   

    //display the days in that month

    cout << "That month has the following days: " << days[x] << endl;


    return 0;

}    //end of main function




My output that I get:

Enter the month's number(1-12):
1
Invalid input
Enter the month's number(1-12):
1
That month has the following days: 31
That month has the following days: 28
That month has the following days: 31
That month has the following days: 30
That month has the following days: 31
That month has the following days: 30
That month has the following days: 31
That month has the following days: 31
That month has the following days: 30
That month has the following days: 31
That month has the following days: 30
That month has the following days: 31
Press any key to continue . . .

#2
Phoenixz

Phoenixz

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 256 posts
You could use a case to get this done. e.g;

switch (month)
 {
  case 1:
          cout << There are << month[0] << days in that month;

Note: i'm not very good with cout nad don't quite understand how to use it properly, but if you understand that. ^^
Posted Image

#3
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
I'll add comments in bold to your code to explain what it is doing... perhaps that will help.

 //Ch11AppE02.cpp

//Displays the number of days in a month

//Created/revised by <your name> on <current date>


#include <iostream>


using std::cout;

using std::cin;

using std::endl;


int main()

{

    //declare variables and arrays

    int month = 0;

    int days[12]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

   

    //get user input

    cout << "Enter the month's number(1-12): " << endl;

    cin >> month;


    //displays error message for invalid input    

    cout << "Invalid input" << endl;  [B]//THIS ALWAYS RUNS!!!  You need a condition.[/B]


    //ask the user to enter data again

    cout << "Enter the month's number(1-12): " << endl;  [B]//THIS ALWAYS RUNS!!!  You need a condition.[/B]

    cin >> month;  [B]//THIS ALWAYS RUNS!!!  You need a condition.[/B]


    //accumulates the month

    for (int x = 0; x < 12; x= x + 1)  [B]//There is no need for a loop[/B]

    //end for

   

    //display the days in that month

    cout << "That month has the following days: " << days[x] << endl;  [B]// You probably want to output days[month-1] here[/B]

    [B]//this is the ACTUAL end of the for loop[/B]

    return 0;

}    //end of main function


Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog