Author Topic: I wrote my first bit of source code!  (Read 20057 times)

0 Members and 1 Guest are viewing this topic.

Offline Flipside

  • əp!sd!l£
  • 212
Re: I wrote my first bit of source code!
I tend to always put curly brackets on a line of their own, simply for readability of code.

 

Offline Mongoose

  • Rikki-Tikki-Tavi
  • Global Moderator
  • 212
  • This brain for rent.
    • Minecraft
    • Steam
    • Something
Re: I wrote my first bit of source code!
My C class's professor used a style that I'm guessing was equivalent to the "fake" K&R; he put every opening bracket, including those of functions, at the end of a line.  That's generally what I've used since then, since that's how I first coded.  My C++ professor last semester used the BSD/Allman; he always made fun of my C professor (whom he was friends with) for doing things the other way. :p

 

Offline At

  • 26
  • Eklektikon
Re: I wrote my first bit of source code!
Code: [Select]
void DoSomething(){int loop=processed=0;bool done=false;while(!done){done=StartProcessing loop,&processed;done=(processed==MaxLimit);}}Perl style would also be writing a small kludge script to add ()'s to the function calls' arguments, so you don't have to waste time on those things except when necessary.  Every key stroke is sacred, in the heat of the moment!  Besides, ()'s are just a suggestion.

Just kidding.  I've actually done some scripting in Perl (I use it as a general system utility language.), and aside from randomly dropping ()'s from function calls, I typically write it very much like what ever C and PHP I've done, with BSD style braces...  Even though I learnt some C from the The C Language book.  (The new one has more pictures... :o )   I'm mostly self taught, except that one C++ class a few years back.

I've looked at some of the SCP code, mostly at the physics stuff.  Thank goodness it's not too C++y, or else my brain'd've melted.
Does there exist a StarFox Mod?  Yes!  Check out Shadows of Lylat!

That thing…its just too persistant.

Sadhal thought to himself in between his pants. After a few seconds, his breathing came under control, and the pain subsided. Sadhal couldn’t help but feel a significant amount of fear and apprehension.

 

Offline Freespace Freak

  • 28
  • Official forum permanewb
Re: I wrote my first bit of source code!
That guy I mentioned does most of his programming in Ruby, which sounds like perl only because both "pearls" and "rubies" are precious stones.  It looks interesting, but I'm not particularly interested in those languages unless my future jobs requires using them for programming search engines.

 

Offline Freespace Freak

  • 28
  • Official forum permanewb
Re: I wrote my first bit of source code!
Yeah, look at this code.  I'm sure you guys are so interested in my homework.   :lol:  Whatdya guys think about the structure.  I tried to make it as easy to look at as possible, but with all the multitude if if-else statements, it didn't look possible.

Code: [Select]
//Setup
#include<stdio.h>
#include<math.h>
int main (void)

