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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
|
07-10-2006, 05:42 PM
|
#1
|
Senior Member
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662
Rep:
|
I ran the following program.
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
cout << "prints N times the message you want" << endl;
int count=0;
cout << "How much times do you want to write it?" << endl;
cin >> count;
cout << "What do you want to write?" << endl;
char what[10];
cin >> what;
for (int i=0; i<=count; ++i) {
cout << what << endl;
}
return 0;
}
--------------------------------------------------------------
The name of the programme is 'trial_c_programming3.c'
I got the following strange output. What went wrong?
[nissanka@c83-250-110-112 ~]$ gcc -Wall -o trial_c_programming3 trial_c_programming3.c
trial_c_programming3.c:3:20: error: iostream: No such file or directory
trial_c_programming3.c:4:19: error: cstring: No such file or directory
trial_c_programming3.c:5: error: syntax error before 'namespace'
trial_c_programming3.c:5: warning: type defaults to 'int' in declaration of 'std'
trial_c_programming3.c:5: warning: data definition has no type or storage class
trial_c_programming3.c: In function 'main':
trial_c_programming3.c:9: error: 'cout' undeclared (first use in this function)
trial_c_programming3.c:9: error: (Each undeclared identifier is reported only once
trial_c_programming3.c:9: error: for each function it appears in.)
trial_c_programming3.c:9: error: 'endl' undeclared (first use in this function)
trial_c_programming3.c:12: error: 'cin' undeclared (first use in this function)
trial_c_programming3.c:16: error: 'for' loop initial declaration used outside C99 mode
[nissanka@c83-250-110-112 ~]$
[ I am running Mandriva Linux 2006 version. It is a 64 bit program.]
Last edited by Gins; 07-10-2006 at 05:44 PM.
|
|
|
07-10-2006, 05:53 PM
|
#2
|
LQ Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Rep:
|
Try compiling with g++, and rename your file ".cpp"
Quote:
gcc -Wall -o trial_c_programming3 trial_c_programming3.c
<= THIS IS YOUR COMPILE COMMAND (BY DEFAULT, "GCC" WILL LOOK
AT YOUR ".C" SUFFIX AND ASSUME C SOURCE)
|
Quote:
trial_c_programming3.c:3:20: error: iostream: No such file or directory
...
trial_c_programming3.c:5: error: syntax error before 'namespace'
...
trial_c_programming3.c:9: error: 'cout' undeclared (first use in this function)
...
<= EACH OF THESE ERRORS SUGGEST TRYING TO COMPILE C++ SOURCE
AS THOUGH IT WAS C
|
Last edited by paulsm4; 07-10-2006 at 10:52 PM.
|
|
|
07-10-2006, 10:20 PM
|
#3
|
LQ Guru
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,987
|
As noted, the only time you use cstring is when you need to interface with code that expects "C-style" (null terminated) strings.
Usually, you want a much more sophisticated string-type... "string."
This string-type keeps track of its own length, without using "magic" characters (nulls) to know this. It's also possible to assign and copy this kind of string efficiently, because it has a reference-count. It can handle international character-sets better. And it even provides easy access to "a null-terminated version of itself" for (limited!) use by functions that expect this format.
When you're dealing with files of text, this is always interesting .. character-sets, how the lines are delimited (CR? LF? CR+LF? LF+CR? None of the above?), and so on. But you have the tools to do it once you figure out what the particular method that you're trying to use, is trying to do.
|
|
|
07-11-2006, 06:07 AM
|
#4
|
Senior Member
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662
Original Poster
Rep:
|
I thank both of you for the help. Now it works. The thing is that I don't know all the pros and cons of C and C++ programming on the shell.
Some 10 years ago, I did some C++ and C on Windows computers.
So when it has C++ codes, I must use ' g++ ' and the source code must have ' .cpp ' at the end.
|
|
|
07-11-2006, 01:06 PM
|
#5
|
Member
Registered: Apr 2005
Distribution: Debian
Posts: 154
Rep:
|
Right. You should also give your header files the .hpp extension.
|
|
|
07-11-2006, 04:20 PM
|
#6
|
Senior Member
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662
Original Poster
Rep:
|
You should install C or C++ in Windows. In Linux it is free; it comes with the software package.
A man told me Linux is completely based on C because of gcc. I am not sure.
Why is this discrepancy?
|
|
|
07-11-2006, 04:54 PM
|
#7
|
HCL Maintainer
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450
Rep:
|
Quote:
Originally Posted by Gins
You should install C or C++ in Windows. In Linux it is free; it comes with the software package.
A man told me Linux is completely based on C because of gcc. I am not sure.
Why is this discrepancy?
|
What discrepancy? Most OS kernels (include Windows, IIRC) are programmed in plain old C because it is low-level enough for a kernel, but also high-level enough to be easily maintained (and portable). That's why `C' is sometimes called ``portable assemble language''.
The fact that Windows does not include a compiler has nothing to do with what language was used to program it.
The thing about linux is that much of it uses many (useful) gcc extensions to C (e.g., nested functions), that for a long time only GCC could compile it.
|
|
|
07-11-2006, 05:29 PM
|
#8
|
Senior Member
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662
Original Poster
Rep:
|
osor
It is a different matter that Windows is written in C. I have known it.
If I changed to my Windows side, I can't work on C or C++ without installing software. This was my point.
It is already there in Linux.
When I start my computer, I have a choice to select either Linux or Windows. The default is Linux. LILO is doing this.
I go to the Windows side once a week or so. I don't like Windows.
|
|
|
07-11-2006, 11:29 PM
|
#9
|
Senior Member
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379
Rep:
|
Why is C/C++ not bundled with Windows?
Well when Microsoft started delivering O/S solutions they did bundle a language with the O/S and in fact that helped to make the O/S very popular. Some say that it was Mr Gates favorite language, Basic. I believe that if you dig deep enough in your windows install you can still find QBasic. Microsoft then developed sophisticated development environments, looking at the early success of Borland they chose to sell their development environment.
Times have changed and Microsoft do have a free version of their C/C++ compiler, but they don't bundle it with their O/S. Why? Well I suspect that they believe that the vast majority of their customers don't want it. What they want is to play games, browse the Internet, listen to music and watch some movies.
Time have changed and continue to change, with the growing popularity of Linux I can see Microsoft bundling their free C/C++ compilers (with other items) and pitching the O/S at school children, calling it the "Learning Edition", or something like that.
|
|
|
All times are GMT -5. The time now is 07:14 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|