Infinate Loop

This is a discussion on Infinate Loop within the C++ Programming forums, part of the General Programming Boards category; When i try to run my program i get an infinate loop and i dont understand why Code: #include <fstream> ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    176

    Infinate Loop

    When i try to run my program i get an infinate loop and i dont understand why


    Code:
    #include <fstream>
    #include <iostream>
    #include <string>
    using namespace std;
    int main()
    {
    	string PlayerFullName, CharacterName, Class, Race, Password, stop, Racials;
    	int hp = 0, xp = 0, mana = 0, sp = 15, SwitchRace;
    
    	system("color 0a");
        system("cls");
    
        cout<<"\nEnter the DM Password or you shall be smited: ";
        getline(cin, Password, '\n');
    
        if (Password == "god")
        {
        system("cls");
        cout<<"\n\nyou truly are an evil gm\n\n Would you like to create a Player?(yes or no): ";
        cin>>stop;
       while (stop == "yes")
       {
     
        cout<<"Enter Player Full Name: ";
    	getline(cin, PlayerFullName, '\n');
        cin.get();
        
    	cout<<"\nEnter Character Name: ";
    	getline(cin, CharacterName, '\n');
        cin.get();
        
    	cout<<"\nEnter Class: ";
    	getline(cin, Class, '\n');
        cin.get();
        
    	cout<<"\nplayable races\n Good Aligned\n________\n(1)Human\n(2)Gnome\n(3)Halfling\n(4)Dwarf\n(5)Elf\n(6)Animal Folk\n(7)Seelie Fae\n\nEvil Aligned\n________\n(1)Human\n(8)Kobold\n(9)Goblin\n(10)Orc\n(11)Dark Elf\n(6)Animal Folk\n(12)Unseelie Fae \ntype the numbers that are next to the race name, to select that race.\n\nEnter Race: ";            
        cin>>SwitchRace;
        cin.ignore();
        
        switch ( SwitchRace )
        {
            case 1:
            cout<<"\n you chose Human +2 ability points";
            Race = "Human";
            sp = sp + 2;
            break;
            
        case 2:
        cout<<"\n you chose Gnome 1/2 cost craft";
        Race = "Gnome";
        Racials = "1/2 cost craft";
        break;
        
            case 3:
            cout<<"\n you chose Halfling 1/2 cost thrown weapons";
            Race = "Halfling";
            Racials = "1/2 cost thrown weapons";
            break;
                
        case 4:
        cout<<"\n you chose Dwarf +2 hp";
        Race = "Dwarf";
        hp = hp + 2;
        sp = 12;
        break;
        
            case 5:
            cout<<"\n you chose Elf +2 mana";
            Race = "Elf";
            mana += 2;
            sp = 12;
            break;
        
        case 6:
        cout<<"\n you chose Animal Folk";
        Race = "Animal Folk";
        Racials = "See Dm";
        break;
       
           case 7:
           cout<<"\n you chose Seelie Fae +2 mana";
           Race = "Seelie Fae";
           mana += 2;
           sp = 12;
           break;
       
        case 8:
        cout<<"\n you chose Kobold 1/2 cost trap making";
        Race = "Kobold";
        Racials = "1/2 cost trap making";
        break;
        
            case 9:
            cout<<"\n you chose Goblin 1/2 Cost Spear";
            Race = "Goblin";
            Racials = "1/2 Cost Spear";
            break;
        
        case 10:
        cout<<"\n you chose Orc 1/2 Cost Two-Handed";
        Race = "Orc";
        Racials = "1/2 Cost Two-Handed";
        break;
        
            case 11:
            cout<<"\n you chose Dark Elf +2 mana";
            Race = "Dark Elf";
            mana += 2;
            sp = 12;
            break;
        
        case 12:
        cout<<"\n you chose Unseelie Fae +2 mana";
        Race = "Unseelie Fae";
        mana += 2;
        sp = 12;
        break;
       
       default:
       cout<<"\n that is not a Race! try again: ";
       cin>>SwitchRace;
       cin.ignore();
       break;
       }
         
        cout<<"\nyour player is.... \n \n \n\n\n\n\n";
    	cout<<"Player Name: "<<PlayerFullName<<"\nCharacter Name: "<<CharacterName<<"\nclass: "<<Class<<"\nRace: "<<Race<<"\nHitpoints: "<<hp<<"\nExperience: "<<xp<<"\nMana: "<<mana<<"\nSkill Points: "<<sp;
    
    	string filename = PlayerFullName + ".txt";
    	ofstream Playerfile ( filename.c_str() );
    	Playerfile<<"Player Name: "<<PlayerFullName<<"\nCharacter Name: "<<CharacterName<<"\nclass: "<<Class<<"\nRace: "<<Race<<"\nHitpoints: "<<hp<<"\nExperience: "<<xp<<"\nMana: "<<mana<<"\nSkill Points: "<<sp;
    	cout<<"\nDo you want to create another character? (yes or no): ";
        cin>>stop;
    	cin.ignore();
    	}
     }
     if (Password != "god")
     {
     }
    cout<<"\nGoodbye";
    cin.get();
     return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > cin>>stop;
    > while (stop == "yes")
    After the cin you should probably add either:
    Code:
    cin.ignore(80,'\n');
    Or:
    Code:
    cin.ignore();

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Like, infinite loop as in blinking cursor, or infinite loop as in screen after screen of scrolling text?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Code:
    default:
       cout<<"\n that is not a Race! try again: ";
       cin>>SwitchRace;
       cin.ignore();
       break;
    This does not start the switch statement all over again. You need a loop of some kind.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    176
    an infinatate loop as in it just keeps flashing text by tou know what i mean well i ahve to eat right now but ill brb to work on this

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    176
    i put cin.ignore() after

    cin>>stop;
    while (stop == "yes")

    but now it wont go past ( Would you like to create a Player?(yes or no): )

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Use your debugger if you have one. Just step over each line of code one at a time, providing input when you need to, until you step over a line that is supposed to wait for input but keeps going instead. Whatever was the last read before that will probably be what is leaving the stream in a bad state or leaving extra characters in the stream.

  8. #8
    Registered User
    Join Date
    Mar 2003
    Posts
    176
    ok thanks i got it
    Last edited by sreetvert83; 07-28-2005 at 05:43 PM.

  9. #9
    Registered User
    Join Date
    Mar 2003
    Posts
    176
    it compiled once then it started doing the same thing it was before .... i need help badly and my debugger isn't helping me at all

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >i put cin.ignore() after

    >cin>>stop;
    >while (stop == "yes")

    After the cin and before the loop?
    Code:
    cin>>stop;
    cin.ignore();
    while (stop == "yes")

  11. #11
    Registered User
    Join Date
    Mar 2003
    Posts
    176
    THANK YOU!!! i accadentally looked over that part of the code
    hehe... im tired been runnin on coffee for 2 days im goin to sleep now good thing to i needed thsi prog. tomorrow

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 11:44 AM
  2. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 01:13 AM
  3. A somewhat bizzare problem!!! - WHILE LOOP
    By bobthebullet990 in forum C Programming
    Replies: 3
    Last Post: 03-31-2006, 07:19 AM
  4. Infinate Loop, not for all
    By Erf in forum C++ Programming
    Replies: 1
    Last Post: 07-22-2004, 03:26 AM
  5. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 04:45 PM

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