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:
Quote:
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>::operator >>(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>::operator >>(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>::operator >>(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>::operator >>(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>::operator >>(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>::operator >>(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