0

enter image description here

I get this error when ever I try to run or compile the file.

It's coded in c++ i used Code::Blocks to code it.

Can anyone please help me?

Tony Tannous
  • 14,154
  • 10
  • 50
  • 86
Chinceapizza
  • 1
  • 1
  • 1

2 Answers2

1

Make sure you are using a compiler for C++, like g++. I noticed that the extension of your file is .c and maybe Codeblocks uses a C compiler, like gcc. If codeblock is using gcc the error is normal because C doesn't provide any standard library called "iostream".

Please check the compiler in the project settings and change the extension of the file to .cpp.

  • I'm using GNU GCC compiler – Chinceapizza Jun 13 '20 at 21:41
  • @Chinceapizza gcc is the wrong compiler. You want g++ to compile c++ code. – user4581301 Jun 13 '20 at 21:59
  • @Chinceapizza I think you have chosen the wrong setting when initializing the project. You can create a New Project > Console Application > Go > **Language you want to use: select C++** > Project Tile > etc... – Andrea Grillo Jun 13 '20 at 22:08
  • @user4581301 GNU GCC is the default compiler for Code::Blocks, and is valid for both C and C++. It is the correct compiler for this task. https://stackoverflow.com/questions/172587/what-is-the-difference-between-g-and-gcc – Guy Keogh Jun 13 '20 at 23:48
  • @GuyKeogh The confusion stems from the fact that both compilers are part of the Gnu Compiler Collection (GCC). gcc-the-program is a C compiler. g++ compiles c++. If you use gcc to compile C++ code, the questioner's results are exactly what you should expect because gcc will go looking for the c library header files, not the c++ library headers. – user4581301 Jun 14 '20 at 00:03
0

Code::Blocks requires all files (including a single .c/.cpp file) to be within a project to compile properly, as it needs a location to store the object (.o) and binary (.exe) files. To do this, click "File" in the top left, "New", "Project". Create the file in this project.

Your code will still not compile as-is, however. Here is how the code is corrected:

  1. iostream is exclusive to C++, so you'll need to create a .cpp file, not a .c file
  2. \n to create a new line must be within the string, e.g. "what's your first number \n". It cannot be outside the string.
  3. A \nwith a backwards slash, rather than a /n is required for a line break.
  4. cin << b\n implies you are trying to divide 'b' with a nonexistant 'n'. The << operator also implies you are trying to pass b to cin, when in fact you are trying to pass the input of cin to b which requires >>. The correct way of writing this is cin >> b;. The line break \n can instead be placed at the end of the previous cout string.
Guy Keogh
  • 549
  • 2
  • 5
  • 15