| | |
Basic Programming help
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Aug 2005
Posts: 2
Reputation:
Solved Threads: 0
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
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)
//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; }
•
•
Join Date: Aug 2005
Posts: 80
Reputation:
Solved Threads: 2
C++ Syntax (Toggle Plain Text)
... 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...
C++ Syntax (Toggle Plain Text)
... 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
•
•
Join Date: Aug 2005
Posts: 14
Reputation:
Solved Threads: 3
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
in your main, you need to create the object of the class (instantiate it)
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
that should get you started (dont forget to include the sigs for these functions in yer class
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)
dateReport::dateReport() // notice the parameters are empty { // setting all members of the object to 0 day = 0; month = 0; year = 2005; // given from requirements }
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;
} C++ Syntax (Toggle Plain Text)
void dateReport::setDate(int day, int year) { //set your data members } void dateReport::print() { //print the formatted date }
![]() |
Similar Threads
- Where can I get E-Book for Visual Basic (Visual Basic 4 / 5 / 6)
- VB Video Game Programming (Visual Basic 4 / 5 / 6)
- Opinions? javascript/php/etc and programming standards (JavaScript / DHTML / AJAX)
- Visual basic Help please! (Visual Basic 4 / 5 / 6)
- Who willing to teach Programming? (C++)
- OOPic Microcontroller - Assembly Language Programming (Assembly)
- Ti-basic (DaniWeb Community Feedback)
- Basic virtual reality questions (Game Development)
- Visual basic programming (Visual Basic 4 / 5 / 6)
Other Threads in the C++ Forum
- Previous Thread: I am very new to C++ and don't know what i am doing wrong
- Next Thread: PLS help me do this program..
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets





