home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Ask a Tech Support Question (free)!

c++ help - infinite loop

Reply
Get bargains at  »  Dealighted.com
 
Thread Tools Search this Thread
Currently Active Users: 2088
Discussions: 200,948, Posts: 2,379,406, Members: 246,309
Old November 3rd, 2004, 07:54 PM   Digg it!   #1 (permalink)
Senior Member
 
TechKnickle's Avatar
 
Join Date: Aug 2004
Location: LA, California
Posts: 812
Send a message via AIM to TechKnickle Send a message via MSN to TechKnickle
c++ help - infinite loop

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.
TechKnickle is offline   Reply With Quote
Old November 4th, 2004, 06:30 AM     #2 (permalink)
may contain mild peril
 
SpookyEddy's Avatar
 
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.
SpookyEddy is offline   Reply With Quote
Old November 4th, 2004, 08:12 AM     #3 (permalink)
may contain mild peril
 
SpookyEddy's Avatar
 
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
SpookyEddy is offline   Reply With Quote
Old November 4th, 2004, 11:40 AM     #4 (permalink)
Member
 
Stick32's Avatar
 
Join Date: Sep 2004
Posts: 164
Send a message via AIM to Stick32
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
Stick32 is offline   Reply With Quote
Old 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
Valentin is offline   Reply With Quote
Old 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
Valentin is offline   Reply With Quote
Old November 6th, 2004, 04:36 PM     #7 (permalink)
Ultimate Member
 
elmers's Avatar
 
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 .
elmers is offline   Reply With Quote
Old November 6th, 2004, 06:13 PM     #8 (permalink)
Ultimate Member
 
elmers's Avatar
 
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.
elmers is offline   Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Similar Threads
Thread Thread Starter Forum Replies Last Post
Sound card problem muno Multimedia and Audio 10 April 30th, 2004 11:51 AM
XP Infinite Loop Error in Graphics Card gyoung Graphics Cards and Displays 9 March 13th, 2003 05:09 PM
NVIDIA Geforce 2 MX 400 hozzie Graphics Cards and Displays 7 January 14th, 2003 12:39 PM
Stupid C++ question techteen143 Webmastering and Programming 29 October 31st, 2002 04:02 PM
What is the "system file checker"? gorath Applications and Operating Systems 38 July 9th, 2002 11:16 PM


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Most Active Discussions
Making Health Care Worse (174)
Is It Just Me? (2938)
The disrespect of Obama by Russian .. (23)
Wireless Televisions. (12)
windows 7 problem (7)
CPU fan stops spinning randomly (8)
Regular Build (6)
Is the PSU I received dead? (12)
radeon x850xt platinum & shader.. (5)
Print spooler problem (15)
HIS HD5770 graphic card question (15)
windows vista security holes (9)
Install XP pro and a Vista laptop ?.. (11)
Dept. of HS: NSA 'Helped' Develop V.. (15)
Recent Discussions
For Sale BFG GTX285 OC2 with 10 year .. (3)
Point and Shoot Camera Suggestions. (3)
How to convert MP3's (4)
Wireless Televisions. (12)
Graphics Card Upgrade Question (3)
Laptop with wireless problem. (2)
Internet Lost (1)
Hp Artist Edition + Matching Bag (0)
My monitor won't turn on after instal.. (0)
Asus P4G8X Mobo (6)
radeon x850xt platinum & shader 3 (5)
Xbox 360 GTA: SA disk error (1)
Is the PSU I received dead? (12)
windows 7 internet problem (5)
Multiple Restarts Required at Boot (0)
BSOD On Startup (ntoskrnl.exe) (2)
Print spooler problem (15)
Have you switched yet? (86)
screen resolution vs monitor size (2)
sms storage to PC (0)
Regular Build (6)
Open With ..... Win7 (0)
java code for fibonacci (1)
[F@H SPAM 11/16/09] ! 1/2 months to r.. (35)
windows 7 problem (7)


All times are GMT -4. The time now is 09:46 PM.
TechIMO Copyright 2009 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28