Writing, testing/compiling, and running C++ in linux (Ubuntu)

C

Cybex

I am taking a C++ class and would like to use a Linux based IDE vs C+
+ .Net but I am a little lost in how to go about this. I think that
the IDE and compilation tools are separate under Linux. I have tried
to take code written and tested under C++ .Net and tried compiling it
in Linux via gcc but I keep getting errors.

The code:
#include <iostream>

using namespace std;

int main()
{
//declare variables
double score1, score2, score3, average;

//enter inputs
cout << "Enter score for first test: ";
cin >> score1;
cout << "Enter score for second test: ";
cin >> score2;
cout << "Enter score for third test: ";
cin >> score3;

//display outputs
if (score1 >= 0 && score2 >= 0 && score3 >= 0)
{
average = (score1 + score2 + score3) / 3;
cout << "The average for the three test is: "
<< average << "\n";
}
else
cout << "You must enter a score for each test"
<< "equal to or greater than zero." << "\n";

return 0;
} //end of main function

The errors:

user@user1:~$ gcc -o /home/user/test.out /home/user/testing.c
/home/user/testing.c:1:20: error: iostream: No such file or directory
/home/user/testing.c:3: error: expected '=', ',', ';', 'asm' or
'__attribute__' before 'namespace'
/home/user/testing.c: In function 'main':
/home/user/testing.c:11: error: 'cout' undeclared (first use in this
function)
/home/user/testing.c:11: error: (Each undeclared identifier is
reported only once
/home/user/testing.c:11: error: for each function it appears in.)
/home/user/testing.c:12: error: 'cin' undeclared (first use in this
function)

Any help on how I can accomplish my assignments in Linux vs Windows
would be appreciated. I am having to use VMWare now in order to use
the C++ >Net IDE.

James
 
B

benben

Cybex said:
I am taking a C++ class and would like to use a Linux based IDE vs C+
+ .Net but I am a little lost in how to go about this. I think that
the IDE and compilation tools are separate under Linux. I have tried
to take code written and tested under C++ .Net and tried compiling it
in Linux via gcc but I keep getting errors.

The code:
#include <iostream>

using namespace std;

int main()
{
//declare variables
double score1, score2, score3, average;

//enter inputs
cout << "Enter score for first test: ";
cin >> score1;
cout << "Enter score for second test: ";
cin >> score2;
cout << "Enter score for third test: ";
cin >> score3;

//display outputs
if (score1 >= 0 && score2 >= 0 && score3 >= 0)
{
average = (score1 + score2 + score3) / 3;
cout << "The average for the three test is: "
<< average << "\n";
}
else
cout << "You must enter a score for each test"
<< "equal to or greater than zero." << "\n";

return 0;
} //end of main function

The errors:

user@user1:~$ gcc -o /home/user/test.out /home/user/testing.c
/home/user/testing.c:1:20: error: iostream: No such file or directory
/home/user/testing.c:3: error: expected '=', ',', ';', 'asm' or
'__attribute__' before 'namespace'
/home/user/testing.c: In function 'main':
/home/user/testing.c:11: error: 'cout' undeclared (first use in this
function)
/home/user/testing.c:11: error: (Each undeclared identifier is
reported only once
/home/user/testing.c:11: error: for each function it appears in.)
/home/user/testing.c:12: error: 'cin' undeclared (first use in this
function)

not sure but looks like the compiler was compiling C code not C++
(mistakenly.) Why not try g++ instead of gcc.
Any help on how I can accomplish my assignments in Linux vs Windows
would be appreciated. I am having to use VMWare now in order to use
the C++ >Net IDE.

For a standard ISO C++ program any tool would be fine.

Ben
 
C

Cybex

That worked like a charm! Thanks

gcc was the only tool I found reference to in a Linux for programmers
book that talked about C/C++. So doing things this way should work
right? Is C++ the same across the two platforms? This is an intro to
C++ so I doubt we will get very far beyond the basics.

James
 
L

Larry Smith

Cybex said:
That worked like a charm! Thanks

gcc was the only tool I found reference to in a Linux for programmers
book that talked about C/C++. So doing things this way should work
right? Is C++ the same across the two platforms? This is an intro to
C++ so I doubt we will get very far beyond the basics.

James

The "GCC" Collection includes the "gcc" compiler for C and
the "g++" compiler for C++. Always compile -and- link
C++ programs using "g++".
 
L

Larry Smith

Cybex said:
That worked like a charm! Thanks

gcc was the only tool I found reference to in a Linux for programmers
book that talked about C/C++. So doing things this way should work
right? Is C++ the same across the two platforms? This is an intro to
C++ so I doubt we will get very far beyond the basics.

James

The newsgroup for the GCC 'C' compiler is

gnu.gcc.help

The newsgroup for the GCC 'C++' compiler is

gnu.g++.help
 
A

Alexander.Zen

The newsgroup for the GCC 'C' compiler is

gnu.gcc.help

The newsgroup for the GCC 'C++' compiler is

gnu.g++.help

Be sure your GCC installation is correct, also , change your file name
test.c to test.cpp
 
J

Jacek Dziedzic

Cybex said:
I am taking a C++ class and would like to use a Linux based IDE vs C+
+ .Net but I am a little lost in how to go about this. I think that
the IDE and compilation tools are separate under Linux. I have tried
to take code written and tested under C++ .Net and tried compiling it
in Linux via gcc but I keep getting errors.

The code:
#include <iostream>

using namespace std;

int main()
{
//declare variables
double score1, score2, score3, average;

//enter inputs
cout << "Enter score for first test: ";
cin >> score1;
cout << "Enter score for second test: ";
cin >> score2;
cout << "Enter score for third test: ";
cin >> score3;

//display outputs
if (score1 >= 0 && score2 >= 0 && score3 >= 0)
{
average = (score1 + score2 + score3) / 3;
cout << "The average for the three test is: "
<< average << "\n";
}
else
cout << "You must enter a score for each test"
<< "equal to or greater than zero." << "\n";

return 0;
} //end of main function

The errors:

user@user1:~$ gcc -o /home/user/test.out /home/user/testing.c
/home/user/testing.c:1:20: error: iostream: No such file or directory
/home/user/testing.c:3: error: expected '=', ',', ';', 'asm' or
'__attribute__' before 'namespace'
/home/user/testing.c: In function 'main':
/home/user/testing.c:11: error: 'cout' undeclared (first use in this
function)
/home/user/testing.c:11: error: (Each undeclared identifier is
reported only once
/home/user/testing.c:11: error: for each function it appears in.)
/home/user/testing.c:12: error: 'cin' undeclared (first use in this
function)

Any help on how I can accomplish my assignments in Linux vs Windows
would be appreciated. I am having to use VMWare now in order to use
the C++ >Net IDE.

Rename to testing.cpp, compile with g++. Otherwise you risk
the compiler thinking it's C, not C++.

HTH,
- J.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,743
Latest member
WoodrowMea

Latest Threads

Top