LinuxQuestions.org
Review your favorite Linux distribution.
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 02-10-2007, 08:00 AM   #1
Senatla
LQ Newbie
 
Registered: Jan 2007
Location: Lesotho in Africa, landlocked by South Africa
Distribution: scientific linux
Posts: 7

Rep: Reputation: 0
Red face This can't compile on g++


The following code fails to give me some output.


#include<iostream>
using namespace std;

int main()
{
»·······int choice;
»·······double km;
»·······double miles;
double MILES_PER-KM = 0.6214;
double KMS_PER_MILE = 1.6093;

»·······cout<<"\nMiles and Kilometers Converter\n";
»·······cout<<"................................\n";
»·······cout<<"1 : Miles to kilometers\n;
»·······cout<<"2 : Kilometers to miles\n;
»·······cout<<"3 :Quit\n\n;
»·······cout<<"Enter 1,2 or 3: ";<<endl;
»·······cin>>choice;

»·······if (choice == 1)·
»·······{
»·······»·······cout<<"Miles? ";
»·······»·······cin>>miles;
»·······»·······km = miles * KMS_PER_MILE ;
»·······»·······cout<<miles<<"miles = "<<km<< "kilometers\n";
»·······}
»·······else if (choice == 2)
»·······{
»·······»·······cout<<"Kilometers? ";
»·······»·······cin>>km;
»·······»·······miles = km * MILES_PER_KM;
»·······»·······cout<<km<< "kilometers = '<<miles << "miles\n;
»·······}
»·······else if (choice == 3)
»·······cout<< "Illegal choice!";
»·······return 0;
}

It gives out this error

> g++ mile.cpp
mile.cpp: In function `int main()':
mile.cpp:9: error: expected initializer before '-' token
mile.cpp:14: error: missing terminating " character
mile.cpp:15: error: missing terminating " character
mile.cpp:16: error: missing terminating " character
mile.cpp:17: error: expected primary-expression before '<<' token
mile.cpp:31: error: `MILES_PER_KM' was not declared in this scope
mile.cpp:32: error: expected `;' before "miles"
mile.cpp:32: error: stray '\' in program

can anyone please help with this....Please........ I am new to c++.

~
 
Old 02-10-2007, 08:10 AM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
You have set up your identifier as MILES_PER-KM, C++ thinks this is: MILES_PER minus KM, and gets confused. Just change it to MILES_PER_KM and you should get past that problem.

You need to look carefully at the output generated by g++, the first line number is 9, the very line in error and is does say that it doesn't understand why a - (minus) is where you have put it. I know it is just a typo but the compiler is unforgiving
 
Old 02-10-2007, 10:59 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Check carefully the syntax! The error pointed out by graemef is the first of a longer list. This code misses some closing double quote (lines 14 15 16), have a misplaced semi-colon (line 17), have a closing single quote instead of double quote (line 32) and missing closing double quote again (same line). It looks like you were in a hurry when you wrote this code. Be patient
 
  


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
Compile madwifi, ... compile error , how can i do. ERBRMN Linux - Networking 3 03-08-2006 07:56 PM
How do I compile this? zgauthier Programming 6 06-26-2005 09:13 AM
ntop compile/post-compile problem? tjb Linux - Software 3 12-28-2004 04:22 PM
why won't the following compile? purpleburple Programming 4 09-10-2002 07:12 PM
compile jakublgz Programming 8 09-05-2002 05:30 AM

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

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