Thread: My do...while loops only once

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    89

    My do...while loops only once

    It wont exit when I type 'n'...


    Code:
    #include "Library.h"
    #include "Globals.h"
    #include "Player.h"
    
    PLAYER player;
    
    void PlayerSetUp(PLAYER* player)
    {
        char choice[5], newname[32];
        int newstrength, newdexterity, newintelligence, newendurance, newcharm;
        int roll;
        
    	cout << "Here we will set your Hero's name and attributes.\n";
    	cout << "First lets start with your name.\n";
        do
    	{
    		cout << "Enter a name, no more than 32 characters long.\n";
    		cout << ">";
    		cin.getline(newname, 32, '\n');
    		cout << "You have entered: " << newname << "\n";
    		cout << "Is this correct? Y/N\n";
    		cout << ">";
    		cin  >> choice;
            cin.ignore(INT_MAX, '\n'); 
    	}
    	while (choice[0] == 'n' || choice[0] == 'N');
    	player->SetName(newname);
    	cout << "Your Hero is now known as: " << player->GetName() << "\n";
    	Pause();
    	Clr_Scrn();
    
    	cout << "Next we will set " << player->GetName() << "'s" << " attributes.\n";
    	cout << "You will have 5 attributes.\n";
    	cout << "Strength\n" << "Intelligence\n" << "Dexterity\n" << "Endurance\n" << "Charm\n";
    	cout << "Each one has its own advantages, your goal is to find the right combination";
    	cout << " for you\n" << endl;
    	Pause();
    	Clr_Scrn();
    
    	cout << "We will now start with strength\n";
    	Pause();
    	do
    	{
            int roll = 0;
    		cout << "You get three rolls and three rolls only to choose a value for strength.\n";
    		cout << "If you have not chosen a value after your third roll, that value will be\n";
    		cout << " assigned to " << player->GetName() << "'s" << " Strength.\n";
    		Pause();
    		cout << "Roll: " << roll + 1 << "\n";
    		newstrength = (rand() % 10 - 1) + 1;
    		cout << "You rolled a " << newstrength << "\n";
    		cout << "Do you accept? Y/N\n";
    		cout << ">";
    		cin  >> choice;
    		roll++;
    	}
    	while ((choice[0] == 'n' || choice[0] == 'N') && roll <= 3);
    	player->SetStrength(newstrength);
    	cout << "Congratulations! " << player->GetName() << "is now: " << player->GetStrength() << endl;
    	Pause();
    	Clr_Scrn();
    	
    	cout << "Now Intelligence\n";
    	Pause();
    	do
    	{
    		cout << "You get three rolls and three rolls only to choose a value for intelligence.\n";
    		cout << "If you have not chosen a value after your third roll, that value will be\n";
    		cout << " assigned to " << player->GetName() << " Intelligence.\n";
    		Pause();
    		int roll = 0;
    		cout << "Roll: " << roll + 1 << "\n";
    		newintelligence = (rand() % 10 - 1) + 1;
    		cout << "You rolled a " << newintelligence << "\n";
    		cout << "Do you accept? Y/N\n";
    		cout << ">";
    		cin  >> choice;
    		roll++;
    	}
    	while ((choice[0] == 'n' || choice[0] == 'N') && roll <= 3);
    	player->SetIntelligence(newintelligence);
    	cout << "Congratulations! " << player->GetIntelligence() << "is now: " << player->GetIntelligence() << endl;
    	Pause();
    	Clr_Scrn();
    
    	cout << "Now Endurance\n";
    	Pause();
    	do
    	{
    		cout << "You get three rolls and three rolls only to choose a value for endurance.\n";
    		cout << "If you have not chosen a value after your third roll, that value will be\n";
    		cout << " assigned to " << player->GetName() << " Endurance.\n";
    		Pause();
    		int roll = 0;
    		cout << "Roll: " << roll + 1 << "\n";
    		newendurance = (rand() % 10 - 1) + 1;
    		cout << "You rolled a " << newendurance << "\n";
    		cout << "Do you accept? Y/N\n";
    		cout << ">";
    		cin  >> choice;
    		roll++;
    	}
    	while ((choice[0] == 'n' || choice[0] == 'N') && roll < 3);
    	player->SetEndurance(newendurance);
    	cout << "Congratulations! " << player->GetEndurance() << "is now: " << player->GetEndurance() << endl;
    	Pause();
    	Clr_Scrn();
    
    	cout << "Finally Charm\n";
    	Pause();
    	do
    	{
    		cout << "You get three rolls and three rolls only to choose a value for charm.\n";
    		cout << "If you have not chosen a value after your third roll, that value will be\n";
    		cout << " assigned to " << player->GetName() << " Charm.\n";
    		Pause();
    		int roll = 0;
    		cout << "Roll: " << roll + 1 << "\n";
    		newcharm = (rand() % 10 - 1) + 1;
    		cout << "You rolled a " << newcharm << "\n";
    		cout << "Do you accept? Y/N\n";
    		cout << ">";
    		cin  >> choice;
    		roll++;
    	}
    	while ((choice[0] == 'n' || choice[0] == 'N') && roll < 3);
    	player->SetCharm(newcharm);
    	cout << "Congratulations! " << player->GetCharm() << "is now: " << player->GetCharm() << endl;
    	Pause();
    	Clr_Scrn();
    }
    the first loops just fine...its just the ones after that...I have been banging my head agianst it all day.

  2. #2
    Registered User
    Join Date
    Apr 2007
    Location
    Sweden
    Posts
    41
    What do you mean, "won't exit when I type 'n'"... is it supposed to? Where?

    BTW, I don't think you want to do this in (some of) your loops:
    Code:
    int roll = 0
    You'd want:
    Code:
    roll = 0
    ...before the loops.

    And "rolls <= 3" (which you have written in some places) means it will loop four times. :-)

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    89
    What do you mean, "won't exit when I type 'n'"... is it supposed to? Where?
    oops...I meant it doesnt exit when I type y for yes...

    BTW, I don't think you want to do this in (some of) your loops:

    Code:
    int roll = 0You'd want:

    Code:
    roll = 0...before the loops.

    And "rolls <= 3" (which you have written in some places) means it will loop four times. :-)
    Yeah I can change that.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    What you are saying is:
    while choice[0] == 'N' and roll < 3.

    To exit earlier, type something different than 'n' or 'N'

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loops Trouble
    By rlframpton in forum C Programming
    Replies: 2
    Last Post: 04-17-2009, 01:08 AM
  2. Too many loops D:
    By F5 Tornado in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2007, 01:18 AM
  3. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 04:28 PM
  4. for loops in C
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 10-15-2001, 05:09 PM
  5. exiting loops with ease?
    By KingRuss in forum Game Programming
    Replies: 3
    Last Post: 09-24-2001, 08:46 PM