Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 13th, 2006, 12:15 AM   #1
52Degrees
Newbie
 
Join Date: May 2006
Location: Central Valley, Kalifornia
Posts: 13
Rep Power: 0 52Degrees is on a distinguished road
My silly, simple gas price calculator

I am sooo just beginning, thought I'd write a cute little program to estimate the cost to fill my gas tank. Unfortunately, it doesn't work. I was hoping someone could give me a pointer or two.

using System;

class GasPrices
{
    static void Main()
    {
        // c is current price per gallon, g is number of gallons
        Console.Write ("Enter price per gallon: ");
        decimal C = Int32.Parse(Console.ReadLine());
        Console.Write ("Enter price per gallon: ");
        decimal G = Int32.Parse(Console.ReadLine());
        decimal Total = C * G;
        Console.WriteLine ("The total cost is $" + Total);
    }
}

I'm assuming that my problem has something to do with incorrect format converting the price to an int. Hope I don't get too badly flamed for my lack of knowledge...

Thanks
52Degrees is offline   Reply With Quote
Old May 13th, 2006, 12:31 AM   #2
Wizard1988
Professional Programmer
 
Wizard1988's Avatar
 
Join Date: Oct 2005
Location: Chitown
Posts: 422
Rep Power: 4 Wizard1988 is on a distinguished road
Thumbs up

You should use the datatype float or double instead of decimal and parse it as a float or double.

using System;

class GasPrices
{
    static void Main()
    {
        // c is current price per gallon, g is number of gallons
        Console.Write("Enter price per gallon: ");
        double C = float.Parse(Console.ReadLine());
        Console.Write("Enter price per gallon: ");
        double G = float.Parse(Console.ReadLine());
        double Total = C * G;
        Console.WriteLine("The total cost is $" + Total);
    }
}
Wizard1988 is offline   Reply With Quote
Old May 13th, 2006, 12:31 AM   #3
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 770
Rep Power: 3 Jimbo is on a distinguished road
I'm guessing you want to use Decimal.Parse instead of Int32.Parse. Also, your second prompt should be "Enter the number of gallons:" instead of "Enter the price per gallon"
Jimbo is offline   Reply With Quote
Old May 13th, 2006, 12:57 AM   #4
52Degrees
Newbie
 
Join Date: May 2006
Location: Central Valley, Kalifornia
Posts: 13
Rep Power: 0 52Degrees is on a distinguished road
:banana:

Decimal.Parse did it! Thanks!

Great catch on the number of gallons quote. I must have been copying the first one.

This is fun stuff. I gotta learn some more though.
52Degrees is offline   Reply With Quote
Old May 13th, 2006, 6:49 AM   #5
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Thumbs up

Quote:
Originally Posted by Wizard1988
You should use the datatype float or double instead of decimal and parse it as a float or double.
Just to sort something out: the Decimal type is designed for currencies. Because it stores each digit as, I believe, binary-coded decimal, it's not subject to the errors that can sometimes crop up when dealing with floating-point numbers.

52Degrees, glad ya got it figured out. There's a problem with your code though: if the user doesn't type a number, it'll crash. Check out the Decimal.TryParse method.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old May 13th, 2006, 7:40 AM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Quote:
if the user doesn't type a number, it'll crash.
That's MY line! :p
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old May 13th, 2006, 8:42 AM   #7
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
I learn, eventually.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old May 13th, 2006, 11:08 AM   #8
52Degrees
Newbie
 
Join Date: May 2006
Location: Central Valley, Kalifornia
Posts: 13
Rep Power: 0 52Degrees is on a distinguished road
Quote:
Check out the Decimal.TryParse method.
I'll have to look that one up. I tried just using TryParse instead of Parse but got compile errors.

I'm using "Programming in the Key of C#" by Charles Petzold. I'm only on chapter 14. Reading to learn takes me longer than reading for entertainment I'd like to take a class, but the local college isn't offering C# yet, only C++.

I ran into a guy from Sun Microsystems the other day. Sort of a friend of a friend. He gave me his number and told me he'd hook me up with a couple of local guys in charge of training people on J2EE (whatever that is).
52Degrees is offline   Reply With Quote
Old May 13th, 2006, 12:10 PM   #9
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
J2EE stands for "Java 2, Enterprise Edition", I believe. Java's a language similar to C# - it's what C# is based on, in fact. It's worth learning both.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old May 13th, 2006, 12:28 PM   #10
Booooze
Expert Programmer
 
Booooze's Avatar
 
Join Date: Mar 2006
Location: Igloo
Posts: 710
Rep Power: 3 Booooze is on a distinguished road
Send a message via MSN to Booooze
Quote:
Originally Posted by Ooble
J2EE stands for "Java 2, Enterprise Edition", I believe. Java's a language similar to C# - it's what C# is based on, in fact. It's worth learning both.
Many 'rumours' around, but C# is said to be the 'MS Java'. I've done Java, and I'm doing C#, I think I'm having such an easy time with C# because I've done Java. VB helped because it's the same environment. Althought I admit, I think it's easier to find Java resources on the net then C#.
Booooze is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:16 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC