LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-12-2007, 10:22 PM   #1
Ryupower
Member
 
Registered: Oct 2006
Posts: 75

Rep: Reputation: 16
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.

Last edited by Ryupower; 06-12-2007 at 10:35 PM.
 
Old 06-12-2007, 10:39 PM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Your program has two main functions. You seemed to have cat'ed two programs into one.

Also, it would be easier for others looking at your code if you enclosed it in code blocks and added line numbers.
E.G. "cat -n celcius.cpp"
Code:
     1  #include <cstdio>
     2  #include <cstdlib>
     3  #include <iostream>
     4  using namespace std;
     5
     6  int main(int nNumberofArgs, char* pszArgs[])
     7  { //enter celsius
     8  int celsius;
     9  cout << "enter the Celsius here:";
    10  cin >> celsius;
    11
    12  //Calculate conversion factor
    13  int factor;
    14  factor = 212 - 32;
    15  //use conversion factor
    16  int fahrenheit;
    17  fahrenheit = factor * celsius / 200 + 32;
    18  //output it all
    19  cout << "in °F:";
    20  cout << fahrenheit << endl ;
    21  //wait for termination
    22  system ("PAUSE");
    23  return 0;
    24  }

Last edited by jschiwal; 06-12-2007 at 10:43 PM.
 
Old 06-13-2007, 01:30 PM   #3
Ryupower
Member
 
Registered: Oct 2006
Posts: 75

Original Poster
Rep: Reputation: 16
ah I see, lol.

Very stupid question. XD
Sorry about that.

I fixed that, made sure I only had one program:

Code:
1. #include <cstdio>
2. #include <cstdlib>
3. #include <iostream>
4 using namespace std;
5. int main(int nNumberofArgs, char* pszArgs[])
6. { //enter celsius 
7. int celsius;
8. cout << "enter the Celsius here:";
9. cin >> celsius;
10. //Calculate conversion factor
11. int factor;
12. factor = 212 - 32;
13. //use conversion factor
14. int fahrenheit;
15. fahrenheit = factor * celsius / 200 + 32;
16.//output it all
17. cout << "in °F:";
18. cout << fahrenheit << endl ;
19. //wait for termination
20. system ("PAUSE");
21. return 0;
22. }

and I get:
Code:
/Desktop$ gcc -o degrees celcius.cpp
/tmp/cc4NALEN.o: In function `__static_initialization_and_destruction_0(int, int)':
celcius.cpp:(.text+0x23): undefined reference to `std::ios_base::Init::Init()'
/tmp/cc4NALEN.o: In function `__tcf_0':
celcius.cpp:(.text+0x6c): undefined reference to `std::ios_base::Init::~Init()'
/tmp/cc4NALEN.o: In function `main':
celcius.cpp:(.text+0x8e): undefined reference to `std::cout'
celcius.cpp:(.text+0x93): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
celcius.cpp:(.text+0xa1): undefined reference to `std::cin'
celcius.cpp:(.text+0xa6): undefined reference to `std::basic_istream<char, std::char_traits<char> >::operator>>(int&)'
celcius.cpp:(.text+0xe5): undefined reference to `std::cout'
celcius.cpp:(.text+0xea): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
celcius.cpp:(.text+0xf8): undefined reference to `std::cout'
celcius.cpp:(.text+0xfd): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(int)'
celcius.cpp:(.text+0x105): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
celcius.cpp:(.text+0x10d): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))'
/tmp/cc4NALEN.o:(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status



I must be doing something wrong... AGAIN...

And are there differences in c++ between linux and windows?

thanks guys for having patience with me.

Last edited by Ryupower; 06-13-2007 at 01:41 PM.
 
Old 06-13-2007, 03:44 PM   #4
Millenniumman
Member
 
Registered: Apr 2006
Posts: 81

Rep: Reputation: 15
You need to compile it with g++
 
Old 06-13-2007, 06:09 PM   #5
Ryupower
Member
 
Registered: Oct 2006
Posts: 75

Original Poster
Rep: Reputation: 16
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
 
Old 06-13-2007, 10:09 PM   #6
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
try:
Code:
g++ celcius.cpp -o celcius
then run './celcius' from the directory you compiled it in.
 
Old 06-15-2007, 12:46 PM   #7
Israfel2000
Member
 
Registered: May 2004
Location: Underground base in the mountains
Distribution: FreeBSD, Fedora, Ubuntu
Posts: 87
Blog Entries: 2

Rep: Reputation: 18
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. :/
 
Old 06-15-2007, 02:57 PM   #8
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Quote:
Originally Posted by Ryupower
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
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;
}
Compile it with the command nadroj gave you:
Code:
g++ celcius.cpp -o celcius
 
Old 06-16-2007, 06:41 PM   #9
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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.
 
Old 06-16-2007, 07:36 PM   #10
Ryupower
Member
 
Registered: Oct 2006
Posts: 75

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by nadroj
try:
Code:
g++ celcius.cpp -o celcius
then run './celcius' from the directory you compiled it in.

:O
It worked! Without error it compiled! The program calculated wrong...but that's an error on my part.
Thanks. ^^

Thanks to the rest too.
 
Old 06-21-2007, 11:22 AM   #11
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
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.
 
  


Reply

Tags
c++, cpp, error, linux



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Yes, total approval n00b p33kabo0 LinuxQuestions.org Member Intro 2 05-21-2006 10:59 PM
Total n00b to linux TriggerHappy Linux - Software 1 02-27-2004 10:04 AM
Compile program errors RH9 sallas Linux - Newbie 0 01-30-2004 08:38 PM
Total n00b from windows GamesMasta Linux - Newbie 6 06-23-2003 01:49 PM
Total N00b question ne21 Slackware 7 06-21-2003 09:48 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration