7 Replies - 504 Views - Last Post: 17 September 2010 - 02:57 PM Rate Topic: -----

#1 Guest_Gabe*


Reputation:

C# invalid code

Posted 17 September 2010 - 01:59 PM

I'm having problems with this code, it keeps telling me I have an invalid token "="


this is the part of code I'm having trouble with

  static void CalcTotalCost,DiscountedCost,TaxAndAmountDue();
  {
    totalCost      = numberOfItems * itemPrice;
    discountedCost = totalCost * (1 - discountRate);
    tax            = discountedCost * TAX_RATE;
    amountDue      = discountedCost + tax;
  }


MOD EDIT: When posting code...USE CODE TAGS!!!

:code:

Is This A Good Question/Topic? 0

Replies To: C# invalid code

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6092
  • View blog
  • Posts: 23,614
  • Joined: 23-August 08

Re: C# invalid code

Posted 17 September 2010 - 02:06 PM

Yup. that's invalid code alright.

You can't have commas in a method name, and a method definition should not be followed by a ;
Was This Post Helpful? 0
  • +
  • -

#3 Guest_Dan*


Reputation:

Re: C# invalid code

Posted 17 September 2010 - 02:08 PM

This is probably going to be a stupid question, but bear with me as I am currently trying to learn the c# language, but I may be able to help.

What is this line

static void CalcTotalCost,DiscountedCost,TaxAndAmountDue();




trying to do?

what are CalcTotalCost and DiscountedCost?

Again, sorry if this is a stupid question (probably is)
Was This Post Helpful? 0

#4 rgfirefly24  Icon User is offline

  • D.I.C Lover
  • member icon


Reputation: 285
  • View blog
  • Posts: 1,503
  • Joined: 07-April 08

Re: C# invalid code

Posted 17 September 2010 - 02:11 PM

That is an invalid Attempt at creating a Method name. We can't tell you exactly what CalcTotalCost and DiscountedCost are as those are made up by the developer who created that attempt at code.

Is this your code? I would suspect not since you have no idea what it is, and in that case i would suggest you don't ever use code from the place/programmer as they are giving you horribly wrong code.

This post has been edited by rgfirefly24: 17 September 2010 - 02:13 PM

Was This Post Helpful? 1
  • +
  • -

#5 Guest_Gabe*


Reputation:

Re: C# invalid code

Posted 17 September 2010 - 02:19 PM

Thanks for the help.
I just started programming my self. I'm not for sure what that line is trying to do. I'm trying to create a program "that prompts the user for an item price, number of items and a discount rate. the program should then calculate and print the total cost, discount cost, tax and amount due for the purchase."
Was This Post Helpful? 0

#6 Guest_dan*


Reputation:

Re: C# invalid code

Posted 17 September 2010 - 02:32 PM

Is is a console application? Is the discount cost the value of the discount and what do you mean by amount due? Is is the total cost after discount?
Was This Post Helpful? 0

#7 Guest_Dan*


Reputation:

Re: C# invalid code

Posted 17 September 2010 - 02:36 PM

I apologise, it's quite obvious from your code what all of those things are...sorry, it's late...ignore me.
Was This Post Helpful? 0

#8 Guest_Dan*


Reputation:

Re: C# invalid code

Posted 17 September 2010 - 02:57 PM


//declare your methods like this if you don't want them to return anything (shown by return type of void). No semicolon after method declarations.
       
 public void CalcTotalCost(){

            //give your variables a type (type in below case is double which support decimal numbers between 5.0  10−324 to 1.7  10308 )
             double totalCost;
       }




If it is a console application, then you can use:

Console.ReadLine(); 



which reads the previous line entered into the console (I think!).

You would then assign the input that has been read to a variable so you can do calculations with it. However, the Console.ReadLine(); method returns the input as a string, therefore, to do calculations with the input, you need to convert it to a number format first.

See example below:

Console.WriteLine("Please Enter the number of items:" //asks user for input
double numberOfItems = double.Parse(Console.ReadLine());  //converts user input to double format and stores it in the numberOfItems variable




Once you have collected all the user input required,, you can use the variables (which will have all the user input stored) to do calculations like:

double totalCost = numberOfItems * itemPrice;



I hope that helps a little bit at least...
Was This Post Helpful? 1

Page 1 of 1