Hi everyone. Today i had my first exam going over the basics and i stumbled on this small project.
The idea is you input up to 5 number (0-9), if less are given we add 0’s in the front.
Then we add them up individually, so for example 12345 becomes 1+2+3+4+5 and the total is 15.
Its supposed to be very simple but i have no clue what is going wrong here.
Any help is explaining is much appreciated.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TussentijdseEvaluatie1
{
public class Permanente
{
public static void SomVanCijfers()
{
Console.WriteLine("Gelieve een getal in te voeren dat bestaat uit exact 5 decimale cijfers.");
string getalInput = Console.ReadLine();
while (getalInput.Length < 5)
{
getalInput = $"0{getalInput}";
}
// check variable getalInput after the loop (value and type)
Console.WriteLine(getalInput);
Console.WriteLine(getalInput.GetType());
// converting to ints (after it kept failing) i guess it starts failing here but i'm not sure why?
int getalInput1 = Convert.ToInt16(getalInput[0]);
int getalInput2 = Convert.ToInt16(getalInput[1]);
int getalInput3 = Convert.ToInt16(getalInput[2]);
int getalInput4 = Convert.ToInt16(getalInput[3]);
int getalInput5 = Convert.ToInt16(getalInput[4]);
// Just making sure its converted
Console.WriteLine(getalInput1.GetType());
Console.WriteLine(getalInput2.GetType());
Console.WriteLine(getalInput3.GetType());
Console.WriteLine(getalInput4.GetType());
Console.WriteLine(getalInput5.GetType());
// checking if the numbers are correct (They are)
Console.WriteLine(getalInput1);
Console.WriteLine(getalInput2);
Console.WriteLine(getalInput3);
Console.WriteLine(getalInput4);
Console.WriteLine(getalInput5);
// this was so i could see what exactly i was adding up.
Console.WriteLine($"De som is: {getalInput1} + {getalInput2} + {getalInput3} + {getalInput4} + {getalInput5}");
Console.WriteLine($"De som is: {Convert.ToInt16(getalInput[0]) + Convert.ToInt16(getalInput[1]) + Convert.ToInt16(getalInput[2]) + Convert.ToInt16(getalInput[3]) + Convert.ToInt16(getalInput[4])}");
}
}
}