I'm new to c# and I have the following code:
using System;
public class Program
{
public static void Main()
{
int precio;
int cantidadarticulo;
int subtotal;
int siniva;
int coniva;
int total;
Console.WriteLine("dame nombre : ");
string nombrepedido = Console.ReadLine();
Console.WriteLine("dame precio : ");
string preciopedidos = Console.ReadLine();
precio = Convert.ToInt32(preciopedidos);
Console.WriteLine("dame cantidad de cualquier articulo : ");
string cantidadarticulopedido = Console.ReadLine();
cantidadarticulo = Convert.ToDouble(cantidadarticulopedido);
//calcular subtotal
subtotal = cantidadarticulo/16;
and if you see in this next line I'm using toDouble to convert a String to a double to get the subtotal.
cantidadarticulo = Convert.ToDouble(cantidadarticulopedido);
For that I am using this c# code:
Convert.ToDouble Method (String)
this is the complete code:
//+===================================================================
//
// Acerca del programa :
//
// 3. Elabore un programa que pida el nombre, el precio y la cantidad de cualquier artículo (sin iva);
// y que después informe de cuanto tiene que pagar el usuario por dicho producto: subtotal sin iva, iva y total.
//
// Autor del codigo : Gilberto Quintero Armenta
// codigo en github ( el codigo en linea ) :
// Herramienta que use para codificar el codigo c# : https://dotnetfiddle.net
//
//
//
//
//
//+===================================================================
using System;
public class Program
{
public static void Main()
{
int precio;
int cantidadarticulo;
int subtotal;
int siniva;
int coniva;
int total;
Console.WriteLine("dame nombre : ");
string nombrepedido = Console.ReadLine();
Console.WriteLine("dame precio : ");
string preciopedidos = Console.ReadLine();
precio = Convert.ToInt32(preciopedidos);
Console.WriteLine("dame cantidad de cualquier articulo : ");
string cantidadarticulopedido = Console.ReadLine();
cantidadarticulo = Convert.ToDouble(cantidadarticulopedido);
//calcular subtotal
subtotal = cantidadarticulo/16;
Console.WriteLine("subtototal: ");
Console.WriteLine("sin iva: ");
Console.WriteLine("total: ");
}
}
I do not understand why it does not work if in the documentation it indicates that I must use toDouble to convert a string to Double, where could my error be?
Well, it's simple, I'll explain:
You are declaring an integer
You are trying to assign a double to an integer.
To fix it, declare itemQuantity as double;
So:
Note: I suggest you declare your int as a decimal, since for example a price can have decimals. as well as the subtotal
Although your question has already been answered, I would like to add some suggestions that allow you to write C# code that is more readable.
To begin with, in C# it is not common to declare variables at the beginning of the
Main()
, as is usually done in C or C++, but it is common practice to declare variables when using them, as for example you did in the line:string nombrepedido = Console.ReadLine();
. Similarly, instead of declaringMain()
the variable at the beginningint cantidadarticulo
and then writing:The following code is more readable:
Writing in the above way would have made you notice your mistake of wanting to store a
double
in aint
, which is not allowed. If you use VS you will even notice that the program "complains" and gives you the error: " Cannot implicitly cast typedouble
toint
".On the other hand, it is unnecessary to create extra type variables
string
to use theConsole.ReadLine()
. Instead of writing:I suggest you write more succinctly the above code as:
The above is also common practice in C#.
Finally, regarding the name of your variables. From what I understand, when your variable name is made up of two or more words, as is the case with
cantidadarticulo
, it is not recommended to write it all in lowercase, but rather to use the sub-hyphen(_)
to separate each word or capitalize the first letter of each. word from the second. For example, in the case of the variablecantidadarticulo
, it is better to write eithercantidad_articulo
orcantidadArticulo
.