{
//Declare Variables
double TempIn, TempOut, TempK;
char   UnitIn, UnitOut;

//Print Title
printf ("This program will convert a temperature in units\n");
printf ("of Celsius (C), Kelvin (K), Fahrenheit (F) or\n");
printf ("Rankine (R), to a temperature in any of these units.\n\n");

//Input
printf ("Input Temperature:");
    scanf  ("%lf", &TempIn);
    printf ("Units to convert from (C, K, F, R):");
    fflush (stdin);
    scanf  ("%c", &UnitIn);
    printf ("Units to conver to (C, K, F, R):");
    fflush (stdin);
    scanf  ("%c", &UnitOut);


//Calculations
//Step One, Standardizing:
if (UnitIn  > 96) UnitIn  = UnitIn  - 32;
if (UnitOut > 96)   UnitOut = UnitOut - 32;

//Step Two, Convert Input to K
if (UnitIn == 'K')
{
TempK=TempIn;
}
else if (UnitIn == 'C')
{
TempK = TempIn + 273.15;
}
else if (UnitIn == 'F')

{
TempK = (TempIn - 32) / 1.8 +273.15;
}
else if (UnitIn == 'R')
{
TempK = TempIn/1.8;
}
else
{
printf ("\nUnits %c is not a valid input.\n\n", UnitIn);
return 0;
}

//Step Three: Convert from K
if (UnitOut == 'K')
{
TempOut = TempK;
}
else if (UnitOut == 'C')
{
TempOut = TempK - 273.15;
}
else if (UnitOut == 'F')
{
TempOut = (TempK * 1.8) - 459.67;
}
else if (UnitOut == 'R')
{
TempOut = TempK * 1.8;
}
else
{
printf ("\nUnits %c is not a valid input.\n\n", UnitOut);
return 0;
}

//Output
if (TempK < 0)
{
printf ("\nTemperature is less than absolute zero.  Temperature is invalid.\n\n");
return 0;
}
if (UnitIn == 'K')
{
printf ("\nA temperature of %4.2lf K"), TempIn;
}
else
{
printf ("\nA temperature of %4.2lf \xf8%c", TempIn, UnitIn);
}
if (UnitOut == 'K')
{
printf (" is equivalent to %4.2lf K", TempOut);
}
else
{
printf (" is equivalent to %4.2lf \xf8%c", TempOut, UnitOut);
}
printf ("\n\n");
return 0;

}

I did some editing that maybe makes it better to read.  Basically I just made all the comments align to the left.
« Last Edit: October 14, 2006, 09:37:20 pm by Freespace Freak »

 
Re: I wrote my first bit of source code!
Ok I'd like to hear the opinions about comment locations :-)
Me personally I indent them to match the code.

Btw that formatting looks quite a bit like mine, just a bit tighter

