LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   I'm Making Nim, the game, and I was wondering where my syntax errors are?? (https://www.linuxquestions.org/questions/linux-newbie-8/i%27m-making-nim-the-game-and-i-was-wondering-where-my-syntax-errors-are-4175433123/)

carlosk711 10-19-2012 04:20 PM

I'm Making Nim, the game, and I was wondering where my syntax errors are??
 
Thanks for all the help!!!

carlosk711 10-19-2012 07:49 PM

It's just syntax I believe

carlosk711 10-20-2012 09:55 AM

can someone at least tell why it says expected primary expression before else? is it something with brackets?

ntubski 10-20-2012 10:14 AM

The version of gcc I'm using (4.7.1) gives clearer error messages:
Code:

make nim.o
g++ -Wall -Wextra -Wformat=2 -g -ansi -pedantic -std=c++98  -c -o nim.o nim.cc
nim.cc: In function ‘int main()’:
nim.cc:29:13: error: ‘else’ without a previous ‘if’
nim.cc:31:13: error: ‘else’ without a previous ‘if’
nim.cc:36:13: error: ‘else’ without a previous ‘if’
nim.cc:38:13: error: ‘else’ without a previous ‘if’
nim.cc:43:13: error: ‘else’ without a previous ‘if’
nim.cc:45:13: error: ‘else’ without a previous ‘if’
nim.cc:49:13: error: expected ‘)’ before ‘cout’
nim.cc:58:17: error: ‘else’ without a previous ‘if’
nim.cc:65:17: error: ‘else’ without a previous ‘if’
nim.cc:72:17: error: ‘else’ without a previous ‘if’
make: *** [nim.o] Error 1

And indeed your ifs aren't connected to your elses (remember this isn't python, indentation doesn't affect the meaning). Also, you don't have a closing brace for your main() function.

Did you write up the whole program and only then try to compile it? It's much easier to build up a program in small steps, ensuring every step is correct.

carlosk711 10-20-2012 02:17 PM

I'm not quite sure how to fix my mistakes now
and I didn't write them all separately

Snark1994 10-20-2012 02:24 PM

So what ntubski was suggesting was to start from the beginning with a very small program that compiles, e.g.:

Code:

#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;
void remove (int, int, int);
void generate (int, int, int, int );
void print (int, int, int);
int main () {
    int turn=0;
    int heap, deduct, row1, row2, row3, total;
    srand (time(NULL));
    row1 = 0;
    row2 = 0;
    row3 = 0;
    total = 0;
    generate (row1, row2, row3, total);
    print (row1, row2, row3);
    return 0;
}

void generate (int& row1, int& row2, int& row3, int& total) {
    row1 = ((rand() % 10) + 1);
    row2 = ((rand() % 10) + 1);
    row3 = ((rand() % 10) + 1);
    total = row1 + row2 + row3;
}

void print (int row1, int row2, int row3) {
    cout << "1: ";
    for (int i=0; i < row1; i++) {
        cout << "*";
    }
    cout << "2: ";
    for (int i=0; i < row2; i++) {
        cout << "*";
    }
    cout << "3: ";
    for (int i=0; i < row3; i++) {
        cout << "*";
    }
}

Does this compile? Does it do what is expected?

Then add another chunk of the original programme - and ask the same questions: does it compile? does it do what is expected?

Lather, rinse, repeat...

Hope this helps,

carlosk711 10-20-2012 02:31 PM

Right, I got that, but I how do I fix the compiler errors at hand?

Snark1994 10-20-2012 02:36 PM

...by starting with a small program that works and compiles, and building up. Honestly, that's the only way you're going to learn to write C++ right, if you'll allow me to turn a phrase.

However, almost all of your errors will be solved by putting a '{' after the ')' of an 'if' statement, and a '}' at the end of the block (normally before the 'else'). For example:

Code:

...
    if(myBool == true){
        doThing1();
        foo = bah;
        doThing2();
    } else {
        doOtherThing();
    }
...


carlosk711 10-20-2012 02:37 PM

so is that what it's supposed to look like?

carlosk711 10-20-2012 02:41 PM

Changed

Snark1994 10-20-2012 02:52 PM

Um... The changes you made are nothing to do with the proper layout of the if statement, as I suggested. Try doing that...

carlosk711 10-20-2012 03:06 PM

It's still not working

Snark1994 10-20-2012 03:12 PM

Please, for the love of Cthulhu, start with something small (like the code I posted in this post) and work up to the whole thing. There are so many syntax errors in your code that you are not going to be able to fix it without a much better knowledge of C++ than you currently have. If you start small, then any errors you have should be far more easily fixable than the code you currently have.

carlosk711 10-20-2012 03:15 PM

I'm sorry, I didn't mean to irritate you, I'm just frustrated at this point and don't have enough patience to break the code up

Snark1994 10-20-2012 03:21 PM

And I'm sorry, I shouldn't have got irritated either. I'm going to put on some laundry, and when I get back I will fix part of your code for you so you can see what we're aiming for...


All times are GMT -5. The time now is 06:27 AM.