1

Today, after Slackware 13.37 installation, i've got the problem: default GCC 4.5.2 cannot compile my code. Now I study C++ by the Stephen Davis's book "C++ for dummies" and want to compile this:

#include <stdio.h>
#include <iostream.h>

int main(int nNumberofArgs, char* pszArgs[])
{

int nNCelsius;
cout << "Celsisus: ";
cin >> nNCelsius;

int nNFactor;
nNFactor = 212 - 32;

int nFahrenheit;
nFahrenheit = nNFactor * nNCelsius / 100 + 32;

cout << "Fahrenheit: ";
cout << nFahrenheit;

return 0;
}

But my GCC 4.5.2 gives these errors:

FahTCel.cpp:7:14: error: expected ')' before ';' token
FahTCel.cpp:7:14: error: 'main' declared as function returning a function
FahTCel.cpp:8:1: error: 'cout' does not name a type
FahTCel.cpp:9:1: error: 'cin' does not name a type
FahTCel.cpp:12:1: error: 'nNFactor' does not name a type
FahTCel.cpp:15:1: error: 'nFahrenheit' does not name a type
FahTCel.cpp:17:1: error: 'cout' does not name a type
FahTCel.cpp:18:1: error: 'cout' does not name a type
FahTCel.cpp:20:1: error: expected unqualified-id before 'return'
FahTCel.cpp:21:1: error: expected declaration before '}' token
linksoed
  • 13
  • 1
  • 4
  • 4
    Don't skip the first error message... you misspelled `#include ` (rather badly, actually). Next, [get a book that's up to date with at least the first version of the C++ standard](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Ben Voigt Jun 15 '12 at 04:54
  • 3
    I've never seen ``. All you should need to include is ``. That book doesn't look like it has the best coding style. – chris Jun 15 '12 at 04:55
  • Check this is correct **#include **, if not change. – tuxuday Jun 15 '12 at 04:57
  • I am curious when this book was published and which edition it is. It must be a bit old (well, old in computer years...) – Dietrich Epp Jun 15 '12 at 05:13

2 Answers2

5

Three errors:

  1. The correct header is <iostream>. This program requires no other headers.

  2. You must either put using namespace std; in the file, or refer to std::cout and std::cin explicitly. Take your pick, plenty of C++ programmers disagree about which of the two options is better. (You could also bring just cin and cout into your namespace, if you wanted.)

  3. The program does not write a line terminator at the end. This will cause the output to "look bad" on most terminals, with the command prompt appearing on the same line as the output. For example:

Here are the corrections:

#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
    ...
    cout << nFahrenheit << '\n';
    ...
}

Note: It is extremely unusual to see main take parameters with names other than argc and argv. Changing the names just makes it harder for other people to read your code.

Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415
1

its std::cout or you should add using namespace std;

and the include should be < iostream> not < ionstream.h>.

Mohammad
  • 1,253
  • 1
  • 10
  • 26