trying to compile a program in C++ gives me a bunch of errors ( Total N00b)
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
trying to compile a program in C++ gives me a bunch of errors ( Total N00b)
hey, I am brand new to c++ and trying to learn it and have it work on Linux.
I tried to compile a basic program via command line ( copied it from a book ) :
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int nNumberofArgs, char* pszArgs[])
{ //enter celsius
int celsius;
cout << "enter the Celsius here:";
cin >> celsius;
//Calculate conversion factor
int factor;
factor = 212 - 32;
//use conversion factor
int fahrenheit;
fahrenheit = factor * celsius / 200 + 32;
//output it all
cout << "in °F:";
cout << fahrenheit << endl ;
//wait for termination
system ("PAUSE");
return 0; #include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int nNumberofArgs, char* pszArgs[])
{ //enter celsius
int celsius;
cout << "enter the Celsius here:";
cin >> celsius;
//Calculate conversion factor
int factor;
factor = 212 - 32;
//use conversion factor
int fahrenheit;
fahrenheit = factor
}
so I try to compile it, but I get:
ME@UBUNTU:~/Desktop$ gcc -o degrees celcius.cpp
celcius.cpp:23: error: stray ‘#’ in program
celcius.cpp: In function ‘int main(int, char**)’:
celcius.cpp:23: error: ‘include’ was not declared in this scope
celcius.cpp:23: error: ‘cstdio’ was not declared in this scope
celcius.cpp:26: error: expected primary-expression before ‘using’
celcius.cpp:26: error: expected `;' before ‘using’
celcius.cpp:29: error: a function-definition is not allowed here before ‘{’ token
celcius.cpp:40: error: expected `}' at end of input
Why is it doing this and what am I doing wrong?
another question: Are there differences between scripting c++ programs on Windows and scripting on Linux? because oftentimes some programs I want to write won't compile on Linux...I get a bunch of errors, but they compile on the windows IDE I use.
like this?
~/Desktop$ g++ celcius.cpp
celcius.cpp:1: error: stray ‘#’ in program
celcius.cpp:2: error: stray ‘#’ in program
celcius.cpp:3: error: stray ‘#’ in program
celcius.cpp:7:1: error: invalid suffix "int" on floating constant
celcius.cpp:1: error: expected unqualified-id before numeric constant
celcius.cpp:5: error: expected unqualified-id before numeric constant
If you are going to compile a C program use gcc. But if you're going compile a C++ program then you can use g++. It will give you errors if you're going to compile a C program using g++ or vice versa. :/
~/Desktop$ g++ celcius.cpp
celcius.cpp:1: error: stray ‘#’ in program
celcius.cpp:2: error: stray ‘#’ in program
celcius.cpp:3: error: stray ‘#’ in program
celcius.cpp:7:1: error: invalid suffix "int" on floating constant
celcius.cpp:1: error: expected unqualified-id before numeric constant
celcius.cpp:5: error: expected unqualified-id before numeric constant
Ok, I'm guessing you're learning C++ coming from a BASIC background. Is that right? The reason I ask is because the errors you report suggest your source code actually contains the line numbers at the beginning of each line. In other words, you're trying to compile exactly what you posted previously. As I recall, the BASIC I learned used line numbers. In C/C++ don't use them. The code you should be compiling is the following (with spacing in the way I prefer it):
Code:
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int nNumberofArgs, char* pszArgs[])
{
//enter celsius
int celsius;
cout << "enter the Celsius here:";
cin >> celsius;
//Calculate conversion factor
int factor;
factor = 212 - 32;
//use conversion factor
int fahrenheit;
fahrenheit = factor * celsius / 200 + 32;
//output it all
cout << "in °F:";
cout << fahrenheit << endl ;
//wait for termination
system ("PAUSE");
return 0;
}
My suggestion was to add line numbers when posting questions about the code. Then you can refer to lines by their linenumber. An editor like kate and vim can display line numbers without them being part of the file.
I know this thread has been taken care of. I just wanted to respond really quick...
Quote:
Originally Posted by jschiwal
My suggestion was to add line numbers when posting questions about the code. Then you can refer to lines by their linenumber. An editor like kate and vim can display line numbers without them being part of the file.
I honestly don't remember if I saw that suggestion in my original reading. I got thrown off looking at the error messages being reported. Specifically:
Quote:
celcius.cpp:7:1: error: invalid suffix "int" on floating constant
That error suggested to me that the line numbers, including the period, were in the file as well. The line numbers, with a trailing period, would be a floating point constant, and would also explain the "stray `#'" errors.
My apologies for not reading things as closely as I should have.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.