Sign in to follow this  

Never ending loops and repeating functions!

This topic is 2124 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So I rewrote this nearly 5 times from scratch every time and this is my most successful one yet. One of my two problems, if you don't count having all my code in the main.cpp file, is that my default on the switch() is neverendingly repeated. My other problem is that player chooseClass repeats twice. Once in the main() and then once below where it was written. I placed it there because it's the only place I got no errors or warnings while setting it to = player ourHero. Any suggestions would be great, even if it's not on what I mentioned. I first tried using strings, but I obviously have yet to grasp that concept.

[code]#include <iostream>
#include <string>

using namespace std;



struct player
{
player(const string playerClass, int health, int str, int dex, int cha) : playerClass(playerClass),
health(health), str(str), dex(dex), cha(cha){}
string playerClass;
int health;
int str;
int dex;
int cha;

};

struct item
{
item(const string itemName, string modifier, float multiplier) : itemName(itemName), modifier(modifier), multiplier(multiplier) {}
string itemName;
string modifier;
float multiplier;

};

player chooseClass()
{
cout << "\nWhich class would you like to play?\n" << endl << "1. Fighter\n" << endl <<
"2. Assassin\n" << endl << "3. Barbarian\n" << endl;
char playerSelect;
cin >> playerSelect;

while(true)
{
switch(playerSelect)
{
case '1':
return player("Fighter", 50, 13, 9, 7);
case '2':
return player("Assassin", 30, 7, 13, 8);
case '3':
return player("Barbarian", 40, 14, 11, 4);
default:
cout << "Invalid selection. Please choose again..." << endl << "1. Fighter\n" << endl << // If this happens, it never ends.
"2. Assassin\n" << endl << "3. Barbarian\n" << endl;
}
}

}

player ourHero = chooseClass(); // I believe this is my repeating function problem


item chooseWeapon()
{
cout << "In Anternia, there are many, many valuable, rare, powerful weapons. However, this is not what you will have.\n" <<
"Press any key to continue." << endl;
cin.ignore();
cin.get();
if(ourHero.playerClass == "Fighter")
cout << "As a fighter you must harness your strength behind your weapon, but also remain agile enough to slice the throat" <<
" of your enemies.\n" << endl << "Press any key to continue." << endl;
cin.get();
cout << "Here is your miltia short sword.";
return item("militia short sword", "str", 1);
}

int main()
{
cout << "Welcome to Anternia. Please enter your name: ";
string playerName;
cin >> playerName;
cout << "\nI don't think I've heard that one before.\n" << endl << "Are you male or female?\n" << endl << "1. Male\n" << endl <<
"2. Female\n" << endl;
string sex;
cin >> sex;
chooseClass();
chooseWeapon();

return 0;
}


[/code]

Share this post


Link to post
Share on other sites
Why would you expect anything else if you just keep prompting the user to enter something but never actually read his input again? I'd also suggest to either always use braces for your if blocks or be very careful with indentation, because in your chooseWeapon the indentation suggests the entire block is part of the if, when the missing braces say something completely different.

Share this post


Link to post
Share on other sites
[quote name='Trienco' timestamp='1329652435' post='4914486']
Why would you expect anything else if you just keep prompting the user to enter something but never actually read his input again? I'd also suggest to either always use braces for your if blocks or be very careful with indentation, because in your chooseWeapon the indentation suggests the entire block is part of the if, when the missing braces say something completely different.
[/quote]

How would you go about making [color=#000000][size=2][left]player ourHero [/left][/size][/color][color=#666600][size=2][left]=[/left][/size][/color][color=#000000][size=2][left] chooseClass[/left][/size][/color][color=#666600][size=2][left]() [/left][/size][/color]without calling the function again? I normally do the braces with the if statements. Not sure why I missed them there, unless I was waiting to add the other 2 classes. Will add them. Thanks!

Share this post


Link to post
Share on other sites
He means you never read the user input in your loop so your playerSelect won't change and you will always fall in your default case. To correct this you must read the player input in your default case.

Also you should break the input reading and the class selection into two distinctive tasks, one that perform correct reading(a loop where you ask the user to choose a class) and another one that actually create the proper player class.

[CODE]
// firtst read user input
std::string playerSelect;
do
{
std::cout<< "Choose your clas...";
std::cin << playerSelect;
}
while( playerSelect < 1 || playerSelect > 3);
// then create the player
switch(playerSelect)
{
case '1':
return player("Fighter", 50, 13, 9, 7);
case '2':
return player("Assassin", 30, 7, 13, 8);
case '3':
return player("Barbarian", 40, 14, 11, 4);
default: // already check in the loop
}
[/CODE]

Share this post


Link to post
Share on other sites

This topic is 2124 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this