C
C#16mo ago
sunny innit

✅ 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
sunny innit
sunny innit16mo ago
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.
Angius
Angius16mo ago
Well, I don't see "The value you entered doesn't exist" anywhere, so that could be the issue
sunny innit
sunny innit16mo ago
its 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
Angius
Angius16mo ago
Where's myBool declared?
sunny innit
sunny innit16mo ago
do you want the full code?
Angius
Angius16mo ago
Would probably be useful, yeah Also, $debug
MODiX
MODiX16mo ago
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.
Angius
Angius16mo ago
Debugging will help you find the issue
sunny innit
sunny innit16mo ago
BlazeBin - hkkpfvulfhnt
A tool for sharing your source code with the world!
sunny innit
sunny innit16mo ago
I'll try that too, need to learn debugging anyway
Angius
Angius16mo ago
Well, if at any point myBool is false, it exits the very outer loop So the code will never even get to the inner check
sunny innit
sunny innit16mo ago
hmmmm but where is it that? i've been staring at this for like 4 hours hahaha
Angius
Angius16mo ago
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 not
sunny innit
sunny innit16mo ago
Fixed it, thanks a ton for your constructive help ❤️ @Angius