LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 07-10-2006, 05:42 PM   #1
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Rep: Reputation: 47

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.
 
Old 07-10-2006, 05:53 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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.
 
Old 07-10-2006, 10:20 PM   #3
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,987
Blog Entries: 4

Rep: Reputation: 4037Reputation: 4037Reputation: 4037Reputation: 4037Reputation: 4037Reputation: 4037Reputation: 4037Reputation: 4037Reputation: 4037Reputation: 4037Reputation: 4037
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.
 
Old 07-11-2006, 06:07 AM   #4
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Original Poster
Rep: Reputation: 47
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.
 
Old 07-11-2006, 01:06 PM   #5
linux=future
Member
 
Registered: Apr 2005
Distribution: Debian
Posts: 154

Rep: Reputation: 30
Right. You should also give your header files the .hpp extension.
 
Old 07-11-2006, 04:20 PM   #6
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Original Poster
Rep: Reputation: 47
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?
 
Old 07-11-2006, 04:54 PM   #7
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 79
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.
 
Old 07-11-2006, 05:29 PM   #8
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Original Poster
Rep: Reputation: 47
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.
 
Old 07-11-2006, 11:29 PM   #9
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
C function to execute a program and return the output of the program ryan.n Programming 4 08-14-2004 11:11 PM
Strange traceroute output egurski Linux - Networking 0 07-13-2004 07:25 PM
Strange dmesg output voyciz Linux - Networking 3 06-08-2004 01:05 PM
very strange dmesg output salparadise Linux - Software 6 04-08-2004 12:34 PM
strange ofstream output maarten Programming 2 09-16-2003 12:42 PM

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

All times are GMT -5. The time now is 07:14 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