I edited it in notepad  :nervous: so it might not look right... I'd like to see what other people would do it too  :)
(I'd also have it divided into about three different functions (input, calc, output) ;

Ok It's C++ and not C...
Code: [Select]
#include <stdio.h>
#include <math.h>

int main (int argc, char *argv)
{

// Shut the warnings up
argc = argc ;
argv = argv ;

//Print Title
printf ("This program will convert a temperature in units\n") ;
printf ("of Celsius (C), Kelvin (K), Fahrenheit (F) or\n") ;
printf ("Rankine (R), to a temperature in any of these units.\n\n") ;

//Input
double tempIn ;
printf ("Input Temperature:") ;
    scanf  ("%lf", &tempIn) ;

char unitIn ;
    printf ("Units to convert from (C, K, F, R):") ;
    fflush (stdin) ;
    scanf  ("%c", &unitIn) ;

char unitOut ;
    printf ("Units to conver to (C, K, F, R):") ;
    fflush (stdin) ;
    scanf  ("%c", &unitOut) ;

//Calculations
//Step One, Standardizing:
if (unitIn  > 96)
unitIn  = unitIn  - 32 ;

if (unitOut > 96)   
unitOut = unitOut - 32 ;

//Step Two, Convert Input to K
double tempK ;
double tempOut ;
switch (unitIn)
{
case 'K':
case 'k':
tempK = tempIn ;
break ;
case 'C':
case 'c':
tempK = tempIn + 273.15 ;
break ;
case 'F':
case 'f':
tempK = (tempIn - 32) / 1.8 + 273.15 ;
break ;
case 'R':
case 'r':
tempK = tempIn / 1.8 ;
default:
{
printf ("\nUnits %c is not a valid input.\n\n", unitIn) ;
return 0 ;
}
}

//Step Three: Convert from K
switch (unitOut)
{
case 'K':
case 'k':
tempOut = tempK ;
break ;
case 'C':
case 'c':
tempOut = tempK - 273.15 ;
case 'F':
case 'f':
tempOut = (tempK * 1.8) - 459.67 ;
case 'R':
case 'r':
tempOut = tempK * 1.8 ;
default:
{
printf ("\nUnits %c is not a valid input.\n\n", unitOut) ;
return 0 ;
}
}

//Output
if (tempK < 0)
{
printf ("\nTemperature is less than absolute zero.  Temperature is invalid.\n\n") ;
return 0 ;
}

print ("\nA temperature of ") ;

if (unitIn == 'K')
printf ("%4.2lf K"), tempIn ;
else
printf ("%4.2lf \xf8%c", tempIn, unitIn) ;

print (" is equivanlent to ") ;

if (unitOut == 'K')
printf ("%4.2lf K", tempOut) ;
else
printf ("%4.2lf \xf8%c", tempOut, unitOut) ;

printf ("\n\n") ;
return 0 ;
}
That's cool and ....disturbing at the same time o_o  - Vasudan Admiral

"Don't play games with me. You just killed someone I like, that is not a safe place to stand. I'm the Doctor. And you're in the biggest library in the universe. Look me up."

"Quick everyone out of the universe now!"

 

Offline taylor

  • Super SCP/Linux Guru
  • Moderator
  • 212
    • http://www.icculus.org/~taylor
Re: I wrote my first bit of source code!
Ok I'd like to hear the opinions about comment locations :-)
Me personally I indent them to match the code.
Yep, comments should be indented the same as the code they are referring to.

 

Offline Freespace Freak

  • 28
  • Official forum permanewb
Re: I wrote my first bit of source code!
Hmmm, lets see if it runs on the compiler.  BTW, I did notice you had case 'K' and case 'k', etc.  The lower case cases are not necessary since I already stadardized with:
Code: [Select]
if (UnitIn>97) UnitIn=UnitIn-32
That converts any lower case character to an upper case one.  Also, I had made it UnitIn and you wrote unitIn so they weren't incompatible, but I fixed that.

There were several errors in your code, some of them went unnoticed by the debugger.  You put print instead of printf in two seperate occasions near the bottom.  You forgot to put breaks in the second switch statement.  You put a break after the first one but not the second one.  Other than that, it looks great!  Probably alot more readable than my first code.

Code: [Select]
#include <stdio.h>
#include <math.h>

int main (int argc, char *argv)
{

// Shut the warnings up
argc = argc ;
argv = argv ;

//Print Title
printf ("This program will convert a temperature in units\n") ;
printf ("of Celsius (C), Kelvin (K), Fahrenheit (F) or\n") ;
printf ("Rankine (R), to a temperature in any of these units.\n\n") ;

//Input
double tempIn ;
printf ("Input Temperature:") ;
    scanf  ("%lf", &tempIn) ;

char UnitIn ;
    printf ("Units to convert from (C, K, F, R):") ;
    fflush (stdin) ;
    scanf  ("%c", &UnitIn) ;

char UnitOut ;
    printf ("Units to conver to (C, K, F, R):") ;
    fflush (stdin) ;
    scanf  ("%c", &UnitOut) ;

//Calculations
//Step One, Standardizing:
if (UnitIn  > 96)
UnitIn  = UnitIn  - 32 ;
if (UnitOut  > 96)
UnitOut  = UnitOut  - 32 ;

//Step Two, Convert Input to K
double tempK ;
double tempOut ;
switch (UnitIn)
{
case 'K':
tempK = tempIn ;
break ;
case 'C':
tempK = tempIn + 273.15 ;
break ;
case 'F':
tempK = (tempIn - 32) / 1.8 + 273.15 ;
break ;
case 'R':
tempK = tempIn / 1.8 ;
default:
{
printf ("\nUnits %c is not a valid input.\n\n", UnitIn) ;
return 0 ;
}
}

//Step Three: Convert from K
switch (UnitOut)
{
case 'K':
tempOut = tempK ;
break ;
case 'C':
tempOut = tempK - 273.15 ;
break;
case 'F':
tempOut = (tempK * 1.8) - 459.67 ;
break;
case 'R':
tempOut = tempK * 1.8 ;
break;
default:
{
printf ("\nUnits %c is not a valid input.\n\n", UnitOut) ;
return 0 ;
}
}

//Output
if (tempK < 0)
{
printf ("\nTemperature is less than absolute zero.  Temperature is invalid.\n\n") ;
return 0 ;
}

printf ("\nA temperature of ") ;

if (UnitIn == 'K')
printf ("%4.2lf K"), tempIn ;
else
printf ("%4.2lf \xf8%c", tempIn, UnitIn) ;

printf (" is equivanlent to ") ;

if (UnitOut == 'K')
printf ("%4.2lf K", tempOut) ;
else
printf ("%4.2lf \xf8%c", tempOut, UnitOut) ;

printf ("\n\n") ;
return 0 ;
}
« Last Edit: October 15, 2006, 09:43:36 am by Freespace Freak »

 

Offline taylor

  • Super SCP/Linux Guru
  • Moderator
  • 212
    • http://www.icculus.org/~taylor
Re: I wrote my first bit of source code!
Hmmm, lets see if it runs on the compiler.  BTW, I did notice you had case 'K' and case 'k', etc.  The lower case cases are not necessary since I already stadardized with:
Code: [Select]
if (UnitIn>97) UnitIn=UnitIn-32
That converts any lower case character to an upper case one.  Also, I had made it UnitIn and you wrote unitIn so they were incompatible so I fixed that.
Umm, why not just use the standard toupper() function?  Your code is non-portable and locale specific.

 

Offline Freespace Freak

  • 28
  • Official forum permanewb
Re: I wrote my first bit of source code!
I haven't learned that one yet.  Is it understood by both C and C++ libraries?  Also, what specifically does this do?

Code: [Select]
// Shut the warnings up
argc = argc ;
argv = argv ;

« Last Edit: October 15, 2006, 09:44:56 am by Freespace Freak »

 

Offline taylor

  • Super SCP/Linux Guru
  • Moderator
  • 212
    • http://www.icculus.org/~taylor
Re: I wrote my first bit of source code!
I haven't learned that one yet.  Is it understood by both C and C++ libraries?
It's part of ANSI C, so it should be available in C++ too.

Also, what specifically does this do?

Code: [Select]
// Shut the warnings up
argc = argc ;
argv = argv ;
Nothing sane.  It shouldn't be there.

 

Offline Freespace Freak

  • 28
  • Official forum permanewb
Re: I wrote my first bit of source code!
Yeah, I removed it and also replaced
Code: [Select]
int main (int argc, char *argv) with
Code: [Select]
int main (void) and it works normally.

Note:  I tried looking for the toupper function here and couldn't find it.
« Last Edit: October 15, 2006, 10:17:03 am by Freespace Freak »

 

Offline taylor

  • Super SCP/Linux Guru
  • Moderator
  • 212
    • http://www.icculus.org/~taylor
Re: I wrote my first bit of source code!
Yeah, I removed it and also replaced
Code: [Select]
int main (int argc, char *argv) with
Code: [Select]
int main (void) and it works normally.
No.  That's wrong.  main() must always be "int main(int argc, char *argv[])", though "char *argv" typically works, it isn't really proper.  main() isn't some arbitrary function but a very specific one used as the program entry point by the linker.  If it isn't done correctly then your progam may not link, may not run, and will be very compiler/linker specific either way.

Note:  I tried looking for the toupper function here and couldn't find it.
It's there (in C++, part of the standard), specified in the cctype header.

 

Offline Freespace Freak

  • 28
  • Official forum permanewb
Re: I wrote my first bit of source code!
I downloaded Microsoft Visual C++ 8.  I was using Visual C++ 6 introductory edition.  The damn thing doesn't work on 8.  When I run the code, it says "fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?"

So I put that at the top and it runs, but instead of displaying the results and leaving the screen saying "Press any key to continue" it appears to just close out of the console as soon as I enter the last input.

 

Offline taylor

  • Super SCP/Linux Guru
  • Moderator
  • 212
    • http://www.icculus.org/~taylor
Re: I wrote my first bit of source code!
So I put that at the top and it runs, but instead of displaying the results and leaving the screen saying "Press any key to continue" it appears to just close out of the console as soon as I enter the last input.
It's a console app and, in general, when a console app is done the cmd window will close.  Such is life in a GUI environment.

You can either set the cmd window to not close when a program exits, or you can modify your code to require a key input before completing.  In the second case just run through like normal, and as the last thing in main() just wait for keyboard input (ie, a simple "Press any key...") before returning from the function.  That way you get to see all output and actually digest it before the program exits.  It's quick, easy, and it works.

 

Offline Turey

  • Installer dude
  • 211
  • The diminutive form of Turambar.
    • FreeSpace Open Installer Homepage
Re: I wrote my first bit of source code!
So I put that at the top and it runs, but instead of displaying the results and leaving the screen saying "Press any key to continue" it appears to just close out of the console as soon as I enter the last input.

amazingly, the following sequence will fix your problem:

Start>Run...>cmd>navigate to directory>run program
Creator of the FreeSpace Open Installer.
"Calm. The ****. Down." -Taristin
why would an SCP error be considered as news? :wtf: *smacks Cobra*It's a feature.

 
Re: I wrote my first bit of source code!
Hmmm, lets see if it runs on the compiler.  BTW, I did notice you had case 'K' and case 'k', etc.  The lower case cases are not necessary since I already stadardized with:
Code: [Select]
if (UnitIn>97) UnitIn=UnitIn-32
Oh thats what the -32 did... I thought it had something to do with the temperatures.  use toupper or tolower instead.

There were several errors in your code, some of them went unnoticed by the debugger.  You put print instead of printf in two seperate occasions near the bottom.  You forgot to put breaks in the second switch statement.  You put a break after the first one but not the second one.  Other than that, it looks great!  Probably alot more readable than my first code.
Well i redid it in notepad so i didn't compile it, plus i haven't written c++ in a while now, I've been using C# instead.


I haven't learned that one yet.  Is it understood by both C and C++ libraries?
It's part of ANSI C, so it should be available in C++ too.

Also, what specifically does this do?

Code: [Select]
// Shut the warnings up
argc = argc ;
argv = argv ;
Nothing sane.  It shouldn't be there.

Depends... lint or fxcop may complain about unused parameters or unused local variables.  Althought it's mostly trival.....
and yes it should be:
Code: [Select]
int main (int argc, char *argv [])
My bad, i probably should have compiled it first. I wasn't so worried about code rather formatting  :)
That's cool and ....disturbing at the same time o_o  - Vasudan Admiral

"Don't play games with me. You just killed someone I like, that is not a safe place to stand. I'm the Doctor. And you're in the biggest library in the universe. Look me up."

"Quick everyone out of the universe now!"

 
Re: I wrote my first bit of source code!
Btw what is "\xf8%c" for?
That's cool and ....disturbing at the same time o_o  - Vasudan Admiral

"Don't play games with me. You just killed someone I like, that is not a safe place to stand. I'm the Doctor. And you're in the biggest library in the universe. Look me up."

"Quick everyone out of the universe now!"

  

Offline Freespace Freak

  • 28
  • Official forum permanewb
Re: I wrote my first bit of source code!
Btw what is "\xf8%c" for?

It's actually just \xf8.  \x tells the compiler that the next string is a hex code for a character, f8 is the hex code for the degree sign, and that's where it stops.  %c is not part of \xf8, and I'm sure you know what %c means.

 

Offline taylor

  • Super SCP/Linux Guru
  • Moderator
  • 212
    • http://www.icculus.org/~taylor
Re: I wrote my first bit of source code!
Depends... lint or fxcop may complain about unused parameters or unused local variables.  Althought it's mostly trival.....
Yes they might, but it doesn't mean they are correct in that.  Those two variables should also be considered const, which means you can't assign anything to them, especially to themselves.