c++ help - infinite loop  | |
November 3rd, 2004, 07:54 PM
|
#1 (permalink)
| | Senior Member
Join Date: Aug 2004 Location: LA, California
Posts: 812
|
Do not get me wrong here, i am really no longer a complete noob at this language... But I am not intermediate yea either.
In our program, when someone enters a character where it should not be, the program goes into an infinite loop and we have to close it. How can I fix this? Code: #include <iostream>
#include <string.h>
#include <string>
using namespace std;
int main ()
{
int suck=0,iVobularyIndex=0,iNumberOfVocabularies = 6;
string strDictionary[]={"poncho","monster","tiger","geek","Derek","God"},strInput="";
bool blnWrong=false;
cout << "\t\t###############################################\n";
cout << "\t\t# Spell Program Version 1.1 #\n";
cout << "\t\t# Mystery Words!! #\n";
cout << "\t\t# #\n";
cout << "\t\t# #\n";
cout << "\t\t###############################################\n";
cout << "\n";
cout << "\n";
while(suck < 4){
cout << "What word would you like to spell? \n ";
for (int i =0; i < iNumberOfVocabularies ; i++){
cout << "\t" << i + 1 << ". " << strDictionary[i] << "\n";
}
cout << "Please enter your selection: ";
cin >> iVobularyIndex;
iVobularyIndex -= 1;
cout << "YOU HAVE CHOSEN TO SPELL " << strDictionary[iVobularyIndex] << "\n";
cout << "SPELL NOW: ";
do{
std::cin >> strInput;
blnWrong = false;
if (strInput.compare(strDictionary[iVobularyIndex])) blnWrong = true;
if (blnWrong )
{
cout << "Spelling " << strDictionary[iVobularyIndex] << "......\n";
cout << "\n";
cout << "\n";
cout << "WRONG SPELLING DETECTED!!! IT IS SPELLED " << strDictionary[iVobularyIndex] << " YOU IDIOT!\n\n";
cout <<"You Spelled: ";
cout << strInput;
cout << "\n";
cout << "TRY SPELLING IT AGAIN! ";
suck ++ ;
}else
{
cout << "\n";
cout << "\nGreat job! You spelled: ";
cout << strInput;
cout << "\n"<<"\n";
suck = 0;
}
}while(blnWrong);
}
return 0;
}
er... sorry about the tabs. My problem is like this:
if a person enters a char as an int then program goes into an infinite loop displaying cout from various inputs.
__________________
People are like coins, there's always two sides.
Last edited by TechKnickle : November 3rd, 2004 at 07:56 PM.
|
| |
November 4th, 2004, 06:30 AM
|
#2 (permalink)
| | may contain mild peril
Join Date: Oct 2001 Location: UK
Posts: 3,329
|
Not only does it loop but it reads random chunks of systems memory if you enter an unexpected value for iVobularyIndex. Validate your input and discard anything that doesn't explicitly match what you are interested in. I would probably hold the dictionary in a vector of strings and check that I was not trying to index it with anything stupid, but then I am something of a "complete noob"
Regards
ed
Last edited by SpookyEddy : November 4th, 2004 at 06:33 AM.
|
| |
November 4th, 2004, 08:12 AM
|
#3 (permalink)
| | may contain mild peril
Join Date: Oct 2001 Location: UK
Posts: 3,329
|
I was thinking of something like this... Code: #include<iostream>
#include<vector>
#include<string>
int main()
{
using namespace std;
vector<string> dictionary;
dictionary.push_back("foo");
dictionary.push_back("bar");
cout << "Spelling Test v0.1" << endl << endl;
for(int i=0; i < dictionary.size(); i++)
cout << i << ") " << dictionary[i] << endl;
cout << endl << "Make your choice... ";
int choice = 0;
if(cin >> choice)
{
if(choice < dictionary.size())
{
if(choice >= 0)
{
cout << endl << "You selected " << dictionary[choice] << endl;
cout << "Spell: ";
string spell;
cin >> spell;
if(spell == dictionary[choice])
cout << endl << "Well done." << endl;
else
cout << endl << "Fool." << endl;
}
else
exit(1);
}
else
exit(1);
}
else
exit(1);
return 0;
} Of course that may not even compile as I don't really know any C++
Regards
ed |
| |
November 4th, 2004, 11:40 AM
|
#4 (permalink)
| | Member
Join Date: Sep 2004
Posts: 164
|
why are you using a do/while loop there? it's not really necessary just use a standard while
__________________
"Nothing is ever as bad as it seems, and nothing is ever as good as it seems. Reality is usually somewhere in the middle" - Lou Holtz
|
| |
November 6th, 2004, 03:50 PM
|
#5 (permalink)
| | Junior Member
Join Date: Nov 2004 Location: Seattle
Posts: 21
|
hAVE YOU TRIED USING CHARACTER VALIDATORS (isalph or isnum) they validate whether the input is numeric |
| |
November 6th, 2004, 03:57 PM
|
#6 (permalink)
| | Junior Member
Join Date: Nov 2004 Location: Seattle
Posts: 21
|
you can use them in if statements and return false if theyre characters |
| |
November 6th, 2004, 04:36 PM
|
#7 (permalink)
| | Ultimate Member
Join Date: Sep 2003 Location: Philadelphia
Posts: 1,484
|
"Of course that may not even compile as I don't really know any C++"
Ssupiciously enugh it compiles, and runs without causing general protection faults  . |
| |
November 6th, 2004, 06:13 PM
|
#8 (permalink)
| | Ultimate Member
Join Date: Sep 2003 Location: Philadelphia
Posts: 1,484
| Code: #include <iostream>
#include <string.h>
#include <string>
using namespace std;
int main (){int suck=0;
string strDictionary[]={"poncho","monster","tiger","geek","Derek","God"};
string input="";
cout << "\t\t###############################################\n";
cout << "\t\t# #\n";
cout << "\t\t# #\n";
cout << "\t\t# Spell Program Version 1.2 #\n";
cout << "\t\t# Mystery Words!! #\n";
cout << "\t\t# #\n";
cout << "\t\t# #\n";
cout << "\t\t###############################################\n";
cout << "\n \n";
cout << "What word would you like to spell? \n ";
for(int i=0; i<=5; i++){cout << "\t" << i + 1 << ". " << strDictionary[i] << "\n";};
cout << "Please enter your selection: ";
do{cin >> input;}while(!(isdigit(input[0]) and (input[0] <= '6') and (input[0] >= 1)));
cout << (char)input[0] << ". Spell the word ";
input[0] = input[0] - '0' - 1;
cout << strDictionary[input[0]] << "\n\n";
suck = input[0];
while(input != strDictionary[suck])
{
cin >> input;
if (input != strDictionary[suck])
{cout << "Haha \nYou suck \nI will now exit :P";
int i = 0;
for(;;){strDictionary[i++]="You SUCK!!!";};
}
else
{return 0;};
}
}
How do you guys get the cool code window? edit: nevermind
Last edited by elmers : November 6th, 2004 at 06:16 PM.
|
| | | Thread Tools | Search this Thread | | | | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |