943,928 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1586
  • C++ RSS
Aug 29th, 2005
0

Basic Programming help

Expand Post »
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

C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Continuous is offline Offline
2 posts
since Aug 2005
Aug 29th, 2005
0

Re: Basic Programming help

This is just a quick guess, but you might want to use a constructor...
Reputation Points: 22
Solved Threads: 5
Posting Whiz in Training
Drowzee is offline Offline
244 posts
since Jul 2005
Aug 29th, 2005
0

Re: Basic Programming help

Quote 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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Continuous is offline Offline
2 posts
since Aug 2005
Aug 29th, 2005
0

Re: Basic Programming help

C++ Syntax (Toggle Plain Text)
  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...

C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
Daishi is offline Offline
80 posts
since Aug 2005
Aug 30th, 2005
0

Re: Basic Programming help

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
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 3
Newbie Poster
nattylife is offline Offline
14 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: I am very new to C++ and don't know what i am doing wrong
Next Thread in C++ Forum Timeline: PLS help me do this program..





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC