expected ',' or ';' before '{' token

I know very little about code in general. I am watching a "all in one" video tutorial on C++, but I wanted to take the lesson of Switch statements above and beyond, and make a sort of game out of it - with the help of some RNG I found online, and recycled (twice I think). The aim of the game was to answer a series of questions and give a few other responses. Very simple, or so I thought. It had a bunch of errors, but without guidance and knowledge of what I was doing, I reduced it to just one. The one in the title. Line 12 char 1. Any help received is very appreciated. Thanks.
(by the way, I put as many #include things as I could in there. Like I said, I know very little and the video tutorial hasn't mentioned much about them or what they do. I figure they tell the console what to expect, or something. I could be very wrong, and I likely am. Thanks for putting up with my most likely terrible code in advance. Maybe in the future I will be better, but this is much different than scripting for New Vegas)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
 #include <vector>
#include <string>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

using namespace std;

int main(main)
{

    int playername;
    int greetOption;
    int responseOne;
    int responseTwo;
    bool yes;
    
    srand (time(NULL));    
    greetOption = rand() % 4 + 1;

    switch(greetOption){
        
        case 1:
            printf << "Hello there" << endl;
            break;
        case 2:
            printf << "Hi" << endl;
            break;
        case 3:
            printf << "Sup?" << endl;
            break;
        case 4:
            printf << "How's it going?" << endl;
            break;
    
    } // this section is meant to randomly generate a number, which is assigned to a specific greeting. 
    
    printf << "What is your name?" << endl;
    
    scanf ("%d", &playername);
    
    printf << "Oh, well hello there, " << playername << endl;
    
    printf << "Look, can you help me? I need to know what 5x5x5 is. Do you know?" << endl;
    
    do {
    
    scanf ("%d", &responseOne);
    if (responseOne == 125)
    {
    printf ("Thank you so much.");
    }
    else (responseOne != 125); printf ("That's not right.");
        printf << "Need some help? psst, I am testing you." << endl;
        scanf ("%d", &responseTwo);
        if (responseTwo == yes)
        {    
            printf ("The answer is 100 more than 25");
        }
        else (responseTwo == 125){
            printf ("That's right. Guess you didn't need help");
        }
        if (responseTwo == yes)
        {
        printf ("The answer is 100 more than 25");
        }
        else (responseTwo != yes){
            printf ("Suit yourself");
        }
        
    } // this is meant to have the player answer the question correctly. If answered incorrectly, the player should be given a hint
      // and the section should be repeated (so as not to skip the section after asking for help). Might need a loop here.

    int secretNum;
    int responseThree;
    
    printf << "You're doing well so far. Let's play one of my favorite games. Try to guess the number in my head. It is a number between 1 and 10" << endl;
             
    do {
    
      srand (time(NULL));
      secretNum = rand() % 10 + 1;
    
    scanf ("%d", &responseThree);
    if (secretNum>responseThree) printf ("Wrong. Lower.");
    else if (secretNum<responseThree) printf ("Wrong. Higher.");
    }while (secretNum=responseThree) printf ("Wow. You actually got it right. Good job");
    
    // this is meant to have a little number guessing game. A random number is generated and the player must guess it.
    // should they get it wrong, they are told "higher" or "lower" and must re-guess.
    
    int closing;
    
    printf << "You are a pretty cool person. I have to go now. Let's meet again sometime in the future." << endl;
    
    srand (time(NULL));
    closing = rand() % 4 + 1;
    
    switch(closing){
        
        case 1:
            printf << "See you later." << endl;
            break;
        case 2:
            printf << "Have a good one." << endl;
            break;
        case 3:
            printf << "Bye." << endl;
            break;
        case 4:
            printf << "It's been nice talking with you." << endl;
            break;
    }
    return 0;
  // like the greeting, this is meant to generate a number which is assigned to a closing. The closing will be printed
} // to screen and the game will be over. 
Last edited on
The function main takes (optionally) an integer, and array of pointers to characters, and possibly pointers to the environment. Just format line twelve to:

1
2
3

int main()
1. int main(main) // remove the main inside of ( )
2. Your if statements are messed up.

1
2
3
4
 if (responseTwo == yes) puts ("The answer is 100 more than 25");
 else (responseTwo != 125); puts ("That's right. Guess you didn't need help"); // remove semicollon after puts
 if (responseTwo == yes) puts ("The answer is 100 more than 25");
 else (responseTwo != yes); puts ("Suit yourself"); // same 


You should always use brackets, even if it is one line of code, it will save you a lot of errors later.

There shouldnt be semicolons after else.

1
2
3
4
5
6
7
8
9
10
11
12
13
 if (responseTwo == yes) 
{
    puts ("The answer is 100 more than 25");
} 
else (responseTwo != 125) {
     puts ("That's right. Guess you didn't need help");
}
if (responseTwo == yes) {
     puts ("The answer is 100 more than 25");
}
else (responseTwo != yes){ 
    puts ("Suit yourself"); 
}
Last edited on
There are many errors in your code: First, like mentioned above, your if statements. But the biggest error is here. In your outer Do ... While loop, you never put the imposing while statement at the end. If you didn't need one, why even make a loop? Second, with your:

1
2
3
4
5
6
7
8
9
10
do {
    
     srand (time(NULL));
     secretNum = rand() % 10 + 1;
    
     scanf ("%d", &responseThree);
     if (secretNum>responseThree) puts ("Wrong. Lower.");
     else if (secretNum<responseThree) puts ("Wrong. Higher.");
     while (secretNum=responseThree) puts ("Wow. You actually got it right. Good job");
}


You put the while condition in the wrong place. It should be after the
}
so your program's inner loop should look like:

