Matt
Matt

Reputation: 19

C# String multiplication error

I have an assignment for TAFE in which i have to create a console program in Visual Studio that calculates the cost of a consultation based on consultation time (at $25 an hour).

string hours, rate, total;
Console.Write("Enter consultation time in hours");
hours = Console.ReadLine();
rate = ;
total = hours * rate;
Console.WriteLine("Fee is " + total);

My problem is on line 5, I get the error "operator '*' cannot be applied to operands of type 'string' and 'string;"

Can someone explain to me what is wrong, why 2 strings cant be multiplied and also supply an alternate method of getting that line to work?

EDIT: Thank you everyone. All info given was helpful. I've completed it leaving the rate as an Integer as its a set value not changed by user input, the hours and total are still strings, The hours converting to decimal via the convert.ToDecimal line GianlucaBobbio gave me. The only problem now is console doesnt stay open after calculating rate * hour, but i can fix that.

You may just have a new regular user :D! appreciate the help. you're all life savers :)

Upvotes: 1

Views: 1028

Answers (4)

GianlucaBobbio
GianlucaBobbio

Reputation: 184

You should convert both strings to decimal, so you can multiple them and finally convert them to string again. So you can use de Convert method.

string hours, rate, total;
Console.Write("Enter consultation time in hours");
hours = Console.ReadLine();
rate = ;
total = Convert.ToString(Convert.ToDecimal(hours) * Convert.ToDecimal(rate));
Console.WriteLine("Fee is " + total);

To handle if he puts a valid number you can use decimal.TryParse(string, decimal) which returns a bool value, and if the string is a decimal value it'll be in the out variable like decimal

string hours, rate, total;
Console.Write("Enter consultation time in hours");
hours = Console.ReadLine();
rate = ;
decimal hourInput = 0;
if(!decimal.TryParse(hours, out hourInput)
{
    Console.Write("Thats not a number!");
}
else
{
    total = Convert.ToString(hourInput * Convert.ToDecimal(rate));
    Console.WriteLine("Fee is " + total);
}

Sorry for my bad english

Upvotes: 2

Dan J
Dan J

Reputation: 16708

C#'s type system doesn't allow for a lot of inference; unlike JavaScript or Ruby, for example, it won't implicitly convert strings to numeric types when you attempt to perform arithmetic on them. Instead, you must explicitly convert the user's input from a String to some numeric type. Here's one approach:

string hours;
decimal numericHours, rate, total;

Console.Write("Enter consultation time in hours");
hours = Console.ReadLine();
if (!Decimal.TryParse(hours, out numericHours))
{
    Console.WriteLine(String.Format("{0} doesn't appear to be a valid number of hours. Please enter a numeric value.", hours));
}
else
{
    rate = 35.6;
    total = numericHours * rate;
    Console.WriteLine("Fee is " + total);
}

That if statement deserves some further explanation: Decimal.TryParse() is one of the rare methods in the .NET framework that has two effects: it returns true or false depending on whether the first parameter can be successfully parsed as data of type decimal. The second parameter, decorated with the out keyword, will contain the resulting decimal value if the parse was successful.

A couple of bonus .NET tips: Notice the use of String.Format() to control the insertion of variables into strings - I find it nicer to use than the + string-concatenation operator. Also, as other answers have pointed out, arithmetic is performed somewhat differently on double and decimal types, due to different binary representations. When dealing with precise fractional values, especially monetary, prefer the decimal type.

Upvotes: 1

Icarus
Icarus

Reputation: 63956

Console.ReadLine() returns a string so naturally, you can't multiply a string with a number. You have to parse the string as a double/int/float/decimal (whatever the case is) and then do the multiplication.

One example would be:

double hours = 0.0;
if(double.TryParse(Console.ReadLine(),out hours))
{
   total = (decimal)hours * rate; 
   Console.WriteLine("Fee is " + total);
}
else 
   Console.WriteLine("You did not enter a valid number of hours.");

Since this is a calculation about money, your rate and total variables should be of type decimal.

Upvotes: 3

CodeCaster
CodeCaster

Reputation: 151586

You're looking for a numeric type like int or decimal. You'll need to parse those from the input.

Upvotes: 6

Related Questions