REJEN RJ
REJEN RJ

Reputation: 19

How to fix"Error- expected initializer before 'const'" in C++

Sorry, if you think I am stupid ( I am stupid ) but i don't know what's the problem? I try do something , and I can't solve this problem , I hope you can help me ...

  #include <iostream>
   using namespace std;  
      int main( int argc, const char * argv[] ) {
           cout << "Hello World! and this is my first program I will write myself, and i imaginate it, like who can talk with somebody use only 
replicas what I wrote in her. \n";
int answer 
const  answer = "num1 + num2 + num3"
int  num1 = 54
int  num2 = -63
int  num3 = +8
cout << num1 + num2 + num3 = "answer"
cin.get () ;
return 0; 
}

Upvotes: 1

Views: 11449

Answers (1)

Jrei Dimaano
Jrei Dimaano

Reputation: 36

Welcome to stack overflow, and that's okay we all started coding or C++ and have been in your shoes!

The first issue I see is that you didn't include semi-colons at the end of all your statements. You included it when you printed "hello world" and the last few lines of main, but forgot to add them after all your variable declarations.

The other issue is that you didn't finish specifying the type for your variable "answer". The const keyword is used together with a variable type to indicate that the variable itself is constant, const alone is not a type. In this case, you will want to make it "const string" or "const char *".

Another thing to note is that, although not required, adding consistent spacing and/or tabs to your code will make it more readable for you and other people working on the code. The standard format for c++ function definitions are usually as follows:

int main(){
    //insert code here
    return 0;
}

although some people would even prefer the brackets having a line of their own as such:

int main()
{
    //insert code here
    return 0;
}

Format doesn't effect performance but it helps us understand and share code. Happy coding!

Edit: The line where you state cout << num1 + num2 + num3 = "answer" is a bit fishy and will most likely throw errors. I'm not sure what you're trying to do here but I'd take a look at that line as well.

Upvotes: 1

Related Questions