1
2
3
4
5
6
7
8
9
do {
    
      srand (time(NULL));
      secretNum = rand() % 10 + 1;
    
    scanf ("%d", &responseThree);
    if (secretNum>responseThree) puts ("Wrong. Lower.");
    else if (secretNum<responseThree) puts ("Wrong. Higher.");
} while (secretNum=responseThree) puts ("Wow. You actually got it right. Good job");


Just as advice, don't mix around the header iostream and stdio.h. I recommend, that since you have scanf so much, instead of puts and cout, use printf. It basically works the exact same way as cout. Here is an example:

1
2
3
4
5
6
7
#include <stdio.h>

int main()
{
    printf("Hello, World")
    return 0;
}
Last edited on
I've fixed up my if statements. I learned to do them this way by scripting for Fallout New Vegas (as mentioned above). It was vastly different on account of having different, things. I don't know what they are called, functions or what not. I had "set" and "to", so it was very easy to manipulate inventories and stuff. I'm not sure how that would be accomplished if normal c++ doesn't have those, but that's not important right now.

I can't take main out of "int main(main)" because then, when I try to compile it gives me 10 errors instead of 1, asking for several "expected 'whatever' before 'whatever' token"'s instead of the one in the title, on line 12.

I also did what you said RUNNER, I changed the cout's and the puts's to printf, and took out the iostream header. I also never meant to make a loop. That code was part of the RNG I found and had recycled, and I didn't change it very much. It worked fine, so I assumed this would too.

