Public Group

# Never ending loops and repeating functions!

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

## 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.

#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; } 

##### 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 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.

How would you go about making [color=#000000][size=2]

player ourHero

[color=#666600][size=2]

=

[color=#000000][size=2]

chooseClass

[color=#666600][size=2]

()

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 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.

 // 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 } 

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 14
• 13
• 9
• 12
• 9
• ### Forum Statistics

• Total Topics
631437
• Total Posts
3000063
×