|
-
May 26th, 2009, 04:16 AM
#1
If statements acting strangely
Hi all. I've got a program that will (hopefully) run a number of small console-based applications. I'm currently stuck with one of the applications.
The application takes user input, stores it in an array, and retrieves it upon request (sort of like a clipboard with strings only). I also have a method to get a yes/no answer from the user. My error is occurring in a specific call to the getConfirm() method, after I use getline(). The program marks 'y' and 'Y' as invalid input, when they should be valid.
The code for the getConfirm method
Code:
bool getConfirm(string q){
testAgain:
string s;
cout<<q<<"[y/n]"<<endl;
cin>>s;
if(s == "Y"|| s == "y"){
return true;
}
else if(s == "n"|| s == "N"){
return false;
}
else {cerr<<"Invalid entry"<<endl;
goto testAgain;
}
}
and for the entire mini-application (It's a self-contained class, initiated by the main method after the user chooses an application)
Code:
class Indexer{
string assignedVals[200];
public:
Indexer(){
string cont = "new";
int val;
bool yn;
clearVals();
begin:
while(cont == "new" || cont == "NEW" || cont == "New"){
cout<<"Enter an index number below 200 (inclusive) and above 0 (exclusive)"<<endl;
cin>>cont;
stringstream(cont)>>val;
if(val>200){
cout<<"Invalid number: Must be below 200 (inclusive) and not 0(exclusive)"<<endl;
goto begin;
}
cout<<"Enter the description"<<endl;
cin.ignore(100, '\n');
getline(cin,cont);
assignedVals[val - 1] = cont;
cout<<"Select and option: Enter a new value [new], begin retrieval function [ret], or exit (press enter)"<<endl;
cin>>cont;
}
if(cont == ""){
exit(0);
}
else if(cont != "ret" && cont != "Ret" && cont != "RET"){
cerr<<"Invalid input"<<endl;
bool b = getConfirm("Continue description entry?");//This getConfirm call causes the error.
if(b){
goto begin;
}
else {
b = getConfirm("Start searching?");
if(!b){
exit(0);
}
}
}
do{
cout<<"Enter index of value to retrieve"<<endl;
cin>>cont;
stringstream(cont)>>val;
cout<<assignedVals[val-1]<<endl;
yn = getConfirm("Get Value Again?");
}while(yn);
bool b = getConfirm("Begin again?");
if(b){
clearVals();
goto begin;
}
else exit(0);
};
private:
void clearVals(){
for(int i = 0; i<200; i++){
assignedVals[0] = "";
}
}
bool getConfirm(string q){
testAgain:
string s;
cout<<q<<"[y/n]"<<endl;
cin>>s;
if(s == "Y"|| s == "y"){//Continuously fails only when there is an error
return true;
}
else if(s == "n"|| s == "N"){
return false;
}
else {cerr<<"Invalid entry"<<endl;
goto testAgain;
}
}
};
If you feel the need to compile and reproduce the error, or need to see output/input, see below:
Code:
Enter an index number below 200 (inclusive) and above 0 (exclusive)
1
Enter the description //Line 3
Hi
Select and option: Enter a new value [new], begin retrieval function [ret], or exit (press enter)
f
Invalid input //This is correct
Continue description entry?[y/n]
y //This should be valid and return to line 3
Invalid input //Should not be printed -- should have returned to line 3 already
Continue description entry?[y/n] //Should not be printed
n //Should not be printed
Start searching?[y/n] //Should not be printed
n //Should not be printed
I hope all my terminology and everything else is correct. I'm really a Java developer, so...
All help/questions are welcome,
Singing Boyo
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|