Brian Brian - 1 year ago 56
C++ Question

What did I declare wrong here?

Im sure this is a simple question, but its had me stuck for hours.

#include <iostream>
using namespace std;

int main()
char name = mark;
int id = 0410448;

cout << name << " " << id << endl;


It is giving me an error like I've declared them wrong. Any help would be appreciated.

Answer Source

On this piece of code you have 4 errors:

  • include should be:

    #include <iostream>

    • Why? You have to specify which header file you have to use with a preprocessor directive. Although that probably happened because of the way SO formats posts...
  • char name = markshould be char* name = "mark"; or char name[] = "mark";

    • Why? A char can only hold 1 single character, so you need a collection of characters to store your complete string "mark", which has to be in double quotes to make it a string and not another variable's name.
  • int id = 0410448; should be int id = 410448;

    • Why? If you start a number with a leading zero it means it is an octal (base8) number, and this numbers only go from 0 to 7, as opposed to decimal, which go from 0 to 9 or hexadecimal, which go from 0 to F.
  • return0 should be return 0;

    • Why? return0 is a variable name, but you never specified a definition for it nor a value, what you essentially wanted there is to use the return keyword with a value of 0 to indicate the program ended successfully.