Basic Programming help

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2005
Posts: 2
Reputation: Continuous is an unknown quantity at this point 
Solved Threads: 0
Continuous Continuous is offline Offline
Newbie Poster

Basic Programming help

 
0
  #1
Aug 29th, 2005
I am taking an intermediate programming class, after a somewhat long hiatus. One of the first projects is a somewhat simple assignment that takes a date from the user, and checks to see if it is valid, using classes. I have been working on it for a while, and i seem to be making no progress lately. My current problem is that as soon as i start the program, the program halts, saying that "variable month1 is being used without being defined". I cannot figure out what my problem is, but i suspect that it is something simple. I understand how to create the program, i just get tripped up on the syntax and the implementation of it. Any help is appreciated


Program, the commented out portions are things i am having trouble with, and that i have removed for testing purposes

  1. //a C++ program that accepts a day and month from the user (2005 as the year), checks that they are valid,
  2. //displays the date entered in MM/DD/CCYY format and displays the date
  3.  
  4. #include "stdafx.h"
  5. using namespace std;
  6.  
  7.  
  8. class dateReport
  9.  
  10. {
  11.  
  12. private:
  13.  
  14. int day;
  15.  
  16. int month;
  17.  
  18. int year;
  19.  
  20. public:
  21.  
  22. dateReport(); // constructor
  23.  
  24. bool CheckDate(int day1, int month1);
  25.  
  26. };
  27. bool CheckDate( int day1, int month1)
  28. {
  29. int day1, month1 = 0;
  30.  
  31.  
  32. if (month1 > 12 || month1 < 1)
  33. {
  34. cout << "Enter A Valid Month" << endl;
  35. return false;
  36. }
  37. // Supposed to check whether or not the day entered is valid.
  38. //Commented out due to it causing errors
  39.  
  40. if (month1 == 1 || month1 == 3 || month1 == 5 || month1 ==7 || month1 == 8 || month1 ==10 || month1 ==12 && day1 > 31
  41. || day1 < 1)
  42. {
  43. cout << "Enter A Valid day" << endl;
  44.  
  45. return false;
  46. }
  47. /*else if (month1 == 4 month1 == 6 || month1 == 9 || month1 == 11 && day1 < 1 || day1 > 30)
  48. {
  49. cout << "Enter A Valid Day" << endl;
  50. return false;
  51. }*/
  52. else
  53. {
  54. return true;
  55. }
  56. }
  57.  
  58.  
  59.  
  60.  
  61. // MAIN
  62. int _tmain(int argc, _TCHAR* argv[])
  63. {
  64. int day1, month1;
  65. CheckDate(day1, month1);
  66. while(CheckDate(day1, month1)) // loops until valid input is detected
  67. {
  68.  
  69. cout << "Enter A Day" << endl;
  70. cin >> day1;
  71.  
  72. cout << "Enter A Month" << endl;
  73. cin >> month1;
  74.  
  75.  
  76. return 0;
  77. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Solved Threads: 5
Drowzee Drowzee is offline Offline
Posting Whiz in Training

Re: Basic Programming help

 
0
  #2
Aug 29th, 2005
This is just a quick guess, but you might want to use a constructor...
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 2
Reputation: Continuous is an unknown quantity at this point 
Solved Threads: 0
Continuous Continuous is offline Offline
Newbie Poster

Re: Basic Programming help

 
0
  #3
Aug 29th, 2005
Originally Posted by Drowzee
This is just a quick guess, but you might want to use a constructor...
oh, well i haven't even started to use that yet. I just included the class because it is going to be part of the final program.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 80
Reputation: Daishi is an unknown quantity at this point 
Solved Threads: 2
Daishi Daishi is offline Offline
Junior Poster in Training

Re: Basic Programming help

 
0
  #4
Aug 29th, 2005
  1. ...
  2. bool CheckDate( int day1, int month1)
  3. {
  4. int day1, month1 = 0;
  5. ...

Two things I see wrong there. First, if you want CheckDate to be a method of class dateReport then you need to add dateReport:: before the method name, like...

  1. ...
  2. bool dateReport::CheckDate( int day1, int month1)
  3. {
  4. int day1, month1 = 0;
  5. ...

The next thing is that you're declaring day1, and month1 locally in that method, but you have day1 and month1 as arguments already. I haven't read into your method to know the intent of those two variables but I have a feeling that you didn't want to make them have the same name as day1, and month1.. So you should probably change their names, or get rid of them.

Hope that helps a bit..

-Fredric
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 14
Reputation: nattylife is an unknown quantity at this point 
Solved Threads: 3
nattylife nattylife is offline Offline
Newbie Poster

Re: Basic Programming help

 
0
  #5
Aug 30th, 2005
while defining the class is on the right track, you still need to create an object of that class (an instance of class dateReport)
your constructor for your class should initialize all data members of your class so that the object is ready for work.
so in your constructor (and like Daishi said, when you define the methods of your class, you need to use the scope resolution operator ( :: ) to define the methods for the compiler
  1. dateReport::dateReport() // notice the parameters are empty
  2. {
  3. // setting all members of the object to 0
  4. day = 0;
  5. month = 0;
  6. year = 2005; // given from requirements
  7. }
in your main, you need to create the object of the class (instantiate it)
int _tmain(int argc, _TCHAR* argv[])
{
	int day1, month1;
            dateReport myObject = new dateReport();
	//CheckDate(day1, month1);   your day1, month1 are not initialized to anything
            // so if you call the method with these 2 variables, its garbage
            // same below, try looking up a do-while statement for below
            //while(CheckDate(day1, month1)) // loops until valid input is detected 
	{

		cout << "Enter A Day" << endl;
		cin >> day1;

		cout << "Enter A Month" << endl;
		cin >> month1;


	return 0;
}
and to format your data, youll prolly need a method to set the data members to = the input and then have another function to print the output
  1. void dateReport::setDate(int day, int year)
  2. {
  3. //set your data members
  4. }
  5.  
  6. void dateReport::print()
  7. {
  8. //print the formatted date
  9. }
that should get you started (dont forget to include the sigs for these functions in yer class
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC