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

//a C++ program that accepts a day and month from the user (2005 as the year), checks that they are valid, 
//displays the date entered in MM/DD/CCYY format and displays the date

#include "stdafx.h"
using namespace std;


class dateReport

{

    private:

           int day;

           int month;

           int year;

    public:

			 dateReport(); // constructor

     bool    CheckDate(int day1, int month1);

};
bool CheckDate( int day1, int month1)
{
	int day1, month1 = 0;


		if (month1 > 12 || month1 < 1)
			{
				cout << "Enter A  Valid Month" << endl;
				return false;
			}
// Supposed to check whether or not the day entered is valid.
//Commented out due to it causing errors

if (month1 == 1 || month1 == 3 || month1 == 5 || month1 ==7 || month1 == 8 || month1 ==10 || month1 ==12 && day1 > 31
			|| day1 < 1)
			{
				cout << "Enter A Valid day" << endl;
	
				return false;
			}
/*else if (month1 == 4 month1 == 6 || month1 == 9 || month1 == 11 && day1 < 1  || day1 > 30)
			{
				cout << "Enter A Valid Day" << endl;
				return false;
			}*/
else
			{
				return true;
			}
}




	// MAIN
int _tmain(int argc, _TCHAR* argv[])
{
	int day1, month1;
	CheckDate(day1, month1);
    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;
}

Dani AI

Generated

This thread already contains the key clues: the program calls the checker with uninitialized variables, the checker redeclares its parameters, and the checker is defined as a free function rather than a class method. correctly spotted the redeclaration/scope problems, and both and were right to point at constructors and member initialization. The concrete fixes are small and will remove the compile/runtime errors.

Keep parameter names only in the signature (do not redeclare them inside the function) and define the function as a member of the class with the scope operator. Use a simple month→max-days table to validate the day, and handle month checks before day checks so logic stays clear. For portability avoid relying on Visual Studio precompiled headers (stdafx.h) unless required.

Example of a compact, original pattern (member method + input loop):

bool dateReport::isValid(int d, int m) const {
    if (m < 1 || m > 12) return false;
    static const int daysInMonth[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
    int maxDay = daysInMonth[m];
    // add leap-year logic here if year becomes variable
    return d >= 1 && d <= maxDay;
}

dateReport report;    // stack object; alternative: dateReport* p = new dateReport();
int day, month;
do {
    cout << "Enter month: "; cin >> month;
    cout << "Enter day: ";   cin >> day;
} while (!report.isValid(day, month));
report.setDate(day, month);

Troubleshooting checklist: compile with warnings enabled (-Wall or /W4), move return outside input loops (OP had an early return), avoid name collisions by using member-name conventions (mday or day), and add parentheses in complex boolean expressions to avoid precedence bugs. With those fixes the class design suggested by will work cleanly and the immediate "used without being defined" error will disappear.

Recommended Answers

All 4 Replies

This is just a quick guess, but you might want to use a constructor...

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.

...
bool CheckDate( int day1, int month1)
{
	int day1, month1 = 0;
...

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...

...
bool dateReport::CheckDate( int day1, int month1)
{
	int day1, month1 = 0;
...

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

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

dateReport::dateReport() // notice the parameters are empty
{
   //  setting all members of the object to 0
   day = 0;
   month = 0;
   year = 2005; // given from requirements
}

in your main, you need to create the object of the class (instantiate it)

int _tmain(int argc, _TCHAR* argv[])
{
	int day1, month1;
            [B]dateReport myObject = new dateReport();[/B]
	//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

void dateReport::setDate(int day, int year)
{
    //set your data members
}

void dateReport::print()
{
    //print the formatted date
}

that should get you started (dont forget to include the sigs for these functions in yer class

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.