Cannot implicitly convert type 'string' to 'int'

oronarosas

New member
Joined
Aug 16, 2012
Messages
2
Programming Experience
Beginner
Hey everyone Im just starting in my programming classes and am stumped on how to fix a problem. Can someone please help! Here is the project description:

This week, you need to write a program for Magic Blender company, which sells a blender for $39.95.
The program should prompt the user for the following information:

  • Name
  • Street address
  • City
  • State
  • Zip code
  • Quantity of blenders ordered
Ensure that the program displays the following:

  • All the input data
  • Amount due before tax, which is equal to the number of blenders ordered multiplied by the price of one blender
  • Sales tax, which is equal to seven percent of the amount due before tax
  • Net due, which is equal to the sum of amount due before tax and sales tax

This is what I have for code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace Magic_Blender
{
class Program
{
static void Main(string[] args)
{
string myName, streetAddress, city, state, zipCode;
double totalBlend, taxTotal, netDue;
int blendersOrdered;


Console.WriteLine("Enter Name: ");
myName = Console.ReadLine();
Console.WriteLine("Enter Street Address: ");
streetAddress = Console.ReadLine();
Console.WriteLine("City: ");
city = Console.ReadLine();
Console.WriteLine("State: ");
state = Console.ReadLine();
Console.WriteLine("Zip Code: ");
zipCode = Console.ReadLine();


Console.WriteLine("Enter Number of Purchased Blenders: ");
blendersOrdered = Console.ReadLine();


totalBlend = (blendersOrdered * 39.95);
taxTotal = (blendersOrdered * .07);
netDue = (totalBlend + taxTotal);




Console.WriteLine("Receipt for:");
Console.WriteLine(myName);
Console.WriteLine(streetAddress);
Console.WriteLine(city);
Console.WriteLine(state);
Console.WriteLine(zipCode);


Console.WriteLine(totalBlend + "blenders ordered @ $39.95ea. ");


Console.WriteLine("Total: $" + totalBlend);
Console.WriteLine("Tax: $" + taxTotal);
Console.WriteLine("---------------------------");
Console.WriteLine("Due: $:" + netDue);
Console.ReadLine();


So the problem is the highlighted area says " Error 1 Cannot implicitly convert type 'string' to 'int' "
Can someone please tell me where Im going wrong please. Much appreciated!
 
Well I figured that part out! Here's what I did (highlighted the solution code):
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace Magic_Blender
{
    class Program
    {
        static void Main(string[] args)
        {
            string myName, lstName, streetAddress, city, state, zipCode;
            double totalBlend, taxTotal, netDue;
            int blendersOrdered;


            Console.WriteLine("Enter First Name: ");
            myName = Console.ReadLine();
            Console.WriteLine("Enter Last Name: ");
            lstName = Console.ReadLine();
            Console.WriteLine("Enter Street Address: ");
            streetAddress = Console.ReadLine();
            Console.WriteLine("City: ");
            city = Console.ReadLine();
            Console.WriteLine("State: ");
            state = Console.ReadLine();
            Console.WriteLine("Zip Code: ");
            zipCode = Console.ReadLine();


            Console.WriteLine("Enter Number of Purchased Blenders: ");
[COLOR=#ff0000]            blendersOrdered = Convert.ToInt32(Console.ReadLine());[/COLOR]


            totalBlend = (blendersOrdered * 39.95);
            taxTotal = (totalBlend * .07);
            netDue = (totalBlend + taxTotal);




            Console.WriteLine("Receipt for:");
            Console.WriteLine(myName + ("_"+lstName));
            Console.WriteLine(streetAddress);
            Console.WriteLine(city);
            Console.WriteLine(state);
            Console.WriteLine(zipCode);


            Console.WriteLine(blendersOrdered + " blenders ordered @ $39.95ea. ");


            Console.WriteLine("Total:     $" + totalBlend); 
            Console.WriteLine("Tax:       $" + taxTotal);
            Console.WriteLine("---------------------------");
            Console.WriteLine("Due:       $:" + netDue);
            Console.ReadLine();
Now the only problem I am having is figuring out how to get the currency to show right. Any advice?
 
Last edited by a moderator:
One issue with the solution you have is that it will throw an exception if the value entered by the user is not a valid number, e.g. an empty string or "Hello World". If this is a learning exercise and you have been told that you can assume that the input will be valid then that's fine but, in a real world app, you'd have to validate the input before converting. The int.TryParse method can do the validation and conversion in one method call.

As for displaying numbers, you can call the ToString method of the number itself to create a string representation and you can pass a standard or custom format specifier to that method. MSDN has all the information you need on format strings for numbers, dates and times but, in this specific case, "c" will format the number using the standard currency format for the local machine. In countries with "dollars and cents" style currency that will usually include two decimal places. You can also specify the exact number of decimal places, e.g. "c0" for just "dollars" and no "cents" or "c2" for always 2 decimal places.
 
Also, please wrap your code snippets in formatting tags ([code][/code] supports other formatting while [xcode=c#][/xcode] does not but provides syntax highlighting) for readability, as I have done for you in your previous post.
 
Back
Top Bottom