// A personalized adventure

#include <iostream>
#include <string>
#include <iostream>
#include <string>

using std::cout;
using std::cin;
using std::endl;
using std::string;

using namespace std;

int main() 
{
	cout << "\tGame Designer's Network\n";
	int security = 0;

	string username;
	cout << "\nUsername: ";
	cin >> username; 

    string password;
	cout << "Password: ";
	cin >> password; 

	if (username == "colton" || password == "williams")
	{
		cout << "\nWelcome, colton.";
		security = 10;
	}

	if (!security)
    	
        cout << "\nYour login failed.";
		cout << "\nAsk Colton for your username and password.";

      char again= 'y';
      while (again=='y')
{
	const int GOLD_PIECES = 900;
	int adventurers, killed, survivors;
	string leader;

	//get the information
	cout << "Welcome to Lost Fortune\n\n";
	cout << "Please enter the following for your personalized adventure\n";

	cout << "Enter a number: "; 
	cin >> adventurers;

	cout << "Enter a number, smaller than the first: ";
	cin >> killed;

	survivors = adventurers - killed;

	cout << "Enter your last name: ";
	cin >> leader;

	//tell the story
	cout << "\nA brave group of " << adventurers << " set out on a quest ";
	cout << "-- in search of the lost treasure of the Ancient Dwarves. ";
	cout << "The group was led by that legendary rogue, " << leader << ".\n";

	cout << "\nAlong the way, a band of marauding ogres ambushed the party. ";
	cout << "All fought bravely under the command of " << leader;
	cout << ", and the ogres were defeated, but at a cost. ";
	cout << "Of the adventurers, " << killed << " were vanquished, ";
	cout << "leaving just " << survivors << " in the group.\n";

	cout << "\nThe party was about to give up all hope. "; 
	cout << "But while laying the deceased to rest, ";
	cout << "they stumbled upon the buried fortune. ";
	cout << "So the adventurers split " << GOLD_PIECES << " gold pieces.";
	cout << leader << " held on to the extra " << (GOLD_PIECES % survivors);
	cout << " pieces to keep things fair of course.\n";

{
              cout << "\n**Played a cool game**";
              cout << "\nDo you want to play again? (y/n): ";
              cin >> again;
}
		return 0;
    }
}

What did i do wrong

Dani AI

Generated

Quick diagnosis: the program returns from main during the first loop iteration because return 0; is inside the loop. As pointed out, return exits the program; was right to flag misplaced braces — the loop's body must actually enclose the story and the replay prompt. Move the return after the loop (or use a loop that naturally repeats) so the game can run multiple times.

A clear, minimal loop pattern (requires #include <cctype> for tolower):

char again = 'y';
do {
    // gather inputs, compute survivors, print the story
    cout << "Play again? (y/n) ";
    cin >> again;
} while (tolower(static_cast<unsigned char>(again)) == 'y');
return 0;

Additional practical points not yet covered in the thread: validate inputs before using them. If survivors = adventurers - killed equals zero or negative, GOLD_PIECES % survivors will crash or be undefined; check killed is between 0 and adventurers and handle the survivors == 0 case explicitly (print a message or skip the split). Normalize the replay response with tolower so both Y and y work. Use continue to restart the loop on invalid input.

Style and housekeeping: remove duplicate #include lines and avoid mixing lots of using forms (as noted). Consistent indentation and brace placement make scope bugs obvious. With the return moved outside the loop, proper validation, and a do/while or correctly-braced while, the story will repeat exactly as intended.

Recommended Answers

All 9 Replies

What did i do wrong

Didn't use CODE tags
Didn't tell us what problem you are having.

HTH

It will not repeat the story between the red { } when i say i want it to.

Well, just look where you put the brackets and what is inside them. What does return 0; do? Do you want that inside your while loop?
Also, I noticed before, in your other question, that these brackets don't seem to belong here. And if it was me, I wouldn't indent quite so far.

{
              cout << "\n**Played a cool game**";
              cout << "\nDo you want to play again? (y/n): ";
              cin >> again;
}

oh dear seems to me that youve included both iostream and string TWICE though thats not messing your code up but its a waste of space and used namespaces when you didn't need to ill get back to in a few

oh dear seems to me that youve included both iostream and string TWICE though thats not messing your code up but its a waste of space and used namespaces when you didn't need to ill get back to in a few

i would just recode it

return 0; is the end of the program and the loop

return 0; is the end of the program and the loop

return 0; will end your program it does not necessarily end the loop since theoretically exiting the program would be part of the loop but try using different control structures like if and else or use switch case if thats easier for you two very very good sites that helped me

http://www.cprogramming.com/
http://www.cplusplus.com/

You've got the basic idea, you just misplaced your { }s. Reformat your code and you should see the problem.

Thanks for the help guys. It repeats now but not exactly what I wanted.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.