Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   What's wrong with this code? (http://www.programmingforums.org/showthread.php?t=13747)

PaCkEtPiRaTe Aug 10th, 2007 11:50 PM

What's wrong with this code?
 
This is a code for a game that I'm making... it's not the whole game obviously, just the menu so far... but I can't figure out what the 5 errors are...

:

#include <iostream>
//Days of Legend. Programmed by Darin Beaudreau.
//Copyright© 2007-2012. All rights reserved.
//This code may not be redistributed.

using namespace std;

int main()
{
 while(1 == 1)
{
        string playername;
        string playerrace;
        string playerclass;
        int status;
        int ready;
        int menuchoice;

//Opening
cout << "Hello there, what is your name? ";
getline (cin, playername);
cout << endl;
cout << "Hello, " << playername << "!\n";
cout << "What race will you play? ";
getline (cin, playerrace);
cout << "And what class shall you play, brave adventurer? ";
getline (cin, playerclass);
cout << "You have chosen to be a " << playerrace << " " << playerclass << "!\n";
cout << "Welcome, " << playername << "to Days of Legend!\n";
cout << "In a world filled with evil, will you take up your\n";
cout << "sword and defend the innocent? ";
cin >> ready;

        if (ready == 1);
                cout << "Then welcome to Narghoz, the first town in your\n";
                cout << "no doubt epic adventure!\n\n";
                cout << "What would you like to do, " << playername << "?\n";
                cout << "1.) Check Status\n";
                cout << "2.) Quit\n";
                cout << "Make your choice! ";
                cin >> menuchoice;
                break;
        if (ready == 2);
                cout << "Then come back when you have some hair on your chest!\n";
                return 0;
        }
 system ("PAUSE");
}


ReggaetonKing Aug 11th, 2007 12:02 AM

Check your if statements! You need to add braces { } around the body after the if statements. Another problem is that you never included the string header file. You must do that in order to use the string class.

Also, you redeclare all of your variables after the loop iterates completely. This might not be a good idea.

*TIP*: Add the error messages when you post something like this!

EDIT: Are you and Arnack working together on this? Maybe the same person?

PaCkEtPiRaTe Aug 11th, 2007 12:11 AM

Yes, me and him are working together. Once we learn C++ well enough we're going to try to start a game company.

ReggaetonKing Aug 11th, 2007 12:12 AM

Good luck with that! I hope for the best.

Arnack Aug 11th, 2007 12:15 AM

Thanks ^_^

PaCkEtPiRaTe Aug 11th, 2007 12:16 AM

So, will this fix it?

:

#include <iostream>
#include <string.h>
//Days of Legend. Programmed by Darin Beaudreau.
//Copyright© 2007-2012. All rights reserved.
//This code may not be redistributed.

using namespace std;

int main()
{
 while(1 == 1)
{
        string playername;
        string playerrace;
        string playerclass;
        int status;
        int ready;
        int menuchoice;

//Opening
cout << "Hello there, what is your name? ";
getline (cin, playername);
cout << endl;
cout << "Hello, " << playername << "!\n";
cout << "What race will you play? ";
getline (cin, playerrace);
cout << "And what class shall you play, brave adventurer? ";
getline (cin, playerclass);
cout << "You have chosen to be a " << playerrace << " " << playerclass << "!\n";
cout << "Welcome, " << playername << "to Days of Legend!\n";
cout << "In a world filled with evil, will you take up your\n";
cout << "sword and defend the innocent? ";
cin >> ready;

        if (ready == 1){
                cout << "Then welcome to Narghoz, the first town in your\n";
                cout << "no doubt epic adventure!\n\n";
                cout << "What would you like to do, " << playername << "?\n";
                cout << "1.) Check Status\n";
                cout << "2.) Quit\n";
                cout << "Make your choice! ";
                cin >> menuchoice;
                }
        if (ready == 2){
                cout << "Then come back when you have some hair on your chest!\n";
                return 0;
                }
        }
 system ("PAUSE");
}


ReggaetonKing Aug 11th, 2007 12:18 AM

Recompile that and see for yourself.

PaCkEtPiRaTe Aug 11th, 2007 12:24 AM

Ok, now when I enter Yes for if I'm ready, or if I had entered No I'm guessing the same would happen, it indefinitely prints the contents of the first IF statement (at least I think that's what it was, it was moving fast).
No errors in compilation obviously...

Edit: Upon closer inspection, it was indefinitely printing the text from the question where it asks if you are ready.

Edit: I can input 1 for yes or 2 for no, but I want to be able to input yes or no. Also, when it asks me what I want to do, I choose 1 or 2 and it restarts the whole thing.

Edit: Ok I defined Yes as 1 and No as 2 and changed the 1 and 2 in the if statements to Yes and No, but now when I enter my choice it keeps printing the questions over and over.

Prm753 Aug 11th, 2007 12:32 AM

:

int ready;

...

cout << "In a world filled with evil, will you take up your\n";
cout << "sword and defend the innocent? ";
cin >> ready; // You put "Yes"

if (ready == 1)
{
    ...
}


Does Yes and 1 look the same?

Well, no. "Yes" is a string, while 1 can be the value of an int. cin is expecting an int, so why are you giving it a string?

PaCkEtPiRaTe Aug 11th, 2007 12:35 AM

Would I add this?

int 1 = Yes;
int 2 = No;

or

int Yes = 1;
int No = 2;

Because I tried that second one and the error in my third edit of the above post happened...

Edit: Happens either way...


All times are GMT -5. The time now is 3:10 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC