Coding questions (C, C++)

Games, RP, general Mayhem (within reason, of course)

Moderators: TingYi, anti-paragon, DarkShive

Coding questions (C, C++)

Postby Ciennas on Mon Jan 14, 2008 3:45 pm

Hello Mayhem,

I'm starting to learn about the nifty new C language, in order to learn about C++ later on.

Anyway, I purchased a Spell- erm, Codebook to learn from, and decided to try one of the opening codes: a calculator that converts Kilometers to miles.

I had already downloaded SciTE 1.7.1 for my U3 drive long before I decided to purchase the book, and ran the code through it.

It keeps telling me that it cannot find the document in question on the system.

Code: Select all
#include<stdio>   /*printf, scanf definitions*/
#define KMS_PER_MILE 1.609 /*conversion constant*/

int

main (void)
{
   double miles,
   kms;
   
   printf("Enter the distance in miles> ")
   scanf("%lf", &miles);
   
   kms= KMS_PER_MILE * miles;
   
   printf ("That equals %f kilometers. /n", kms);
   return (0)
}


So I hit the compile key, and get the following message.

>g++ -pedantic -Os -c MPHtoKMH calculator TEST.cpp -o MPHtoKMH calculator TEST.o
>The system cannot find the file specified.


I think it's telling me that it's trying to look for it in my C:Drive, although when I saved a copy in there, it reported the same error.

Anybody have any suggestions?
I told you. Humans only see what they're looking for.
Image
Drayco wrote:I always make sense! Do not ask questions of my sensemaking!

Ennesby wrote:> About this life thing: > If I screw it up I can revert to a saved game, right?

Inventory- A stylish bag of Emuholding +8, containing all the standard fixins, and an emu from Squato. I should really cut some airholes for the poor guy.
User avatar
Ciennas
Grand Poobah Keenspotter
 
Posts: 811
Joined: Thu Jun 08, 2006 1:22 pm
Location: In a room of Golden sunlight streaming and leather armchairs, Talking with my characters...

Postby CPUX on Mon Jan 14, 2008 4:37 pm

I wonder if that's a direct copy-paste. If so, it seems a bit wonky. It's not syntactically wrong (C compilers generally just treat line breaks as spaces), but it looks unclean.

Also, on the printf() line that shows the output, it should be a \n instead of /n. \n is a special "newline" string, and /n is nothing special.

I should probably ask, what OS are you using? I'll admit, I know waaaaay more about Unix-like shells than DOS/Windows, but it's not like my brain turns off when I see the Windows logo. :P

EDIT: OK, I relooked, and I see you're stuck with the compiler. I'll figure out exactly what to say in a bit, but I can tell right away that I think you're trying to name the file with a whitespace, and that's not a good idea.
Last edited by CPUX on Mon Feb 04, 2008 3:43 am, edited 2 times in total.
User avatar
CPUX
Keenspot Juggernaut
 
Posts: 9864
Joined: Fri May 28, 2004 7:26 am
Location: Under a pile of action figures

Postby Ciennas on Mon Jan 14, 2008 4:56 pm

Changed to both variations, and still the same result.

Windows XP, service pack two at the moment. Updated to current, unless my system is ignoring microsoft for some reason.

And yes, that's a direct copy paste, mostly. Inelegance I can live with for the moment, but when I get more comfortable reading line functions I'll clean it up for the finished working code.

New code line, And new results:

Code: Select all
#include <cstdio>   /*printf, scanf definitions*/
#define KMS_PER_MILE 1.609 /*conversion constant*/

int

main (void)
{
   double miles,
   kms;
   
   printf("Enter the distance in miles> ")
   scanf("%lf", &miles);
   
   kms= KMS_PER_MILE * miles;
   
   printf ("That equals %f kilometers. \n", kms);
   return (0)
}


>g++ -pedantic -Os -c MPHtoKMH calculator TEST.cpp -o MPHtoKMH calculator TEST.o
>The system cannot find the file specified.


I have a question for you. What does -c, -o, and. o mean, anyway?
I told you. Humans only see what they're looking for.
Image
Drayco wrote:I always make sense! Do not ask questions of my sensemaking!

Ennesby wrote:> About this life thing: > If I screw it up I can revert to a saved game, right?

Inventory- A stylish bag of Emuholding +8, containing all the standard fixins, and an emu from Squato. I should really cut some airholes for the poor guy.
User avatar
Ciennas
Grand Poobah Keenspotter
 
Posts: 811
Joined: Thu Jun 08, 2006 1:22 pm
Location: In a room of Golden sunlight streaming and leather armchairs, Talking with my characters...

Postby CPUX on Mon Jan 14, 2008 5:06 pm

OK, I've thought up a quick, very simplified primer on gcc/g++

Code: Select all
g++ filename.cpp

This is the most bare-bones compilation. It creates an executable file called "a.out" in the working directory from the code.

However, gcc (and many command line tools built for Unix and Linux systems) have built-in options. These option sets are generally prefaced with a '-' character.

Code: Select all
g++ -c filename.o filename.cpp

This creates the file "filename.o" from the source, filename.cpp, in the working directory. filename.o is object code, which is useful for debugging and linking.

Code: Select all
g++ -o filename filename.cpp

This creates an executable file called "filename," compiled from filename.cpp. You can also use object files instead of source code. On Linux systems, this executable can then be called with the line:

Code: Select all
./filename

On Windows systems, I believe it's sufficient to just type the file name if it's in the working directory.

Now, to analyze this:

Code: Select all
>g++ -pedantic -Os -c MPHtoKMH calculator TEST.cpp -o MPHtoKMH calculator TEST.o

What this is trying to do is find the source file "calculator", and it can't find it. Also, it'll be looking for TEST.o when TEST.o was never created to begin with.

It should be something more like this:

Code: Select all
>g++ -c MPHtoKMH.o TEST.cpp -o MPHtoKMH MPHtoKMH.o

Try and see if that works. If it doesn't, then just do

Code: Select all
>g++ -o MPHtoKMH TEST.cpp

-pedantic is a minor option, and just means it goes over every warning it comes across (this is usually shown by default anyway). I have no idea what -Os does.

For what it's worth, compiling is half the battle when it comes to C/C++. It's why many of its successors (and even C++ itself) have GUI tools that really help automate the compilation process.

EDIT: I also noticed, there should be a ; at the end of the first printf() line and the return line. And it's certainly odd seeing

Code: Select all
int

main (void)

like that. Usually it's just

Code: Select all
int main(void)
User avatar
CPUX
Keenspot Juggernaut
 
Posts: 9864
Joined: Fri May 28, 2004 7:26 am
Location: Under a pile of action figures

Postby Lomgren on Mon Jan 14, 2008 9:06 pm

It's also possible that the compiler isn't on the system. As far as I know, SciTE is only a text editor, it doesn't include g++. I might be quite wrong on that though...
"Illustrious 'Lommy' Lomgren" - anti-paragon, "The Doer of Impossible Things" - Demonic Duck
(Got pulled in by the GPF guest strips. Read the archive in one night, getting 2 hours of sleep)
Image Image
Epic Patrol Bunny artist and avatarist, 12867 bunnypoints (version 2.8, Oct 31, 2006)
Image Image
My inventory ... Where I finished reading carrot..?
Avatar drawn by KillerFish, colored by me. Thanks!
(banners clickable!)
User avatar
Lomgren
Keenspot Singularity
 
Posts: 85030
Joined: Sun Nov 27, 2005 4:45 pm
Location: EGS Mayhem or El Goonish Shive forums

Postby CPUX on Mon Jan 14, 2008 9:20 pm

Lomgren wrote:It's also possible that the compiler isn't on the system. As far as I know, SciTE is only a text editor, it doesn't include g++. I might be quite wrong on that though...

Ooooh, hadn't thought of that. I live in this crazy open source world where everyone's got gcc. :gonk:

Certainly there's gotta be some free C/C++ compiler for Windows.

Two minutes later, with the power of Google, came this. Looks a wee bit complex (certainly designed as not just a compiler, but a full programming environment), but certainly should do what it should.
User avatar
CPUX
Keenspot Juggernaut
 
Posts: 9864
Joined: Fri May 28, 2004 7:26 am
Location: Under a pile of action figures

Postby Ciennas on Mon Feb 04, 2008 2:11 am

Okay, So it's been a while.

I apologize.

I looked up the program you sent me, and with much paranoia induced misgiving, I installed and ran it. Nothing personal, but the net gives me heeby jeebies when it comes to actually pulling program information off of it.

I find myself giddy as a schoolchild clapping my hands in sheer joy. SciTE is bare bones... basic. No compiler. You're right, Lomgren. It's not there, and this thing has it.

Thank you CPUX. Thank you so very very much!
...

That said... I ran it again, and the program is telling me that there's a syntax error on the second line of the program, but it won't elaborate for me.

