LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 08-07-2008, 12:14 PM   #1
blastradius
Member
 
Registered: Oct 2004
Location: Cheshire, England
Distribution: Debian Stable
Posts: 269

Rep: Reputation: 39
worlds simplest program won't work for me!


I know this is staggeringly simple but I'm a raw recruit, I've typed this into Vim:-

#include <iostream>

using namespace std;

int choice;

int main(void)

{
cout << "\t\t\t\tEnter integer" << endl;
cin >> choice;
cout << "\t\t\t\tYOU ENTERED THE INTEGER" << choice <<endl;



return 0;

}

I type gcc -Wall untitled.cpp into the terminal and I get:-

untitled.cpp:10: error: expected primary-expression before ‘<<’ token
untitled.cpp:12: error: expected primary-expression before ‘<<’ token

Please put me out of my misery!

Also what should I put in the int main(void) line, my book just has () but it seems to cause problems so I copied the int main(void) from some code online and that seems ok. Is my book outdated?

Thanks in advance

Eric

Last edited by blastradius; 08-07-2008 at 12:19 PM.
 
Old 08-07-2008, 12:38 PM   #2
kilgoretrout
Senior Member
 
Registered: Oct 2003
Posts: 2,987

Rep: Reputation: 388Reputation: 388Reputation: 388Reputation: 388
I don't think there is anything wrong with your code but you are using the wrong compiler. For C++ code, you should use g++ instead of gcc, i.e.:

$ g++ -Wall untitled.cpp
 
Old 08-07-2008, 01:57 PM   #3
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Take out the spaces between < < and make it <<, which is a single operator.
ta0kira

edit:
Nevermind. Looks like they show up spaced apart with my screen font.

edit2:
Compilation works fine for me, though you get STL link errors when building with gcc. You need to use g++, but I don't know what's causing your problem. Maybe you've written this with a rich text editor and there's a hidden character in there? Copy and paste from your own post and see if it works then.

Last edited by ta0kira; 08-07-2008 at 02:10 PM.
 
Old 08-07-2008, 03:07 PM   #4
blastradius
Member
 
Registered: Oct 2004
Location: Cheshire, England
Distribution: Debian Stable
Posts: 269

Original Poster
Rep: Reputation: 39
Thanks guys, using g++ sorted the problem.

What is the score with the main() function though, if I load an example from an ide there's stuff like'argc' something or other in there. Do I need to include these args or not?
 
Old 08-07-2008, 03:16 PM   #5
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Those arguments are how one passes command-line arguments. void is non-standard since it gives you no "startup options" for your program.
ta0kira
 
Old 08-07-2008, 03:19 PM   #6
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Code:
#include <stdio.h>

int main(int argc, char *argv[])
{
    int I = 0;
    for (; I < argc; I++) printf("argument %d: '%s'\n", I, argv[I]);
}
Build and run using various command-line arguments.
ta0kira
 
  


Reply



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
Two Worlds (not installing) phoenix_wolf Linux - Games 13 09-03-2008 02:28 AM
LXer: Best of many worlds LXer Syndicated Linux News 0 08-21-2007 09:30 PM
How can I get the best of all Linux worlds? eoinrua Linux - Newbie 10 05-15-2007 05:44 PM
cryptic errors compiling simplest C program code_noc Slackware 4 10-06-2003 08:38 PM
Worlds Worst distributions acid_kewpie General 78 03-05-2002 09:10 AM

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

All times are GMT -5. The time now is 04:04 AM.

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