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

0 Members and 1 Guest are viewing this topic.

Re: I wrote my first bit of source code!
Well if their const then I'll retract my statement :)
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!
Okay I fixed the whole closing console problem.  I tried this toupper thing and the debugger calls it an unknown identifier.  Do I need to include a specific library?  So, with Visual C++ 8.0, it says I need to have #include <stdafx.h>.  I have no idea what that is, but apparently it replaces <stdio.h>.  I saw something similar when I was using cin and cout, so I guess they have another library that replaced <iostream.h>.  Despite the need for the extra command lines, I actually like it because it has the collapsable menus.

 
Re: I wrote my first bit of source code!
put the #include <stdafx.h> at the very top
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!
put the #include <stdafx.h> at the very top


I know that!  I was merely mentioning that it replaced <stdio.h>, and it also seems to have replaced iostream.h,  though with what, I haven't figured out yet.

 
Re: I wrote my first bit of source code!
Ok is this better?  :D
 It compiles and runs

tempconverter.cpp
Code: [Select]
#include <stdafx.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>

#include "tempconverter.h"

int main (int argc, char *argv[])
{
    double tempIn ;
    char   unitIn ;
    char   unitOut ;

    Input (tempIn, unitIn, unitOut) ;

    double tempOut ;
    if (!Calc (tempIn, unitIn, unitOut, tempOut))
        return 0 ; // Bad unit ;

    Output (tempIn, unitIn, tempOut, unitOut, tempOut) ;

    return 0 ;
}

//////////////////////////////////////////////////////////////////////////
// Get data from user
void Input (double &tempIn, char &unitIn, char &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 convert to (C, K, F, R):") ;
    fflush (stdin) ;
    scanf  ("%c", &unitOut) ;

    unitIn = toupper (unitIn) ;
    unitOut = toupper (unitOut) ;
}



//////////////////////////////////////////////////////////////////////////
// Convert from source temp to dest temp
// Returns true if successful, false if invalid temp
bool Calc (double tempIn, char unitIn, char unitOut, double &tempOut)
{
    //Step Two, Convert Input to K
    double tempK ;

    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 ;
        break ;
    default:
        {
            printf ("Unit %c is not a valid input.\n\n", unitIn) ;
            return false ;
        }
    }

    //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 ("Unit %c is not a valid input.\n\n", unitOut) ;
            return false ;
        }
    }

    return true ;
}

//////////////////////////////////////////////////////////////////////////
// Output results
void Output (double tempIn, char unitIn, double tempOut, char unitOut, double tempK)
{
    // Check for invalid temp
    printf ("\n") ;

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

    printf ("A temperature of %4.2lf %c is equivalent to %4.2lf %c.\n\n", tempIn, unitIn, tempOut, unitOut) ;
}

tempconverter.h
Code: [Select]
void Input (double &tempIn, char &unitIn, char &unitOut) ;
bool Calc (double tempIn, char unitIn, char unitOut, double &tempOut) ;
void Output (double tempIn, char unitIn, double tempOut, char unitOut, double tempK) ;
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!
My Compiler looks at toupper and say "wtf!"   :P  *shrugs*  I dunno.

 
Re: I wrote my first bit of source code!
I'm using Visual studio .net 2003.
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!
The free download I used from microsoft is Visual C++ 8.0 (aka 2005 Express Edition), and it includes the .NET framework.  However there seems to be more than just those changes.  Perhaps the got rid of toupper to make tighter code, maybe toupper was a source of system crashes. 