The line in question is ' scanf("%lf", &miles);'

I don't know what's wrong with this picture here.

Any ideas?
I told you. Humans only see what they're looking for.
Image
Drayco wrote:I always make sense! Do not ask questions of my sensemaking!

Ennesby wrote:> About this life thing: > If I screw it up I can revert to a saved game, right?

Inventory- A stylish bag of Emuholding +8, containing all the standard fixins, and an emu from Squato. I should really cut some airholes for the poor guy.
User avatar
Ciennas
Grand Poobah Keenspotter
 
Posts: 811
Joined: Thu Jun 08, 2006 1:22 pm
Location: In a room of Golden sunlight streaming and leather armchairs, Talking with my characters...

Postby CPUX on Mon Feb 04, 2008 3:36 am

Look at the line before it. Notice anything missing on the end? :P
User avatar
CPUX
Keenspot Juggernaut
 
Posts: 9864
Joined: Fri May 28, 2004 7:26 am
Location: Under a pile of action figures

Re: Coding questions (C, C++)

Postby Coder Cat on Mon Feb 04, 2008 7:29 am

Ciennas wrote:Hello Mayhem,

I'm starting to learn about the nifty new C language, in order to learn about C++ later on.


Nice. Hope you find it exciting and rewarding.

Ciennas wrote:
Code: Select all
#include<stdio>   /*printf, scanf definitions*/



Assuming that we're using C here, that should read:

Code: Select all
#include <stdio> /*printf,scanf definitions*/


Ciennas wrote:
Code: Select all
int

main (void)



probably want to put those on one line so that it's easier to tell what the int belongs to.

like this:
Code: Select all
int main(void)


Ciennas wrote:
Code: Select all
        double miles,
   kms;



I'd put those on the same line like this:

Code: Select all
double miles, kms;


Ciennas wrote:
Code: Select all
printf("Enter the distance in miles> ")



Needs a semicolon on the end of the line.

Ciennas wrote:
Code: Select all
printf ("That equals %f kilometers. /n", kms);



That should be \n not /n in there to insert a newline.

like this:
Code: Select all
printf("That equals %f kilometers.\n", kms);


Ciennas wrote:
Code: Select all
return (0)



"return" is a statement, not a function. It doesn't use any parentheses. Also, there's a semicolon missing again.

like so:
Code: Select all
return 0;


Ciennas wrote:So I hit the compile key, and get the following message.

>g++ -pedantic -Os -c MPHtoKMH calculator TEST.cpp -o MPHtoKMH calculator TEST.o
>The system cannot find the file specified.


I think it's telling me that it's trying to look for it in my C:Drive, although when I saved a copy in there, it reported the same error.


Hmm...

Do you have gcc/g++ installed on your system? Also, what OS are you using?

Also, if you're coding in C, wouldn't it be better to end the filename in .c and use gcc to compile instead of g++?

Also, did the book say to use g++? And, did you read the part of the book up to the example you tried or did you just copy it into the computer and try to compile it without reading the book?

And also, were the errors I pointed out above your errors or were they present in the book?

Btw, you should try learning C++ from a book assumes pre-ISO syntax while using an ISO-compatible compiler on your system. It gives such wonderful headaches.

Coder Cat, whose preferred IDE is called vim.
Fanart??? I couldn't draw a straight line, even if you gave me a ruler!

Newbie Pack:
6 cans of unlabeled soda, dead fish, Squirrel Teddy, Cookie, :tfgun:, Gravity leveller, all courtesy of Ankhareon :D

Avatar courtesy of Professor Tomoe.

Currently running:
Gentoo Linux, X11 with DRI, Xinerama, KDE, Firefox, Thunderbird, and VIM (among other things).


Image
I currently have: 0 EXP (as far as I know, right now I'm too lazy to check.)
User avatar
Coder Cat
Keenspot Despot
 
Posts: 2259
Joined: Sun Feb 25, 2007 10:16 pm
Location: MKRP

Postby CPUX on Mon Feb 04, 2008 7:41 am

I pretty much stated all that already. and by the way, I'm sure the "stdio" thing is a filter on the board's part. I'm guessing there's an evil conspiracy to hide references to C header files.
User avatar
CPUX
Keenspot Juggernaut
 
Posts: 9864
Joined: Fri May 28, 2004 7:26 am
Location: Under a pile of action figures

 

Return to Mayhem

Who is online

Users browsing this forum: No registered users and 8 guests

cron