✅ Case 3 of my code.
So im trying to create a program within Visual studios in c# as part of an assignment.
The program is supposed to create and present a String vector;
1) tells you to enter 5 elements
2) lists all 5 elements you entered
3) lets you search within these indexes, incase it's one of the 5 elements you entered.
If it finds it it tells gives you a message i wrote where it says "Success, the value is present in index #", If not it should tell you that the value you entered doesnt exist.
4) Quits the program.
Now the program works flawlessly; Except that when you use 3) it doesn't prompt the "The value you entered doesn't exist.
14 Replies
case 3:
Console.Write("Vad vill du söka efter?: ");
string searchWord = Console.ReadLine();
for (int i = 0; i < myVector.Length; i++)
{
if (searchWord == myVector[i])
{
Console.WriteLine($"Sökningen lyckades! {searchWord} is at index {i}");
myBool = true;
}
}
if (myBool == false)
{
Console.WriteLine($"Fel: {searchWord} siffran du angav finns inte");
myBool = false;
}
that's the entirety of case 3.
Well, I don't see
"The value you entered doesn't exist"
anywhere, so that could be the issueits right below the if (myBool == false)
but i assume that line is incorrect
Fel: {searchWord} siffran du angav finns inte <-- the text the user sees is in Swedish 🙂 but thats the one
Where's
myBool
declared?do you want the full code?
Would probably be useful, yeah
Also, $debug
Tutorial: Debug C# code - Visual Studio (Windows)
Learn features of the Visual Studio debugger and how to start the debugger, step through code, and inspect data in a C# application.
Debugging will help you find the issue
BlazeBin - hkkpfvulfhnt
A tool for sharing your source code with the world!
I'll try that too, need to learn debugging anyway
Well, if at any point
myBool
is false
, it exits the very outer loop
So the code will never even get to the inner checkhmmmm but where is it that? i've been staring at this for like 4 hours hahaha
Line 16
If
myBool
is set to false
anywhere, line 53 won't execute
So either the if
in line 53 is unnecessary or that's the issue
You probably want to introduce another boolean there
One that specifically stores the information about whether the item was inside of the array or notFixed it, thanks a ton for your constructive help ❤️ @Angius