Help with functions and cin

This is a discussion on Help with functions and cin within the C++ Programming forums, part of the General Programming Boards category; Hey all i am having 2 problems. - first problem is that i am unable to run the code with ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    16

    Question Help with functions and cin

    Hey all i am having 2 problems.
    - first problem is that i am unable to run the code with the function inside the switch. For some reason it keeps giving me these errors:
    f:\c++2\addressClass\addressClass.cpp(135): error C2050: switch expression not integral
    f:\c++2\addressClass\addressClass.cpp(133): error C2065: 'repeat' : undeclared identifier
    f:\c++2\addressClass\addressClass.cpp(132): error C2065: 'theUserInput' : undeclared identifier
    f:\c++2\addressClass\addressClass.cpp(121): error C2365: 'main' : redefinition; previous definition was a 'formerly unknown identifier'
    f:\c++2\addressClass\addressClass.cpp(132): error C2593: 'operator >>' is ambiguous
    c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\istream(413): could be 'std::basic_istream<_Elem,_Traits>::_Myt &std::basic_istream<_Elem,_Traits>:perator >>(std::basic_istream<_Elem,_Traits>::_Mysb *)'
    with
    [
    _Elem=char,
    _Traits=std::char_traits<char>
    ]
    c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\istream(394): or 'std::basic_istream<_Elem,_Traits>::_Myt &std::basic_istream<_Elem,_Traits>:perator >>(void *& )'
    with
    [
    _Elem=char,
    _Traits=std::char_traits<char>
    ]
    c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\istream(376): or 'std::basic_istream<_Elem,_Traits>::_Myt &std::basic_istream<_Elem,_Traits>:perator >>(long double &)'
    with
    [
    _Elem=char,
    _Traits=std::char_traits<char>
    ]
    c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\istream(358): or 'std::basic_istream<_Elem,_Traits>::_Myt &std::basic_istream<_Elem,_Traits>:perator >>(double &)'
    with
    [
    _Elem=char,
    _Traits=std::char_traits<char>
    ]
    c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\istream(339): or 'std::basic_istream<_Elem,_Traits>::_Myt &std::basic_istream<_Elem,_Traits>:perator >>(float &)'
    with
    [
    _Elem=char,
    _Traits=std::char_traits<char>
    ]
    c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\istream(320): or 'std::basic_istream<_Elem,_Traits>::_Myt &std::basic_istream<_Elem,_Traits>:perator >>(unsigned __int64 &)'
    with
    [
    _Elem=char,
    _Traits=std::char_traits<char>
    ]
    c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\istream(301): or 'std::basic_istream<_Elem,_Traits>::_Myt &std::basic_istr
    f:\c++2\addressClass\addressClass.cpp(118): error C3861: 'main': identifier not found, even with argument-dependent lookup
    f:\c++2\addressClass\addressClass.cpp(147): error C3861: 'repeat': identifier not found, even with argument-dependent lookup
    f:\c++2\addressClass\addressClass.cpp(150): error C3861: 'repeat': identifier not found, even with argument-dependent lookup
    f:\c++2\addressClass\addressClass.cpp(135): error C3861: 'theUserInput': identifier not found, even with argument-dependent lookup
    f:\c++2\addressClass\addressClass.cpp(119): warning C4508: 'tryitout' : function should return a value; 'void' return type assumed
    - Second program i had (BEFORE placing everything into a function) is being able to input a large int for the phone number (5555555555) without it exiting the code and going back to the main.

    Here is my code:
    Code:
    tryitout()
    {
    extPersonType thePerson;
    addressType thePersonAddress;
    dateType thePersonDate;
    string fName, lName, theRelation, theAddress;
    string theCity, theState;
    int theZip, pNumber, theUserInput, repeat = 0, theDOBM;
    int theDOBD, theDOBY;
    	
    	system("cls");
    	cin.ignore();
    
    	cout << "First Name: ";
    	cin >> fName;
    
    	cout << "Last Name: ";
    	cin >> lName;
    
    	cout << "Phone Number: ";
    	cin >> pNumber;
    
    	cout << "Relation (Family, Friend or Associate): ";
    	cin >> theRelation;
    
    	cout << "Address: ";
    	getline(std::cin, theAddress);
    
    	cout << "City: ";
    	cin >> theCity;
    
    	cout << "State: ";
    	cin >> theState;
    
    	cout << "Zip: ";
    	cin >> theZip;
    
    	cout << "DOB (Month): ";
    	cin >> theDOBM;
    
    	cout << "DOB (Day): ";
    	cin >> theDOBD;
    
    	cout << "DOB (Year): ";
    	cin >> theDOBY;
    				
    	thePerson.setNameNum(fName, lName, pNumber, theRelation);
    	thePersonAddress.setAddress(theAddress, theCity, theState, theZip);
    	thePersonDate.setDate(theDOBM, theDOBD, theDOBY);
    
    	thePersonAddress.printAddress();
    
    	system("cls");
    	cout << fName << " has been saved!" << endl;
    	system("PAUSE");
    	main();
    }
    
    int main(){
    
    	system("cls");
    	cout << "(1) Add a User" << endl;
    	cout << "(2) View a User" << endl;
    	cout << "(3) Search by Last Name" << endl;
    	cout << "(0) Exit" << endl;
    	cout << endl;
    	cout << "Pick a number from above: ";
    	
    	do{
    		cin >> theUserInput;
    		repeat = 0;
    
    		switch (theUserInput)
    		{
    			case 1:
    				tryitout();				
    			case 2:
    				break;
    			case 3:
    				break;
    			case 0:
    				break;
    			default:
    				cout << "Thats not a number listed above..." << endl;
    				repeat = 0;
    				system("PAUSE");
    		}
    	} while(repeat != 0);
    
    	system("PAUSE");
    }
    Any help would be great!

    David

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    1) The function 'tryitout' needs a return value.
    2) Never call 'main' directly. If you need to display the menu more than once, just use a loop.
    3) The identifiers 'repeat' and 'theUserInput' don't exist in the context of 'main'. Either declare them in 'main' and pass them to 'tryitout' or else make them global variables (but the former is generally considered better practice).

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    16
    Ah, ok i just saw that i had it inside the tryitout() and not the main:
    Code:
    int main(){
    int repeat = 0, theUserInput;
    However, i am still getting an error:
    f:\c++2\addressClass\addressClass.cpp(121): error C2365: 'main' : redefinition; previous definition was a 'formerly unknown identifier'
    f:\c++2\addressClass\addressClass.cpp(118): error C3861: 'main': identifier not found, even with argument-dependent lookup
    f:\c++2\addressClass\addressClass.cpp(119): warning C4508: 'tryitout' : function should return a value; 'void' return type assumed
    David

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    Post the entire program.

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    16
    Ok, i fixed the errors:
    Code:
    bool tryitout()
    {
    extPersonType thePerson;
    addressType thePersonAddress;
    dateType thePersonDate;
    string fName, lName, theRelation, theAddress;
    string theCity, theState;
    int theZip, pNumber, theDOBM;
    int theDOBD, theDOBY;
    	
    	system("cls");
    	cin.ignore();
    
    	cout << "First Name: ";
    	cin >> fName;
    
    	cout << "Last Name: ";
    	cin >> lName;
    
    	cout << "Phone Number: ";
    	cin >> pNumber;
    
    	cout << "Relation (Family, Friend or Associate): ";
    	cin >> theRelation;
    
    	cout << "Address: ";
    	getline(std::cin, theAddress);
    
    	cout << "City: ";
    	cin >> theCity;
    
    	cout << "State: ";
    	cin >> theState;
    
    	cout << "Zip: ";
    	cin >> theZip;
    
    	cout << "DOB (Month): ";
    	cin >> theDOBM;
    
    	cout << "DOB (Day): ";
    	cin >> theDOBD;
    
    	cout << "DOB (Year): ";
    	cin >> theDOBY;
    				
    	thePerson.setNameNum(fName, lName, pNumber, theRelation);
    	thePersonAddress.setAddress(theAddress, theCity, theState, theZip);
    	thePersonDate.setDate(theDOBM, theDOBD, theDOBY);
    
    	thePersonAddress.printAddress();
    
    	system("cls");
    	cout << fName << " has been saved!" << endl;
    	system("PAUSE");
    	return true;
    }
    
    int main(){
    int repeat = 0, theUserInput;
    bool trueOrFalse;
    
    	system("cls");
    	cout << "(1) Add a User" << endl;
    	cout << "(2) View a User" << endl;
    	cout << "(3) Search by Last Name" << endl;
    	cout << "(0) Exit" << endl;
    	cout << endl;
    	cout << "Pick a number from above: ";
    	
    	do{
    		cin >> theUserInput;
    		repeat = 0;
    
    		switch (theUserInput)
    		{
    			case 1:
    				trueOrFalse = tryitout();
    				if (trueOrFalse == true)
    				{
    					main();
    				}else
    				{
    					cout << "Your data was not saved";
    					system("PAUSE");
    					main();
    				}
    			case 2:
    				break;
    			case 3:
    				break;
    			case 0:
    				break;
    			default:
    				cout << "Thats not a number listed above..." << endl;
    				repeat = 0;
    				system("PAUSE");
    		}
    	} while(repeat != 0);
    
    	system("PAUSE");
    }
    Now onto the second problem. The int and stuff.

    It seems that if i input relation it puts "Address:" and "City: " both on the same input line... Why does it do this?

    David
    Last edited by StealthRT; 10-04-2009 at 01:04 PM.

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    You're still calling 'main' directly. Don't. Put the menu in a loop in 'main'. It's really that simple.

    >> It seems that if i input relation it puts "Address:" and "City: " both on the same input line... Why does it do this?

    No idea, there. I don't use 'cin' much, personally (though I'm sure someone else will have an answer to that question), and since you didn't post the *entire* program there's no way for me to test it.

  7. #7
    Registered User
    Join Date
    Sep 2009
    Posts
    16
    heres the whole program:
    Code:
    #include <string>
    #include <assert.h>
    #include <iostream>
    #include <windows.h>
    #include <fstream>
    
    using namespace std;
    
    class addressType
    {
    public:
    	void printAddress() const;
    	void setAddress(string street, string city, string state, int zip);
    	string getStreet() const;
    	string getCity() const;
    	string getState() const;
    	int getZip() const;
    	addressType(string street = "", string city = "", string state = "", int zip = 0);
    	
    private:
    	string addrStreet;
    	string addrCity;
    	string addrState;
    	int addrZip;
    };
    
    class extPersonType
    {
    public:
    	void printPerson() const;
    	void setNameNum(string first, string last, int number, string personType);
    	void setPersonType(string pt);
    	string getFirstName() const;
    	string getLastName() const;
    	string getPersonType() const;
    	int getNumber() const;
    	extPersonType(string first = "", string last = "", int number = 0000000000, string personType = "");
    	
    private:
    	string firstName;
    	string lastName;
    	string personType;
    	int phoneNumber;
    };
    
    class dateType
    {
    public:
    	void setDate(int month, int day, int year);
    	int getDay() const;
    	int getMonth() const;
    	int getYear() const;
    	void printDate() const;
    	dateType(int month = 1, int day = 1, int year = 1900);
    	
    private:
    	int dMonth;
    	int dDay;
    	int dYear;
    };
    
    
    bool tryitout()
    {
    extPersonType thePerson;
    addressType thePersonAddress;
    dateType thePersonDate;
    string fName, lName, theRelation, theAddress;
    string theCity, theState;
    int theZip, pNumber, theDOBM;
    int theDOBD, theDOBY;
    	
    	system("cls");
    	cin.ignore();
    
    	cout << "First Name: ";
    	cin >> fName;
    
    	cout << "Last Name: ";
    	cin >> lName;
    
    	cout << "Phone Number: ";
    	cin >> pNumber;
    
    	cout << "Relation (Family, Friend or Associate): ";
    	cin >> theRelation;
    
    	cout << "Address: ";
    	getline(std::cin, theAddress);
    
    	cout << "City: ";
    	cin >> theCity;
    
    	cout << "State: ";
    	cin >> theState;
    
    	cout << "Zip: ";
    	cin >> theZip;
    
    	cout << "DOB (Month): ";
    	cin >> theDOBM;
    
    	cout << "DOB (Day): ";
    	cin >> theDOBD;
    
    	cout << "DOB (Year): ";
    	cin >> theDOBY;
    				
    	thePerson.setNameNum(fName, lName, pNumber, theRelation);
    	thePersonAddress.setAddress(theAddress, theCity, theState, theZip);
    	thePersonDate.setDate(theDOBM, theDOBD, theDOBY);
    
    	thePersonAddress.printAddress();
    
    	system("cls");
    	cout << fName << " has been saved!" << endl;
    	system("PAUSE");
    	return true;
    }
    
    int main(){
    int repeat = 0, theUserInput;
    bool trueOrFalse;
    
    do{
    	system("cls");
    	cout << "(1) Add a User" << endl;
    	cout << "(2) View a User" << endl;
    	cout << "(3) Search by Last Name" << endl;
    	cout << "(0) Exit" << endl;
    	cout << endl;
    	cout << "Pick a number from above: ";
    	
    	do{
    		cin >> theUserInput;
    		repeat = 0;
    
    		switch (theUserInput)
    		{
    			case 1:
    				trueOrFalse = tryitout();
    				if (trueOrFalse == true)
    				{
    					repeat = 0;
    					break;
    				}else
    				{
    					cout << "Your data was not saved";
    					system("PAUSE");
    					repeat = 0;
    					break;
    				}
    			case 2:
    				break;
    			case 3:
    				break;
    			case 0:
    				break;
    			default:
    				cout << "Thats not a number listed above..." << endl;
    				repeat = 0;
    				system("PAUSE");
    		}
    	} while(repeat != 0);
    } while(theUserInput != 0);
    
    	system("PAUSE");
    }
    
    
    
    
    
    
    
    
    // START ADDRESS TYPE
    void addressType::printAddress() const
    {
    	cout << "Street: " << addrStreet << "\n" << "City: " << addrCity << "\n" << "State: " << addrState << "\n" << "Zip: " << addrZip;
    	ofstream myfile ("addressFile.txt");
    	if (myfile.is_open())
    	{
    		myfile << addrStreet << "\n";
    		myfile << addrCity << "\n";
    		myfile << addrState << "\n";
    		myfile << addrZip << "\n";
    		myfile.close();
    	}
    	else cout << "Unable to open file";
    }
    
    void addressType::setAddress(string street, string city, string state, int zip)
    {
    	addrStreet = street;
    	addrCity = city;
    	addrState = state;
    	addrZip = zip;
    }
    
    string addressType::getStreet() const
    {
    	return addrStreet;
    }
    
    string addressType::getCity() const
    {
    	return addrCity;
    }
    
    string addressType::getState() const
    {
    	return addrState;
    }
    
    int addressType::getZip() const
    {
    	return addrZip;
    }
    
    addressType::addressType(string street, string city, string state, int zip)
    {
    	addrStreet = street;
    	addrCity = city;
    	addrState = state;
    	addrZip = zip;
    }
    // END ADDRESS TYPE
    
    
    // START PERSON TYPE
    void extPersonType::printPerson() const
    {
    	cout << firstName << " " << lastName << " " << phoneNumber << " " << personType;
    }
    
    void extPersonType::setNameNum(string first, string last, int number, string personType)
    {
    	firstName = first;
    	lastName = last;
    	phoneNumber = number;
    	personType = personType;
    }
    
    void extPersonType::setPersonType(string pt)
    {
    	personType = pt;
    }
    
    string extPersonType::getFirstName() const
    {
    	return firstName;
    }
    
    string extPersonType::getLastName() const
    {
    	return lastName;
    }
    
    string extPersonType::getPersonType() const
    {
    	return personType;
    }
    
    int extPersonType::getNumber() const
    {
    	return phoneNumber;
    }
    
    extPersonType::extPersonType(string first, string last, int number, string personType)
    {
    	firstName = first;
    	lastName = last;
    	phoneNumber = number;
    	personType = personType;
    }
    // END PERSON TYPE
    
    
    // START DATE TYPE
    void dateType::setDate(int month, int day, int year)
    {
    	dMonth = month;
    	dDay = day;
    	dYear = year;
    }
    
    int dateType::getDay() const
    {
    	return dDay;
    }
    
    int dateType::getMonth() const
    {
    	return dMonth;
    }
    
    int dateType::getYear() const
    {
    	return dYear;
    }
    
    void dateType::printDate() const
    {
    	cout << dMonth << "-" << dDay << "-" << dYear;
    }
    
    dateType::dateType(int month, int day, int year)
    {
    	dMonth = month;
    	dDay = day;
    	dYear = year;
    }
    // END DATE TYPE
    David

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,975
    Quote Originally Posted by StealthRT
    I guess as many as I feel like it?
    Read about choosing your forum carefully.

    Quote Originally Posted by StealthRT
    Not everyone on one particular forum may know the answer to my question.
    That is certainly true. On the other hand, more than one person on each forum may know a correct answer to your question, in which case there may be unnecessary duplication of effort.

    Quote Originally Posted by StealthRT
    And besides, it’s nice to have different views of how to go about doing it other than just one.
    It is not so nice for others when they unknowingly repeat the same thing to you, thus wasting their time (and yours).

    Therefore, pose your question to one forum. If after some reasonable length of time you do not receive a satisfactory answer, pose your question to another forum, with a link to the earlier discussion.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Sep 2009
    Posts
    16
    Quote Originally Posted by laserlight View Post
    Read about choosing your forum carefully.


    That is certainly true. On the other hand, more than one person on each forum may know a correct answer to your question, in which case there may be unnecessary duplication of effort.


    It is not so nice for others when they unknowingly repeat the same thing to you, thus wasting their time (and yours).

    Therefore, pose your question to one forum. If after some reasonable length of time you do not receive a satisfactory answer, pose your question to another forum, with a link to the earlier discussion.
    Thank you, laserlight, for telling me politely how this forum works.

    However, if I were to say, post this same question on this forum under c and c++ and anything else associated with c then yes that would be unnecessary. But I am posting on other forums to get their take as well. So that really doesn’t waste anyone’s time and helps me come up with a solution quicker. If I wanted an answer to my question in 3-4 days by posting on one forum then I would simply just email my professor (it takes her a long time to email back). I have better success on forums, though.
    Now getting back on track. The only problem I have having right now is trying to figure out why I can not get a cin to take a string that has spaces in it or if the int is longer than 9 numbers?
    Code:
    cout << "Phone Number: ";
    cin >> pNumber;
    
    cout << "Address: ";
    cin >> theAddress;
    
    cout << "City: ";
    cin >> theCity;
    
    cout << "State: ";
    cin >> theState;
    After I type “555 test street” for the address, it shows both City: & State: on the same line. But if I only type “555” for the address it shows “City: “ and then after entering the city, it shows “State: “

    I’ve tried the cin.ignore(); but doesn’t seem to work.

    David

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,975
    Quote Originally Posted by StealthRT
    But I am posting on other forums to get their take as well. So that really doesn’t waste anyone’s time and helps me come up with a solution quicker.
    You waste people's time because one person on one forum could give you a solution, and another person (or even the same person) on another forum could give you the exact same solution. If the other had known that you already were given that solution, he/she would have moved on instead of taking the time to type out that solution for you.

    Quote Originally Posted by StealthRT
    The only problem I have having right now is trying to figure out why I can not get a cin to take a string that has spaces in it or if the int is longer than 9 numbers?
    Formatted input with operator>> for a string only reads in the first "word" by default. You should use std::getline() to read in the entire line.

    Quote Originally Posted by StealthRT
    I’ve tried the cin.ignore(); but doesn’t seem to work.
    cin.ignore() comes in handy when you have used operator>> to say, read into an int, but next you want to use std::getline() to read in a line to a string. If you do not ignore the whitespace before reading in the line, you will likely end up reading in an apparently blank line, i.e., the part of the previous line that contained the int.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Sep 2009
    Posts
    16
    I still get the same results when using getline() on the address.

    The output looks like this:
    First Name: david
    Last Name: tester
    Phone Number: 3424355
    Relation (Family, Friend or Associate):
    family
    Address: City:
    David

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,975
    Post your current code.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    Any getline that comes after a cin.operator>>() call is likely going to also need a call to cin's ignore member function prior to the getline.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  14. #14
    Registered User
    Join Date
    Sep 2009
    Posts
    16
    this is my output after i do the cin.ignore();
    First Name: david
    Last Name: blah
    Phone Number: 3453453
    Relation (Family, Friend or Associate):
    family
    Address: 9227 amos
    City: State:
    Code:
    cout << "Relation (Family, Friend or Associate): " << endl;
    cin >> theRelation;
    
    cout << "Address: ";
    cin >> theAddress;
    cin.ignore();
    
    cout << "City: ";
    cin >> theCity;
    David
    Last edited by StealthRT; 10-06-2009 at 03:35 PM.

  15. #15
    Registered User
    Join Date
    Sep 2009
    Posts
    16
    And this is my full code:
    Code:
    #include <string>
    #include <assert.h>
    #include <iostream>
    #include <windows.h>
    #include <fstream>
    #include <limits>
    
    using namespace std;
    
    class addressType
    {
    public:
    	void printAddress() const;
    	void setAddress(string street, string city, string state, int zip);
    	string getStreet() const;
    	string getCity() const;
    	string getState() const;
    	int getZip() const;
    	addressType(string street = "", string city = "", string state = "", int zip = 0);
    	
    private:
    	string addrStreet;
    	string addrCity;
    	string addrState;
    	int addrZip;
    };
    
    class extPersonType
    {
    public:
    	void printPerson() const;
    	void setNameNum(string first, string last, int number, string personType);
    	void setPersonType(string pt);
    	string getFirstName() const;
    	string getLastName() const;
    	string getPersonType() const;
    	int getNumber() const;
    	extPersonType(string first = "", string last = "", int number = 0000000000, string personType = "");
    	
    private:
    	string firstName;
    	string lastName;
    	string personType;
    	int phoneNumber;
    };
    
    class dateType
    {
    public:
    	void setDate(int month, int day, int year);
    	int getDay() const;
    	int getMonth() const;
    	int getYear() const;
    	void printDate() const;
    	dateType(int month = 1, int day = 1, int year = 1900);
    	
    private:
    	int dMonth;
    	int dDay;
    	int dYear;
    };
    
    extPersonType thePerson;
    addressType thePersonAddress;
    dateType thePersonDate;
    
    
    void saveToTxt()
    {
    	ofstream myfile ("addressFile.txt");
    	if (myfile.is_open())
    	{
    		myfile << thePerson.getFirstName() << "\n";
    		myfile << thePerson.getLastName() << "\n";
    		myfile << thePerson.getNumber() << "\n";
    		myfile << thePerson.getPersonType() << "\n";
    
    		myfile << thePersonAddress.getStreet() << "\n";
    		myfile << thePersonAddress.getCity() << "\n";
    		myfile << thePersonAddress.getState() << "\n";
    		myfile << thePersonAddress.getZip() << "\n";
    
    		myfile << thePersonDate.getMonth() << "\n";
    		myfile << thePersonDate.getDay() << "\n";
    		myfile << thePersonDate.getYear() << "\n";
    
    		myfile.close();
    	}
    	else cout << "Unable to open file";
    }
    
    bool addSomeone()
    {
    string fName, lName, theRelation, theAddress;
    string theCity, theState;
    int theZip, pNumber, theDOBM;
    int theDOBD, theDOBY;
    	
    	system("cls");
    	cin.ignore();
    
    	cout << "First Name: ";
    	cin >> fName;
    
    	cout << "Last Name: ";
    	cin >> lName;
    
    	cout << "Phone Number: ";
    	cin >> pNumber;
    
    	cout << "Relation (Family, Friend or Associate): " << endl;
    	cin >> theRelation;
    
    	cout << "Address: ";
    	cin >> theAddress;
    	cin.ignore();
    
    	cout << "City: ";
    	cin >> theCity;
    
    	cout << "State: ";
    	cin >> theState;
    
    	cout << "Zip: ";
    	cin >> theZip;
    
    	cout << "DOB (Month ex: 04): ";
    	cin >> theDOBM;
    
    	cout << "DOB (Day ex: 08): ";
    	cin >> theDOBD;
    
    	cout << "DOB (Year ex: 1994): ";
    	cin >> theDOBY;
    				
    	thePerson.setNameNum(fName, lName, pNumber, theRelation);
    	thePersonAddress.setAddress(theAddress, theCity, theState, theZip);
    	thePersonDate.setDate(theDOBM, theDOBD, theDOBY);
    
    	saveToTxt();
    
    	system("cls");
    	cout << fName << " has been saved!" << endl;
    	system("PAUSE");
    	return true;
    }
    
    int main(){
    int repeat = 0, theUserInput;
    bool trueOrFalse;
    
    do{
    	system("cls");
    	cout << "(1) Add a User" << endl;
    	cout << "(2) View a User" << endl;
    	cout << "(3) Search by Last Name" << endl;
    	cout << "(0) Exit" << endl;
    	cout << endl;
    	cout << "Pick a number from above: ";
    	
    	do{
    		cin >> theUserInput;
    		repeat = 0;
    
    		switch (theUserInput)
    		{
    			case 1:
    				trueOrFalse = addSomeone();
    				if (trueOrFalse == true)
    				{
    					repeat = 0;
    					break;
    				}else
    				{
    					cout << "Your data was not saved";
    					system("PAUSE");
    					repeat = 0;
    					break;
    				}
    			case 2:
    				break;
    			case 3:
    				break;
    			case 0:
    				break;
    			default:
    				cout << "Thats not a number listed above..." << endl;
    				repeat = 0;
    				system("PAUSE");
    		}
    	} while(repeat != 0);
    } while(theUserInput != 0);
    
    	system("PAUSE");
    }
    
    
    
    
    
    
    
    
    // START ADDRESS TYPE
    void addressType::printAddress() const
    {
    	cout << "Street: " << addrStreet << "\n" << "City: " << addrCity << "\n" << "State: " << addrState << "\n" << "Zip: " << addrZip;
    }
    
    void addressType::setAddress(string street, string city, string state, int zip)
    {
    	addrStreet = street;
    	addrCity = city;
    	addrState = state;
    	addrZip = zip;
    }
    
    string addressType::getStreet() const
    {
    	return addrStreet;
    }
    
    string addressType::getCity() const
    {
    	return addrCity;
    }
    
    string addressType::getState() const
    {
    	return addrState;
    }
    
    int addressType::getZip() const
    {
    	return addrZip;
    }
    
    addressType::addressType(string street, string city, string state, int zip)
    {
    	addrStreet = street;
    	addrCity = city;
    	addrState = state;
    	addrZip = zip;
    }
    // END ADDRESS TYPE
    
    
    // START PERSON TYPE
    void extPersonType::printPerson() const
    {
    	cout << "First Name: " << firstName << "\n" << "Last Name: " << lastName << "\n" << "Phone Number: " << phoneNumber << "\n" << "Relation: " << personType << "\n";
    }
    
    void extPersonType::setNameNum(string first, string last, int number, string personType)
    {
    	firstName = first;
    	lastName = last;
    	phoneNumber = number;
    	personType = personType;
    }
    
    void extPersonType::setPersonType(string pt)
    {
    	personType = pt;
    }
    
    string extPersonType::getFirstName() const
    {
    	return firstName;
    }
    
    string extPersonType::getLastName() const
    {
    	return lastName;
    }
    
    string extPersonType::getPersonType() const
    {
    	return personType;
    }
    
    int extPersonType::getNumber() const
    {
    	return phoneNumber;
    }
    
    extPersonType::extPersonType(string first, string last, int number, string personType)
    {
    	firstName = first;
    	lastName = last;
    	phoneNumber = number;
    	personType = personType;
    }
    // END PERSON TYPE
    
    
    // START DATE TYPE
    void dateType::setDate(int month, int day, int year)
    {
    	dMonth = month;
    	dDay = day;
    	dYear = year;
    }
    
    int dateType::getDay() const
    {
    	return dDay;
    }
    
    int dateType::getMonth() const
    {
    	return dMonth;
    }
    
    int dateType::getYear() const
    {
    	return dYear;
    }
    
    void dateType::printDate() const
    {
    	cout << "DOB: " << dMonth << "/" << dDay << "/" << dYear << "/n";
    
    }
    
    dateType::dateType(int month, int day, int year)
    {
    	dMonth = month;
    	dDay = day;
    	dYear = year;
    }
    // END DATE TYPE
    David

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cin problem
    By mikahell in forum C++ Programming
    Replies: 12
    Last Post: 08-22-2006, 11:14 AM
  2. Variables and Functions
    By Hitetsu in forum C++ Programming
    Replies: 2
    Last Post: 03-29-2006, 07:01 AM
  3. cin not allowing input after first use of function
    By Peter5897 in forum C++ Programming
    Replies: 5
    Last Post: 01-31-2006, 05:29 PM
  4. functions breaking other functions
    By eth0 in forum C++ Programming
    Replies: 4
    Last Post: 01-12-2004, 02:05 PM
  5. Newbie asking about overloading functions
    By GLR in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2001, 09:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21