BTW, microsoft also has C#, J#, Visual Basic, and Visual Web developer for free download.  What do you guys think of these languages?  I'm guessing J# is a Microsoft knock off of Java, they use it to make windows apps. 

 
Re: I wrote my first bit of source code!
Check the help for toupper. It should tell you which header files to include (they shouldn't have changed)

Btw it needs stdlib.h not stdio.h.

« Last Edit: October 15, 2006, 10:35:02 pm by Scooby_Doo »
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!
Does anyone here use Visual C++ 8.0 with the .NET framework?  It doesn't have ANSI-C++, but instead uses CLI.  Do you guys know what they use for iostream for cin and cout and all that jazz?

Edit: Nevermind, I found it!  They standardized the C/C++ libraries.  It's all normal, it's just that you drop the .h from the #include line.
« Last Edit: October 16, 2006, 12:04:15 pm by Freespace Freak »

 
Re: I wrote my first bit of source code!
The free download I used from microsoft is Visual C++ 8.0 (aka 2005 Express Edition), and it includes the .NET framework.  However there seems to be more than just those changes.  Perhaps the got rid of toupper to make tighter code, maybe toupper was a source of system crashes. 

BTW, microsoft also has C#, J#, Visual Basic, and Visual Web developer for free download.  What do you guys think of these languages?  I'm guessing J# is a Microsoft knock off of Java, they use it to make windows apps. 

Avoid J#.. VB is ok, don't know anything about VW.
I love C#
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!
My dad does programming with VB.  He likes it.  I heard J# is easy to use, but is slow in execution.  Pretty much what I understand is that the easier the language, the less powerful it is.  The web developer program looks fun.  I'll definately try that out.  Hmmm, I wonder what the big difference is between the free visual studio compilers and the $300 visual studio professional compilers.

 

Offline Mathwiz6

  • Pees numbers
  • 27
Re: I wrote my first bit of source code!
Get the 7 page python X and O game away from me *shivers*

Gah... the redundancy... the 8 lines for making a sprite class... the 2 pages devoted to AI... the 2 pages devoted to making an endgame message...

Better than my friend did in Javascript though... 8 pages, seperate object classes for every potential X or O.. and no AI :D

 
Re: I wrote my first bit of source code!
If you want to do java... do it the right way with Sun's Java... Just don't expect to do great fast gui with it, it's great for console stuff.

Now I know a lot of people hate VB.. the newer VB# is better, since it's more object based for one thing.
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!
Okay, I made a new program for my next assignment, and I found some new information.  First the program:
Code: [Select]
//setup
#include<cstdio>
#include<cmath>
int main(void)
{

//declare variables
double DataIn, Average, Sum;
int    NGrades, Value;

//print title
printf ("Program \x22");
printf ("Average\x22 \n\n");
printf ("This program calculates the average of an arbitrary number\n");
printf ("of grades.\n\n");
printf ("After all the grades have been entered, any negative number\n");
printf ("is entered, and the averaging is performed.\n\n");

//get input
NGrades = 0;
Sum = 0.0;
Value = 0;

while (Value == 0)
{
scanf ("%lf", &DataIn);
if (DataIn > 0)
{
Value = 0;
Sum = DataIn + Sum;
++NGrades;
}
else (++Value);
}

//calculate
Average = Sum / NGrades;

//output data
switch (NGrades)
{
case 0:
printf ("\nNo valid grades have been entered.\n\n");
printf ("An average cannot be computed.");
break;
case 1:
printf ("\nThere is only one grade whose value is %4.2lf.", Average);
break;
default:
printf ("\nThere are %d grades, with an average of %4.2lf.", NGrades, Average);
}
printf ("\n\n");
return 0;
}

This one is much shorter, but it took me forever to figure out how to write the while loop correctly.  I also learned more about Visual C++ 8, aka VC++ 2005.  The only real difference between the free version and the $300 version, so far as I can tell, is that the free version doesn't have the ANSI-C/C++ libraries.  That's okay, because the CLI libraries are for the most part identical, you just have to remember to use iostream and not iostream.h and cmath instead of math.h, etc.  Also, for iostream you have to add the line
Code: [Select]
using namespace std; otherwise you cin's and cout's won't work.  I also figured out why the consoles were always closing after executing instead of having that "Press any key to continue" statement at the end.  That was because I was pressing F5 which runs the "real program" instead of CTRL-F5 which runs the "troubleshooting program" which does have that statement at the end.  The same thing happens in my studen VC++ 6.0, so everything's cool now.

 
Re: I wrote my first bit of source code!
You should be able to do
Code: [Select]
std::cout <<
std::cin >>


Also what happens if the first grade entered was a -1?   :drevil:
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!
You should be able to do
Code: [Select]
std::cout <<
std::cin >>


Also what happens if the first grade entered was a -1?   :drevil:

It's easier just to put "using namespace std;" in the header and go on like normal, I think.  If you put -1 as the only grade, then case 0 of the switch is executed.  If you put one valid grade, and then a negative number, case 1 is executed.  If you put in more than one valid grade, and then a negative number, then the default is executed.

 
Re: I wrote my first bit of source code!
Personally thats the way I do it to... although you'd get a few purists complaing....

If it looks like it does... -1 will leave NGrades to 0 and you'll never get as far as the switch statement, you'd get an exception occuring before that. (Hint remember your math  :) )
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!
Personally thats the way I do it to... although you'd get a few purists complaing....

If it looks like it does... -1 will leave NGrades to 0 and you'll never get as far as the switch statement, you'd get an exception occuring before that. (Hint remember your math  :) )

What kind of compiler are you using?  It doesn't do that to me.  If you look at my while loop, notice that it has an if-else statement embedded in it.  (Figuring out how to make this work took me a day and a half!)  What exits the loop is if you can get the integer called "Value" to be greater than 0.  Notice in the if else statement, it does the math if and only if the value entered for DataIn is greater than zero (I just realized it should be >=0), if DataIn is not greater than zero, then the else statement is executed, and no math is performed.  What the else statement does is increase value by one, making value equal to one.  This breaks the loop.  Simultaneously, since no math was performed, NGrades does not increase and remains zero.  So what happens is if the first thing you enter is a negative number, no math is performed, it exits the loop, and case 0 of the switch is executed, where it says

"No valid grades have been entered.

An average cannot be computed."

BTW, I just found out another trick so I don't have to use the hex code for ascii characters.  (Why didn't my prof. tell us about this earlier?)  So here's my new finalized code.
Code: [Select]
//setup
#include<cstdio>
#include<cmath>
int main(void)
{

//declare variables
double DataIn, Average, Sum;
int    NGrades, Value;

//print title
printf ("Program \"Average\" \n\n");
printf ("This program calculates the average of an arbitrary number\n");
printf ("of grades.\n\n");
printf ("After all the grades have been entered, any negative number\n");
printf ("is entered, and the averaging is performed.\n\n");

//get input
NGrades = 0;
Sum = 0.0;
Value = 0;

while (Value == 0)
{
scanf ("%lf", &DataIn);
if (DataIn >= 0)
{
Value = 0;
Sum = DataIn + Sum;
++NGrades;
}
else (++Value);
}

//calculate
Average = Sum / NGrades;

//output data
switch (NGrades)
{
case 0:
printf ("\nNo valid grades have been entered.\n\n");
printf ("An average cannot be computed.");
break;
case 1:
printf ("\nThere is only one grade whose value is %4.2lf.", Average);
break;
default:
printf ("\nThere are %d grades, with an average of %4.2lf.", NGrades, Average);
}
printf ("\n\n");
return 0;
}

 
Re: I wrote my first bit of source code!
Ok theres something funky going on here....

NGrades is set to 0
and Average = Sum / NGrades works correctly  :confused:

It should throw a divide by zero error.
Quote
Division by 0 in either a division or a modulus expression is undefined and causes a run-time error. Therefore, the following expressions generate undefined, erroneous results:
i % 0
f / 0.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!"