Nathan Law
Nathan Law

Reputation: 1

error expected a declaration c ++

UPDATE 2: well I fixed it, turns out it was a simple matter of a semi column getting in where it didn't belong, was looking in completely the wrong place.

UPDATE:removing some comments fixed the cin cout errors, now the only errors are expected a declaration on the opening and closing brackets and one has appeared on the else statement.

the Error "expected a declaration" appears in the function playeroneturn on the opening and closing {} aswell as the closing } of the if statement in the function, also in the if statement the cin and cout both give the error "this declaraton has no storage class or type specifier"

#include "stdafx.h"
#include "iostream"
#include "ctime"
#include "cstdlib"
#include "string"

//prototype function declaration from stackoverflow.com/questions/2575153/must-declare-function-prototype-in-c
int help(int menu);
int start(int menu);
int oneplaymode();
int playeroneturn();
int playertwoturn();

//function start is called to display the games menu screen
int start(int menu)
{
    using std::cout;
    using std::cin;
    using std::endl;
    cout << "#########################################################################" << endl;
    cout << "#\t\t\t\t\t\t\t\t\t#" << endl << "#\t\t\t\t    PIGS\t\t\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl << "#\t\t\t\t1. 1 Player\t\t\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl << "#\t\t\t\t2. 2 Player\t\t\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl << "#\t\t\t\t3. help\t\t\t\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl<< "#\t\t\t\t\t\t\t\t\t#" << endl<< "#\t\t\t\t\t\t\t\t\t#" << endl<< "#\t\t\t\t\t\t\t\t\t#" << endl<< "#\t\t\t\t\t\t\t\t\t#" << endl<< "#\t\t\t\t\t\t\t\t\t#" << endl;
    cout << "#########################################################################" << endl;
    cout << "enter number for your selection: ";
    cin >> menu;
    if(menu == 1)
    {
        cout << "single-player not yet implemented" << endl;

    }
    else if(menu == 2)
    {
    int twoplayermode();
    }
    else if(menu == 3)
    {
    help(menu);
    }
    else
    {
    cout << "Error: Please choose a valid option" << endl;

    start(menu); 

    }
return(menu);
}

int help(int menu)
{
    using std::cout;
    using std::endl;    
    cout << "#########################################################################" << endl;
    cout << "#\t\t\t\t\t\t\t\t\t#" << endl << "#\t\t\t\t HELP\t\t\t\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl << "#  The objective of pigs is to be the first to score 100.\t\t#" << endl << "#  Each turn you must roll a die then pass or roll again.\t\t#" << endl << "#  You must then choose to ROLL again or END your turn.\t\t\t#" << endl << "#  At the end of your turn your total is added to your score.\t\t#" << endl << "#  However if you roll a 1 your turn ends and you score 0.\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl << "#\t\t\t\t\t\t\t\t\t#" << endl<< "#\t\t\t\t\t\t\t\t\t#" << endl<< "#\t\t\t\t\t\t\t\t\t#" << endl<< "#\t\t\t\t\t\t\t\t\t#" << endl<< "#\t\t\t\t\t\t\t\t\t#" << endl<< "#\t\t\t\t\t\t\t\t\t#" << endl;
    cout << "#########################################################################" << endl;
    system("pause");
    start(menu);
    return 0;
}

int playeroneturn(int p1score);
{
    using namespace std;
    using std::cout;
    using std::cin;
    using std::endl;
    using std::srand;
    using std::rand;
    using std::time;
    using std::string;
    srand((unsigned int)time(0)); 
    int roll = 0;
    int p1score = 0;
    string reroll = "roll";
    while(reroll == "roll") 
    {
        roll = 1 + (rand() % 6);
            if(roll > 1)
            {
                p1score = p1score+roll;
                // using " in a string msdn.microsoft.com/en-us/library/267k4fw5.aspx
                cout << "You rolled a " << roll << endl << "Type roll to roll again or end to end your turn."; // error on cout this declaraton has no storage class or type specifier and error on first << expected a ;
                cin >>  reroll; 
            }
            else 
            { 
                cout >> "Bad luck! you rolled a 1, your turn is over and you score nothing!"
                p1score = 0; 
                reroll = end;
            }
    }
    return p1score; 
}

Upvotes: 0

Views: 7125

Answers (4)

Kiril Kirov
Kiril Kirov

Reputation: 38173

I see several issues (from my comment):

  • you cannot have statements like 1 + (rand() % 6); = roll. What is this supposed to do? If you want roll to be 1 + (rand() % 6);, write it roll = 1 + (rand() % 6);
  • you cannot use continue as variable, it's reserved keyword
  • you cannot mix >> and << when using cout. Use only <<.
  • p1score is not initialized (it's not compiler's error, but still - you use it (in the body of the if-statement, where the first condition (if(roll > 1)) is true) without being initialized, so you'll get garbage value
  • have you included #include <string>?
  • you don't have using std::string and if you don't have using namespace std;, you'll have a compiler error for this, too.

  • waiting for an answer - what is before the first {.


EDIT: Looking at you edit, you need to do some other changes:

  • first, the definition of playeroneturn is wrong: int playeroneturn(int p1score); { ... } <- remove ; before the function's body
  • note, that in playeroneturn, int p1score = 0; shadows the parameter of the function, it has the same name
  • again in playeroneturn - cout must be used with <<, not with >>
  • a ';' must be added after the wrong cout (from the previous point)
  • in playeroneturn (again), reroll = end; is bad - what is this end, it's not defined nowhere.

Upvotes: 8

Libin
Libin

Reputation: 86

continue is a keyword / reserved word. and hence it should not be used as a variable name or other literal specification.For feeling the difference just rename the variable 'continue' to 'continue1' and see the error go. Hope this will help.

Upvotes: 0

huseyin tugrul buyukisik
huseyin tugrul buyukisik

Reputation: 11926

while(continue == "roll")
{
    1 + (rand() % 6); = roll //<--- what is this? no ; and no l value
        if(roll > 1)

where is semicolon and lvalue?

Upvotes: 1

interjay
interjay

Reputation: 110192

continue is a keyword and may not be used as a variable name.

Upvotes: 3

Related Questions