❔ I have implemented a project and need to solve these errors

{
int antalOrd, summa;
List<string> ordLista = new List<string>();
Console.WriteLine("Välkommen till Räknaren!");
Console.Write("Hur många ord vill su skriva in? Antal: ");
Int32.TryParse(Console.ReadLine(), out antalOrd);
for (int i = 0; i < antalOrd; i++)
{
Console.WriteLine("Skriv in ord nr." + (i + 1) + ": ");
ordLista.Add(Console.ReadLine());
}
if (ordLista.Count > 0)
int summa = RäknaBokstäver(ordLista);
Console.WriteLine("Alla ord du har skrivit har totalt " + summa + " symboler.");
else
Console.WriteLine("Du har valt att inte skriva några ord.");
Console.ReadLine();
}
static void RäknaBokstäver(List<string> mottagenLista)
{
summa = 0;
for (int i = 0; i < mottagenLista.Count; i++)
{
summa += mottagenLista[i].Length;
return summa;
}
}
{
int antalOrd, summa;
List<string> ordLista = new List<string>();
Console.WriteLine("Välkommen till Räknaren!");
Console.Write("Hur många ord vill su skriva in? Antal: ");
Int32.TryParse(Console.ReadLine(), out antalOrd);
for (int i = 0; i < antalOrd; i++)
{
Console.WriteLine("Skriv in ord nr." + (i + 1) + ": ");
ordLista.Add(Console.ReadLine());
}
if (ordLista.Count > 0)
int summa = RäknaBokstäver(ordLista);
Console.WriteLine("Alla ord du har skrivit har totalt " + summa + " symboler.");
else
Console.WriteLine("Du har valt att inte skriva några ord.");
Console.ReadLine();
}
static void RäknaBokstäver(List<string> mottagenLista)
{
summa = 0;
for (int i = 0; i < mottagenLista.Count; i++)
{
summa += mottagenLista[i].Length;
return summa;
}
}
16 Replies
netotz
netotz2y ago
please reformat your code
The king of kings
Now, looks much better.
Thinker
Thinker2y ago
Do you know what void means? Your issue is that RäknaBokstäver returns void, and you're trying to assign it to int.
The king of kings
Oh! Sorry, I forgot that part. Yes, void is not a return method.
Thinker
Thinker2y ago
I think you probably want RäknaBokstäver to return int, because it should, well, count the amount of letters
The king of kings
Ok How about these the declaration of these variables int antalOrd, summa; ?
Thinker
Thinker2y ago
Well, your second issue is that summa is not a variable in the RäknaBokstäver method, because you haven't declared it there. You can solve this quite simply, by just declaring summa inside RäknaBokstäver.
The king of kings
Ok! You're right it makes sense. Is there more issues except for the void and summa?
Thinker
Thinker2y ago
Not in terms of errors at least There are some faults in your logic, and some minor style nitpicks, however nothing terrible.
The king of kings
Ok! Could you please point at these issues? For example the if and else are missing curly brackets
Thinker
Thinker2y ago
actually there is one error, you're declaring summa twice, once at the top of Main and once inside the if You also need braces around the int summa = ... and Console.WriteLine("Alla ord du har... lines
The king of kings
How to solve this issue?
Thinker
Thinker2y ago
Well, declare it only once Think about where you actually need the variable. You're only using it twice, once when you assign to it, and once when you use it in a Console.WriteLine
The king of kings
Ok Thanks a lot man, I really appreciate your effort of helping me and the others 🙏 🙂 I learned a several things from this project
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server