Wrox Programmer Forums
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old March 20th, 2005, 07:10 AM
Registered User
 
Join Date: Mar 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default if / else problem...

As I am novice to C++, I would like to get help from the experts writing simple program for the following problem...

Write a C++ program that reads a month number as an integer (form 1 to 12) and a d ay number as an integer (form 1 to 31).
If the month entered is not between 1 and 12 inclusive, print a message informing the user that an invalid month has been entered. If the day entered is not between 1 and 31 inclusive, print a message informing the user that an invalid day has been entered.

If no error messages are displayed, print “Winter” if the month is in the range 1-3, “Spring” if the month is in the range 4-6, “Summer” if the month is in the range 7-9, and “Autumn” otherwise.


Thank you...

Reply With Quote
  #2 (permalink)  
Old March 22nd, 2005, 12:46 PM
Authorized User
 
Join Date: Oct 2004
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to ealkurabi
Default

honestly im new to C++ the first part of ur question sounds fun :) but the second part is kinda tricky a if then in a if then. The best kind of programmer should try his hardest to figure it out on his own its the best kid of accomplishment. only result to help with teeth gritting, have fun bro sorry i couldnt help im in school now and i dont have my book id help tho. good luck

Reply With Quote
  #3 (permalink)  
Old March 22nd, 2005, 11:53 PM
Authorized User
 
Join Date: Mar 2005
Posts: 58
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to Alan-LB Send a message via Yahoo to Alan-LB
Default

try this (part of program only) -
-----------------------------------------------------------------
int month;
int day;

std::cout << "Enter the Month" << std::endl;
std::cin >> month;

if ((month < 1) || (month > 12))
  std::cout << "Invalid Month entered" << std::endl;
else
  std::cout << "Month acceptable" << std::endl;

std::cout << "Enter the Day" << std::endl;
std::cin >> day;

if ((day < 1) || (day > 31))
  std::cout << "Invalid Day entered" << std::endl;
else
  std::cout << "Day acceptable" << std::endl;

switch (month)
{
  case 1:
  case 2:
  case 3:
    std::cout << "Winter" << std::endl;
    break;
  case 4:
  case 5:
  case 6:
    std::cout << "Spring" << std::endl;
    break;
  case 7:
  case 8:
  case 9:
    std::cout << "Summer" << std::endl;
    break;
  default:
    std::cout << "Winter" << std::endl;
    break;
------------------------------------------------------------
Go to your text books and look up IF and SWITCH in the index. Then read the
text and follow the examples. Then try to write your own code. This will help
you learn C++. Otherwise other people are doing all the work for you and you will not learn.

Alan




Reply With Quote
  #4 (permalink)  
Old April 28th, 2005, 04:04 AM
Registered User
 
Join Date: Apr 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I do agree we shouldn't just give you the code and let you copy and paste it, however I'll help you as best as I can.

I assume if the user inputs an invalid number, the program will prompt the user to input the number again, whether it be from the month or day. The problem with Alan-LB's code, is that the program will not exit or prompt the user for a valid input, if the user doesn't do so. For instance, if the user inputs 13, Alan's coding will simply go through the program output messages saying an invalid input has been made, then output winter as the default.

You would want a while loop that checks whether or not months were valid and re-prompt the user for another input, if invalid. Same goes for days. There's no need for the else statements. The switch case that Alan mentioned would be adequate, although it should be Autumn in the default, not Winter.
Reply With Quote
  #5 (permalink)  
Old May 1st, 2005, 01:31 AM
Authorized User
 
Join Date: Mar 2005
Posts: 58
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to Alan-LB Send a message via Yahoo to Alan-LB
Default

My code was merely an attempt to give a basic answer to the OP's posting. It was not intended to be a working program but only an illustration of the IF..ELSE and SWITCH commands. Like you I dont believe in writing complete programs for a student to copy blindly as his homework!!

You are right about Autumn as the default

Alan

Reply With Quote
  #6 (permalink)  
Old May 17th, 2005, 05:05 AM
Registered User
 
Join Date: May 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to vigorous365 Send a message via MSN to vigorous365
Default

 I am very glad to see the all posts. I do agree with Alan. As a novice, you should try your best to write you own code.

Adventurer
Reply With Quote
  #7 (permalink)  
Old May 18th, 2005, 05:23 AM
Authorized User
 
Join Date: May 2005
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

you should check whether the number is between 1 &12 for the month.
if yes,only then go for entering and checking the day between 1 and 31;else ask him to enter between 1 and 12.
after that check for the range in which month falls and print the message.

Reply With Quote
  #8 (permalink)  
Old May 22nd, 2005, 01:43 AM
Authorized User
 
Join Date: Mar 2005
Posts: 58
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to Alan-LB Send a message via Yahoo to Alan-LB
Default

Rashmid,

Please take the time to read the code that I have posted - if you read it you will see that I do check the values for the month and day.

The code I posted was not intended to be a complete working program but as I said in my post "part of program only". I was giving an example of a basic "if/else problem" which the OP, who describes himself as a "novice to C++", had originally requested.

There is often little benefit in posting complete working programs in answer to simple basic questions.

Alan


Reply With Quote









Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.