I changed all of that and I still get the same error. Now if I take the main out of line 10 I get like 10 more errors, on top of the original 10 it seems (I've not counted nor checked to see if they are even different).
Last edited on
No one noticed that else cannot have a condition? It should be an else if instead.

I wouldn't recommend using C style printf scanf in C++ code. Either write C code or write C++ code. However I do recognise that it takes a while to transition from C to C++.

by the way, I put as many #include things as I could in there.


The way forward here, is to read the documentation - There are tutorials, reference and articles at the top left of this page. Read the reference material for the function or keyword you wish to use, and look at it's example code, it will tell you which header file to include. Only include the header files you need.

You also have problems with types. Your types are int, so that won't work well with playername you need to use std::string read up about that.

56
57
if (responseTwo == yes) puts ("The answer is 100 more than 25");
        else (responseTwo != 125); puts ("That's right. Guess you didn't need help");


The variable yes is a bool (effectively translated to a 0 or 1), responseTwo is an int so this isn't making sense with an input of 100 say.

Consider writing some pseudo code. Write comments in English for what you want to do. Start out as general as you like, then go back and add more detail until you are happy to convert the comments to code. It is a kind of "Divide & Conquer" approach. This will help in organising your thoughts, think about what variables and types are needed, what needs to be in a loop, whether a function is needed etcetera.

Also consider starting simple and write you own code, rather than adapting some one else's (for now anyway). Later when you have a better idea of what is going on, you can modify other code.

Good Luck !!
Last edited on
I'm not transitioning, at least, to my knowledge I'm not. I think I might need a loop in there around the responseTwo area because if the prog ever works I fear it may skip the chunk should the player get responseOne incorrect, and ask for help in responseTwo, since it goes up to down unless told to loop.

I added some comments to help generalize what I'm trying to do. It has helped to be able to understand the code quickly, and in a language I almost fully understand. I also appear to have found a nontechnical bug in the code. By that I mean, it would have worked fine, but not how I want it to. Line 57 says that if the player's answer isn't 125, which is the right answer, then the console gives them the message that they are right. Fixed now.

Your advice may help me write code in the future, but for now this code is already written. I would like to try and make this one work instead of rebuilding, because I am basing this off of the video tutorial I found on youtube. I wanted to make progs based on the sections of video he has dedicated to different things in C++, and switch statements was this objective. I was just using help from other sources, something I will avoid in the future if I can help it (until I have a better idea, like you said). I don't really want to skip ahead in the video if I can help that.
I can't take main out of "int main(main)" because then, when I try to compile it gives me 10 errors instead of 1, asking for several "expected 'whatever' before 'whatever' token"'s instead of the one in the title, on line 12.


At some point you will have to believe the the advice that is being given to you. Further, what was your motivation for doing such a thing? We are saying that is is plain wrong. The other 10 errors are still errors, and have mostly already been mentioned.

Your advice may help me write code in the future, but for now this code is already written.


I have heard this line lots of times before: essentially you would rather patch up some really bad code until it works; as opposed to learning to write it properly. Realise that just because code "works" (even if if you can get it to) it doesn't make it correct. The confusion over the types used shows that the design is confused.

..... because I am basing this off of the video tutorial I found on youtube.


And how do you know what you found on youtube is a good example of best practise with coding? Anyone can make a video on youtube: the "correctness" can vary wildly.

I added some comments to help generalize what I'm trying to do. It has helped to be able to understand the code quickly, and in a language I almost fully understand. I also appear to have found a nontechnical bug in the code. By that I mean, it would have worked fine, but not how I want it to. Line 57 says that if the player's answer isn't 125, which is the right answer, then the console gives them the message that they are right. Fixed now.


You haven't made the changes to the original post, are we supposed to guess what you have done? What you think is fixed, is a matter of opinion. I am not trying to be really harsh here, but you seem to have confidence in what you have done and how you are going about it, but I have some doubts about the correctness of your project.

I also did what you said RUNNER, I changed the cout's and the puts's to printf, and took out the iostream header.


So you are doing C code from now on then. Realise that C and C++ are quite different paradigms, some of the basic code is the same, but the entire way of thinking is very different.
Sorry if I seem a bit brash or hasty.

My motivation for the prog, or learning c++ (the one I want to learn, not c)? For the prog, I want to take the simple chunk of this video (chunks as in, the labeled parts in the description) https://www.youtube.com/watch?v=Rub-JsjMhWY and I want to take it a bit above and beyond the very simple examples he appears to give. For learning c++, well I aspire to make games. While I know that not all of game developing is code, I realize that it will help me with many things computer related. Also, a secret project I am working on (that at this rate will take years). Also, the 10 other errors I mentioned, are all pretty much the same thing. They want me to put a , or a ; before some (, ), or {, or } token, but when I do that it asks for prime expressions or unqualified-id's. With the code how it is now, I don't need any prime expressions or any symbols anywhere. Just the unqualified-id, whatever that is.

I suppose that I know enough about switch statements that I can just scrap this prog. I don't actually need to make it work, and with the patch job that it has now it would need to be re-written, but with all of the tips that I am getting the code is getting very complicated for someone with as little knowledge as I. Maybe when I get a better understanding I can come back, develop a style that will make it easier to read, and make proper working code. I have completed the goal, somewhat, of taking the video's parts and trying to expand them.

The video seems to be a good example/tutorial because he has many videos on many languages, and seems to be quite popular. If he wasn't doing something right, he wouldn't have any sort of following. Unless he were funny or something. I know I am guessing at it, but the information doesn't appear false, at least for now.

I should have been changing the post, but for some reason editing the original post hadn't crossed my mind. Schools have a way of taking critical thinking away from you, but they aren't solely to blame. I can edit it quickly enough, but I explained what I had changed to "fix" the "nontechnical" bug.

I know that the code is a different way of thinking, to paraphrase. I have never really seen C before, just some html, java, c#, and c++, as well as some others I probably don't know the name of. I have read that c++ was designed to be a sort of balance of two different ways to write code, but that was on a badly translated Italian - English webpage which could have been completely false. Maybe I should watch more of the video, see if I can get a grasp of it, and then mix concepts together. That way I could possibly avoid having to look up bits of code that would take up large chunks of my programs, and I would be able to write code without any "confusion," as stated above.
Sorry if I seem a bit brash or hasty.


Well you don't seem to be taking on board the advice being given, or at least I don't see any evidence that you have. Makes me wonder if it is worth replying at all.

.... and I want to take it a bit above and beyond the very simple examples he appears to give.


From the code you have posted, you don't have a grasp of the basics yet, let alone extending anything.

I suppose that I know enough about switch statements that I can just scrap this prog. I don't actually need to make it work, and with the patch job that it has now it would need to be re-written, but with all of the tips that I am getting the code is getting very complicated for someone with as little knowledge as I. Maybe when I get a better understanding I can come back, develop a style that will make it easier to read, and make proper working code. I have completed the goal, somewhat, of taking the video's parts and trying to expand them.


Really the only way to learn is by fixing up the problems you have attempted, and learn the basics properly. Your assertion that you have somewhat completed the goal, is a matter of opinion, and you saying that you have described what you have done to fix it, doesn't mean you have done it correctly.

I get the distinct impression that you are looking at videos, and trying to do code by trial and error, without a basic understanding. Placement of semicolons and braces are very important in C and C++, as is the syntax in any programming language. IMO you would be better to get a good text book and learn from that. And look at the reference material and examples on this page as I mentioned earlier.

http://www.codergears.com/QACenter/index.php?qa=resources


Realise the difference between C & C++ :

http://www.differencebetween.com/difference-between-c-and-c/
There is one good website to learn the basics of C++:
http://www.learncpp.com
Learn cpp is really good, I used that before cplusplus.com

@TheIdeaMan
You are completely right about the C and C++ differentiation.

@Scary Necropolis
Use cout, like you did in some cases, instead of printf,or puts.
Also, instead of scanf, use getline for strings, and cin for other data types. Here is an example for both

cin:
1
2
3
4
5
6
7
8
9
10
#include <iostream>

int main()
{
    int a;
    std::cout << "Enter a number! " << std::endl;
    std::cin >> a;
    std::cout << "Your number is " << a;
    return 0;
}


getline:
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <string>
int main()
{
    std::string a;
    std::cout << "Enter some text! " << std::endl;
    getline(std::cin,a);
    std::cout << "Your text is " << a;
    return 0;
}
Topic archived. No new replies allowed.