LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-19-2012, 04:20 PM   #1
carlosk711
Member
 
Registered: Sep 2012
Posts: 52

Rep: Reputation: Disabled
I'm Making Nim, the game, and I was wondering where my syntax errors are??


Thanks for all the help!!!

Last edited by carlosk711; 10-21-2012 at 08:57 PM. Reason: Completely redid it all
 
Old 10-19-2012, 07:49 PM   #2
carlosk711
Member
 
Registered: Sep 2012
Posts: 52

Original Poster
Rep: Reputation: Disabled
It's just syntax I believe
 
Old 10-20-2012, 09:55 AM   #3
carlosk711
Member
 
Registered: Sep 2012
Posts: 52

Original Poster
Rep: Reputation: Disabled
can someone at least tell why it says expected primary expression before else? is it something with brackets?
 
Old 10-20-2012, 10:14 AM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Arch
Posts: 3,077

Rep: Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268
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.
 
Old 10-20-2012, 02:17 PM   #5
carlosk711
Member
 
Registered: Sep 2012
Posts: 52

Original Poster
Rep: Reputation: Disabled
I'm not quite sure how to fix my mistakes now
and I didn't write them all separately
 
Old 10-20-2012, 02:24 PM   #6
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
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,
 
Old 10-20-2012, 02:31 PM   #7
carlosk711
Member
 
Registered: Sep 2012
Posts: 52

Original Poster
Rep: Reputation: Disabled
Right, I got that, but I how do I fix the compiler errors at hand?
 
Old 10-20-2012, 02:36 PM   #8
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
...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();
    }
...
 
Old 10-20-2012, 02:37 PM   #9
carlosk711
Member
 
Registered: Sep 2012
Posts: 52

Original Poster
Rep: Reputation: Disabled
so is that what it's supposed to look like?
 
Old 10-20-2012, 02:41 PM   #10
carlosk711
Member
 
Registered: Sep 2012
Posts: 52

Original Poster
Rep: Reputation: Disabled
Changed

Last edited by carlosk711; 10-21-2012 at 08:57 PM. Reason: Changed
 
Old 10-20-2012, 02:52 PM   #11
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
Um... The changes you made are nothing to do with the proper layout of the if statement, as I suggested. Try doing that...
 
Old 10-20-2012, 03:06 PM   #12
carlosk711
Member
 
Registered: Sep 2012
Posts: 52

Original Poster
Rep: Reputation: Disabled
It's still not working
 
Old 10-20-2012, 03:12 PM   #13
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
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.
 
Old 10-20-2012, 03:15 PM   #14
carlosk711
Member
 
Registered: Sep 2012
Posts: 52

Original Poster
Rep: Reputation: Disabled
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
 
Old 10-20-2012, 03:21 PM   #15
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
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...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
syntax errors connect2janu Programming 1 04-09-2012 07:11 PM
Making a Game, anyone want to help? Newtonx Linux - Games 15 01-19-2007 04:49 AM
"NIM thread blocked" & "Deadman Switch (DMS) close to trigger" errors sosborne Linux - Networking 1 06-28-2006 02:07 PM
"NIM thread blocked" & "Deadman Switch" errors sosborne AIX 3 06-01-2006 11:21 AM


All times are GMT -5. The time now is 04:32 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration