//A Fight

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>

using namespace std;

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

int main()
{
    char again = 'y';
    while (again == 'y')

    string yourweapon;
    cout << "Enter weapon: ";
[B][U]    cin >> yourweapon;[/U][/B]

    string yourname;
    cout << "Enter first name: ";
    cin >> yourname;

    string enemyname;
    cout << "Enter enemy name: ";
    cin >> enemyname;

    srand(time(0));

    int randomNumber = rand();

    int healthloss = (randomNumber % 200) + 1;
    cout << "You did damage " << healthloss << endl;

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

    return 0;
}

I am teaching myself C++ and this is my first program. the error is bolded and underlined.

Edited 6 Years Ago by WaltP: Added CODE Tags

It is what my compiler says. It is the error.

Oh, that explains all. Then the solution is get rid of the error.

You need a better compiler that tells you what the error actually is. Get rid of the one you're using if all is says is "This is an error". You need better messages than that.

Not trying to butt in here, but I'd say that the error is on (or at least caused by) a line above the underlined one.

A while loop should include brackets like:

while(condition){//bracket goes here
     //do stuff
    
}//end loop

Edited 6 Years Ago by MyrtleTurtle: n/a

That is a very good information, thanks for the information, if you have more information please tell us.

Here you have the correct code:

//A Fight

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>

using namespace std;

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

int main()
{
    char again = 'y';
	while (again == 'y') {
		string yourweapon;
		cout << "Enter weapon: ";
		cin >> yourweapon;

		string yourname;
		cout << "Enter first name: ";
		cin >> yourname;

		string enemyname;
		cout << "Enter enemy name: ";
		cin >> enemyname;
		
		srand(time(0));

		int randomNumber = rand();

		int healthloss = (randomNumber % 200) + 1;
		cout << "You did damage " << healthloss << endl;

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

    return 0;
}

The while-loop was the problem here - as mentioned. Always use brackets whenever there are several statements inside the loop. :)

Comments
We do NOT correct code for people. We HELP them correcet their own code.

Thankyou now it works. Thanks for that tip I will keep that in mind

This article has been dead for over six months. Start a new discussion instead.