please help, infinite loops!

This is a discussion on please help, infinite loops! within the C++ Programming forums, part of the General Programming Boards category; Hi, i've been pulling my hair out over this for quite some time, and i'm fed up! Could someone please ...

  1. #1
    HybridM
    Guest

    please help, infinite loops!

    Hi, i've been pulling my hair out over this for quite some time, and i'm fed up! Could someone please tell me why this code works fine once, and after it loops it does so infinitely, without asking for input again.

    Code:
    do{
     cout<<"\nYour current statistics are:"; 
     cout<<"\nHealth Points (HP): "<<player.stathpm;
     cout<<"\nMagic Points (MP):   "<<player.statmpm;
     cout<<"\nFree Points: "<<points;
     cout<<"\n\nTo add points to a statistic, type its abbreviation followed by the number\nof points you wish to add, seperate them with a space. ";
    
     cin.getline(pointchoi, 3, ' ')>>points2;
     if(pointchoi=="HP"||"hp")                                                                              /// AA: Character creation
     {
           if(points2>points)
            {
            cout<<"\nPlease do not attempt to cheat. (fined 2 points)\n\n";
            points = points - 2;
            points2 = 0;
            }
            else if(points2<points)
            {
            player.stathpm = player.stathpm + points2;
            points = points - points2;
            }
     }
     else if(pointchoi=="MP"||"mp")                                                             /// AA:
     {
     
            if(points2>points)
            {
            cout<<"\nPlease do not attempt to cheat. (fined 2 point)\n\n";
            points = points - 2;
            points2 = 0;
            }
            else if(points2<points)
            {
            player.statmpm = player.statmpm + points2;
            points = points - points2;
            }
            
     }
     else                                                                                       /// AA:
     {
     cout<<"\nEnter valid input."<<endl; 
     }
     }while(points>0);
    I appologise if the formatting is bad, it's the first time i've used code tags.
    Thanks in advance for any help, and sorry for the newbie question.

  2. #2
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    a possible cause is the if statements, I dont have much time to look over it in more detail but...

    Code:
    if(pointchoi=="HP"||"hp")
    cannot be done, you have to use strcmp to compare strings and make the comparison explicit, ie

    Code:
    if( strcmp( pointchoi, "HP" ) == 0 || strcmp( pointchoi, "hp" )== 0 )
    sort that out and the loop may well be sorted
    Couldn't think of anything interesting, cool or funny - sorry.

  3. #3
    HybridM
    Guest

    ...

    Thanks but it didn't solve the looping.

  4. #4
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    think u could attach a zip of the whole code?

    /* or just pm it to me doens't have to be a zip, just need to see it as a whole*/
    Last edited by RoD; 01-12-2003 at 08:32 AM.

  5. #5
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    Ok, here is the .cpp file, there is very little there because what you've seen is basically all i've done.
    Attached Files Attached Files

  6. #6
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    Yes, the points variable changes when you input your first choice, before it ****s itself.

    edit** points variable not points2 **edit

  7. #7
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    i stopped it from looping by moving the while to the top. I also, for debugging purposes, made the taking in of abbrev and amount two sep lines. I took the liberty of shortenting your calculations:

    Code:
    variable = variable - another_variable 
    
    to 
    
    variable -= another_variable
    I hope it helps, feel free to pm me if u need.


    Code:
    /*  RPG 2   Copyright 2003 */
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    using namespace std;
    
    /**PLAYER STATISTICS STRUCTURE**/
    struct stats
    {
        int stathpm;
        int stathpc;
        int statmpm;
        int statmpc;
    };
    /**ENEMY STATISTICS STRUCTURE**/
    struct enemies
    {
    int enemyhpm;
    int enemyhpc;
    int enemytype;
    };
    
    ////////Functions!
    void fight(int stathpm,int stathpc,int statmpm,int statmpc);
    
    
    
    
    int main()
    {
    // Structure Variables
    stats player;                                                          
    enemies mob;
    // Structure Variables Initial Values
    player.stathpm=21; 
    player.stathpc=21;
    player.statmpm=7;
    player.statmpc=7;             
    
    // Other Variables + initial values
    char name[15], pointchoi[2];            
    int points=7,points2=0,loop=1; 
    
     cout<<"   RPG2 (working title)   \n\n";
     cout<<"Enter your name: ";
     cin.getline(name, 15, '\n');
     system("cls");
     cout<<"Welcome "<<name<<" to the character creation."<<endl<<endl;
     cout<<"Your character starts off with a preset number of statistics.\n";
     cout<<"However, initially you are granted an additional 10 points\nto add to the statistic of your choosing."<<endl;
     cout<<"Each time you level up will also grant you 5 more points to add to\nyour preferred statistic(s)."<<endl;
     system("pause");
     system("cls");
                                                                                                     //AA: Loop
     cout<<"\nYour current statistics are:"; 
     cout<<"\nHealth Points (HP): "<<player.stathpm;
     cout<<"\nMagic Points (MP):   "<<player.statmpm;
     cout<<"\nFree Points: "<<points;
     cout<<"\n\nTo add points to a statistic, type its abbreviation and press enter\n";
     points2=0;
     cin.getline(pointchoi, 3);
     cout<<"Enter amount";
     cin>>points2;
     
     
     while(points>0)
     {
     if( strcmp( pointchoi, "HP" ) == 1 || strcmp( pointchoi, "hp" )== 1 )                               /// AA = Character creation
     {
           if(points2>points)                                                           
            {
            cout<<"\nPlease do not attempt to cheat. (fined 2 points)\n\n";
            points -= 2;
            points2 = 0;
            }
            else if(points2<points)
            {
            player.stathpm += points2;
            points -= points2;
            }
     }
    
     if( strcmp( pointchoi, "MP" ) == 1 || strcmp( pointchoi, "mp" )== 1 )                                                               /// AA:
     {
     
            if(points2>points)
            {
            cout<<"\nPlease do not attempt to cheat. (fined 2 points)\n\n";
            points -= 2;
            points2 = 0;
            }
            else if(points2<points)
            {
            player.statmpm += points2;
            points -= points2;
            }
            
     }
     else                                                                                       /// AA:
     {
     cout<<"\nEnter valid input."<<endl; 
     }
    
     }
     
                                                                      /// AA END ///
     
     
     
     
     
     
    
                                                            
    
    system("pause");
    return 0;
    }
    
    
    /** Fight function
        Given:  Statistical variables
        Task:   To calculate the end result of a battle based of player and enemy statistics
        Return: Nothing
        To be done...that's not the actual function...it was a test :)
    **/
        
    void fight(int stathpm,int stathpc,int statmpm,int statmpc)
    {
        if(stathpm<11)
        {
        cout<<"Gay ";
        }
        else
        {
        cout<<"Non-Gay. ";
        }
    }

  8. #8
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    Thanks a lot man, i'm eternally grateful.

    note: i haven't tried it yet, so i may turn on you still.

  9. #9
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    lol, it doens't freak out thats a good thing, BTW i didn't test mp only hp. Part of it was the strcmp check was checking for == 0 not ==1....i didn't extensively test it, hope it helps.

  10. #10
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    Man, i've tried it out and it just says you've tried to cheat, and exits...

  11. #11
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    umm, no it doesn't:

    Code:
    Your current statistics are:
    Health Points (HP): 21
    Magic Points (MP):   7
    Free Points: 7
    
    To add points to a statistic, type its abbreviation and press enter
    mp
    Enter amount7

  12. #12
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    Well, if i choose to allocate 3 points to hp for instance, it will print that i have tried to cheat twice, and then exit.

    if i choose 7 points to go to mp it will do nothing, it should exit normally, like the above example does for me, does it do something else for you?

    btw i'm using dev c++

  13. #13
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    hmm if its not fixed ina few hours i will look, but for now its football time and i wanna know if TB or San fran is going to philly against my team!

  14. #14
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    Well i have to get some sleep (it's 5am) so i will continue tomorrow i guess, i'm starting to lose my patients. Thanks for trying though man, if you decide to have another look at it i'll be grateful. Likewise to anyone else who helps.

  15. #15
    Registered User Caze's Avatar
    Join Date
    Nov 2002
    Posts
    18
    Change the ifs to use !strcmpi.
    Code:
    if (!strcmpi("HP", pointchoi))
    if (!strcmpi("MP", pointchoi))
    That gets the points value to change but it doesn't fix the loop problem.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Infinite Loops in C
    By DaniiChris in forum C Programming
    Replies: 8
    Last Post: 07-06-2008, 12:21 PM
  2. Infinite While Loops Question
    By hern in forum C++ Programming
    Replies: 4
    Last Post: 08-13-2005, 07:27 PM
  3. i have infinite loops. why?
    By hermit in forum C Programming
    Replies: 20
    Last Post: 04-17-2002, 06:58 AM
  4. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 03:28 PM

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