![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Newbie
Join Date: May 2006
Location: Central Valley, Kalifornia
Posts: 13
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#2 |
|
Professional Programmer
Join Date: Oct 2005
Location: Chitown
Posts: 422
Rep Power: 4
![]() |
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);
}
} |
|
|
|
|
|
#3 | |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Quote:
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. |
|
|
|
|
|
|
#4 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 770
Rep Power: 3
![]() |
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"
|
|
|
|
|
|
#5 |
|
Newbie
Join Date: May 2006
Location: Central Valley, Kalifornia
Posts: 13
Rep Power: 0
![]() |
: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. |
|
|
|
|
|
#6 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Quote:
__________________
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 |
|
|
|
|
|
|
#7 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
I learn, eventually.
![]() |
|
|
|
|
|
#8 | |
|
Newbie
Join Date: May 2006
Location: Central Valley, Kalifornia
Posts: 13
Rep Power: 0
![]() |
Quote:
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). |
|
|
|
|
|
|
#9 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
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.
|
|
|
|
|
|
#10 | |
|
Expert Programmer
|
Quote:
|
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|