Results 1 to 11 of 11

Thread: VS 2010 If Else If Issue

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2014
    Posts
    73

    Question VS 2010 If Else If Issue

    So , I Have A Quiz Application ive been working on and I just ran into a problem , when it calculates your score at the bottom the % score is always correct , but if you receive over a certain % (65) then it will say you've passed that level , but when its over that amount , it says That you've failed that level , any ideas ?


    Code:
          Console.ForegroundColor = ConsoleColor.Cyan
                            Console.WriteLine("Level 3")
                            Console.ForegroundColor = ConsoleColor.Yellow
                            Console.WriteLine("About How Many Minutes Does It Take For Sunlight To Reach Earth ?")
                            two1 = Console.ReadLine.ToLower
                            If two1 = "8" Then
                                Console.ForegroundColor = ConsoleColor.Green
                                Console.WriteLine("Correct!")
                                score13 = 1
                            Else
                                Console.ForegroundColor = ConsoleColor.Red
                                Console.WriteLine("Incorrect")
                                Console.ForegroundColor = ConsoleColor.Magenta
                                Console.WriteLine("The Correct Answer Was : 8")
                                score13 = 0
                            End If
                            Console.ForegroundColor = ConsoleColor.Yellow
                            Console.WriteLine("In Which U.S. State Is The Golden Gate Bridge Located In ?")
                            two2 = Console.ReadLine.ToLower
                            If two2 = "california" Then
                                Console.ForegroundColor = ConsoleColor.Green
                                Console.WriteLine("Correct!")
                                score14 = score13 + 1
                            Else
                                Console.ForegroundColor = ConsoleColor.Red
                                Console.WriteLine("Incorrect")
                                Console.ForegroundColor = ConsoleColor.Magenta
                                Console.WriteLine("The Correct Answer Was : California")
                                score14 = score13 + 0
                            End If
                            Console.ForegroundColor = ConsoleColor.Yellow
                            Console.WriteLine("Which Planet In our Solar System Spins The Fastest ?")
                            two3 = Console.ReadLine.ToLower
                            If two3 = "jupiter" Then
                                Console.ForegroundColor = ConsoleColor.Green
                                Console.WriteLine("Correct!")
                                score15 = score14 + 1
                            Else
                                Console.ForegroundColor = ConsoleColor.Red
                                Console.WriteLine("Incorrect")
                                Console.ForegroundColor = ConsoleColor.Magenta
                                Console.WriteLine("The Correct Answer Was : Jupiter")
                                score15 = score14 + 0
                            End If
                            Console.ForegroundColor = ConsoleColor.Yellow
                            Console.WriteLine("What Is The Eastern Most Capital In The United States ?")
                            two4 = Console.ReadLine.ToLower
                            If two4 = "augusta" Then
                                Console.ForegroundColor = ConsoleColor.Green
                                Console.WriteLine("Correct!")
                                score16 = score15 + 1
                            Else
                                Console.ForegroundColor = ConsoleColor.Red
                                Console.WriteLine("Incorrect")
                                Console.ForegroundColor = ConsoleColor.Magenta
                                Console.WriteLine("The Correct Answer Was : Augusta")
                                score16 = score15 + 0
                            End If
                            Console.ForegroundColor = ConsoleColor.Yellow
                            Console.WriteLine("What Is The Full Name Of The Person Who Painted The Famous Painting Named The Mona Lisa")
                            two5 = Console.ReadLine.ToLower
                            If two5 = "leonardo da vinci" Then
                                Console.ForegroundColor = ConsoleColor.Green
                                Console.WriteLine("Correct!")
                                score17 = score16 + 1
                            Else
                                Console.ForegroundColor = ConsoleColor.Red
                                Console.WriteLine("Incorrect")
                                Console.ForegroundColor = ConsoleColor.Magenta
                                Console.WriteLine("The Correct Answer Was : Leonardo Da Vinci")
                                score17 = score16 + 0
                            End If
                            Console.ForegroundColor = ConsoleColor.Yellow
                            Console.WriteLine("How Many Squares Are There On A Standard Chessboard ?")
                            two6 = Console.ReadLine.ToLower
                            If two6 = "64" Then
                                Console.ForegroundColor = ConsoleColor.Green
                                Console.WriteLine("Correct!")
                                score18 = score17 + 1
                            Else
                                Console.ForegroundColor = ConsoleColor.Red
                                Console.WriteLine("Incorrect")
                                Console.ForegroundColor = ConsoleColor.Magenta
                                Console.WriteLine("The Correct Answer Was : 64")
                                score18 = score17 + 0
                            End If
                            Console.WriteLine()
                            Console.ForegroundColor = ConsoleColor.Yellow
                            Console.WriteLine("Your Score Was:")
                            Console.ForegroundColor = ConsoleColor.Yellow
                            Console.WriteLine("Percentage Form")
                            Console.ForegroundColor = ConsoleColor.DarkYellow
                            Dim scorel3 As Integer = score18 / 6 * 100
                            Console.WriteLine(score18 / 6 * 100)
                            If score18 < 65 Then
                                Console.ForegroundColor = ConsoleColor.Red
                                Console.WriteLine("You Failed Level 3")
                                Console.ForegroundColor = ConsoleColor.Gray
                                Console.WriteLine("----------------------------------------------")
                                Console.ForegroundColor = ConsoleColor.Red
                                Console.WriteLine("NOTE : Using Any Type Of Search Engine Or The Internet To Help You Is Cheating !")
                                Console.ReadLine()
                            End If
                            If score18 >= 65 Then
                                Console.ForegroundColor = ConsoleColor.Green
                                Console.WriteLine("You Passed Level 3")
                                Console.ForegroundColor = ConsoleColor.Gray
                                Console.WriteLine("----------------------------------------------")
                                Console.ForegroundColor = ConsoleColor.Red
                                Console.WriteLine("NOTE : Using Any Type Of Search Engine Or The Internet To Help You Is Cheating !")
                                Console.ForegroundColor = ConsoleColor.Gray
                                Console.WriteLine("----------------------------------------------")
                            End If
                        End If
    
                    End If
                End If
            End If
    Last edited by Jasonpaul2001; Nov 26th, 2014 at 09:25 AM.

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,956

    Re: VS 2010 If Else If Issue

    suggestion - edit your post, and put [code][/code] or [highlight=vb.net][/highlight] tags around your code. I'mnotgoingtotrytoreaditwithoutanyformattingespeciallywithnestedifs.

    OR, you can set some breakpoints and step through the code to determine where the logic is wrong.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,471

    Re: VS 2010 If Else If Issue

    Posting that with code tags will be move conducive to getting someone to respond.

    Code:
    Console.ForegroundColor = ConsoleColor.Cyan
            Console.WriteLine("Level 3")
            Console.ForegroundColor = ConsoleColor.Yellow
            Console.WriteLine("About How Many Minutes Does It Take For Sunlight To Reach Earth ?")
            two1 = Console.ReadLine.ToLower
            If two1 = "8" Then
                Console.ForegroundColor = ConsoleColor.Green
                Console.WriteLine("Correct!")
                score13 = 1
            Else
                Console.ForegroundColor = ConsoleColor.Red
                Console.WriteLine("Incorrect")
                Console.ForegroundColor = ConsoleColor.Magenta
                Console.WriteLine("The Correct Answer Was : 8")
                score13 = 0
            End If
            Console.ForegroundColor = ConsoleColor.Yellow
            Console.WriteLine("In Which U.S. State Is The Golden Gate Bridge Located In ?")
            two2 = Console.ReadLine.ToLower
            If two2 = "california" Then
                Console.ForegroundColor = ConsoleColor.Green
                Console.WriteLine("Correct!")
                score14 = score13 + 1
            Else
                Console.ForegroundColor = ConsoleColor.Red
                Console.WriteLine("Incorrect")
                Console.ForegroundColor = ConsoleColor.Magenta
                Console.WriteLine("The Correct Answer Was : California")
                score14 = score13 + 0
            End If
            Console.ForegroundColor = ConsoleColor.Yellow
            Console.WriteLine("Which Planet In our Solar System Spins The Fastest ?")
            two3 = Console.ReadLine.ToLower
            If two3 = "jupiter" Then
                Console.ForegroundColor = ConsoleColor.Green
                Console.WriteLine("Correct!")
                score15 = score14 + 1
            Else
                Console.ForegroundColor = ConsoleColor.Red
                Console.WriteLine("Incorrect")
                Console.ForegroundColor = ConsoleColor.Magenta
                Console.WriteLine("The Correct Answer Was : Jupiter")
                score15 = score14 + 0
            End If
            Console.ForegroundColor = ConsoleColor.Yellow
            Console.WriteLine("What Is The Eastern Most Capital In The United States ?")
            two4 = Console.ReadLine.ToLower
            If two4 = "augusta" Then
                Console.ForegroundColor = ConsoleColor.Green
                Console.WriteLine("Correct!")
                score16 = score15 + 1
            Else
                Console.ForegroundColor = ConsoleColor.Red
                Console.WriteLine("Incorrect")
                Console.ForegroundColor = ConsoleColor.Magenta
                Console.WriteLine("The Correct Answer Was : Augusta")
                score16 = score15 + 0
            End If
            Console.ForegroundColor = ConsoleColor.Yellow
            Console.WriteLine("What Is The Full Name Of The Person Who Painted The Famous Painting Named The Mona Lisa")
            two5 = Console.ReadLine.ToLower
            If two5 = "leonardo da vinci" Then
                Console.ForegroundColor = ConsoleColor.Green
                Console.WriteLine("Correct!")
                score17 = score16 + 1
            Else
                Console.ForegroundColor = ConsoleColor.Red
                Console.WriteLine("Incorrect")
                Console.ForegroundColor = ConsoleColor.Magenta
                Console.WriteLine("The Correct Answer Was : Leonardo Da Vinci")
                score17 = score16 + 0
            End If
            Console.ForegroundColor = ConsoleColor.Yellow
            Console.WriteLine("How Many Squares Are There On A Standard Chessboard ?")
            two6 = Console.ReadLine.ToLower
            If two6 = "64" Then
                Console.ForegroundColor = ConsoleColor.Green
                Console.WriteLine("Correct!")
                score18 = score17 + 1
            Else
                Console.ForegroundColor = ConsoleColor.Red
                Console.WriteLine("Incorrect")
                Console.ForegroundColor = ConsoleColor.Magenta
                Console.WriteLine("The Correct Answer Was : 64")
                score18 = score17 + 0
            End If
            Console.WriteLine()
            Console.ForegroundColor = ConsoleColor.Yellow
            Console.WriteLine("Your Score Was:")
            Console.ForegroundColor = ConsoleColor.Yellow
            Console.WriteLine("Percentage Form")
            Console.ForegroundColor = ConsoleColor.DarkYellow
            Dim scorel3 As Integer = score18 / 6 * 100
            Console.WriteLine(score18 / 6 * 100)
            If score18 < 65 Then
                Console.ForegroundColor = ConsoleColor.Red
                Console.WriteLine("You Failed Level 3")
                Console.ForegroundColor = ConsoleColor.Gray
                Console.WriteLine("----------------------------------------------")
                Console.ForegroundColor = ConsoleColor.Red
                Console.WriteLine("NOTE : Using Any Type Of Search Engine Or The Internet To Help You Is Cheating !")
                Console.ReadLine()
            End If
            If score18 >= 65 Then
                Console.ForegroundColor = ConsoleColor.Green
                Console.WriteLine("You Passed Level 3")
                Console.ForegroundColor = ConsoleColor.Gray
                Console.WriteLine("----------------------------------------------")
                Console.ForegroundColor = ConsoleColor.Red
                Console.WriteLine("NOTE : Using Any Type Of Search Engine Or The Internet To Help You Is Cheating !")
                Console.ForegroundColor = ConsoleColor.Gray
                Console.WriteLine("----------------------------------------------")
            End If
    I saw a bunch of extraneous "End If" statements when I did that. As suggested step through the code line by line.
    Please remember next time...elections matter!

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Nov 2014
    Posts
    73

    Re: VS 2010 If Else If Issue

    Any other suggestions other than debugging ?

  5. #5
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,471

    Re: VS 2010 If Else If Issue

    Yes..give up trying to program

    or

    You either didn't show us all the code or these are highlighted in IDE. Remove them:

    End If
    End If
    End If
    End If
    Please remember next time...elections matter!

  6. #6
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: VS 2010 If Else If Issue

    Hire a programmer? Programmers debug. It's just the way it is; it's a part of the job.

    By stepping through the code (a tool used to debug) you will find your error, and probably narrow it down to one or two lines.

    In addition, try breaking it down to a sequence of function calls, so you functions aren't massive splurges of random code. You can debug each function independently, ensure it's doing what it is asked to do. By limiting the amount of code in each function it makes debugging simple to almost non-existent.
    "Ok, my response to that is pending a Google search" - Bucky Katt.
    "There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
    "Before you can 'think outside the box' you need to understand where the box is."

  7. #7
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,612

    Re: VS 2010 If Else If Issue

    JasonPaul, I have some symapthy for you because I think some of the responses you've had in this and your other thread have been phrased much more harshly than I would like. However, outbursts like that aren't acceptable. Keep it clean.

    To everyone else, don't forget we were all new once and sometimes we just didn't get it. Sometimes the weight of information coming at us was too much to cope with and we may not have immediately recognised good advice when we saw it. Sometimes it's hard to offer encouragement but it's hard to ever justify offering discouragement.

    JasonPaul, some of the advice that has been given to you may not have been given in the most supportive tone but, none the less, it was good advice. You need to learn to debug if you want to progress at all. In a very real sense Debugging is programming and, unless you get your head round it you'll never be able to figure out the problems in your code. Step back from your project for a moment and actually run through some of the tutorials you were linked to in your other thread. They'll probably only take an hour or two and will improve your ability hugely, and I mean HUGELY. It may seem like a pain but you'll thank us later.
    Last edited by FunkyDexter; Nov 26th, 2014 at 09:54 AM.
    You can depend upon the Americans to do the right thing. But only after they have exhausted every other possibility - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  8. #8
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,471

    Re: VS 2010 If Else If Issue

    OK...I'm sorry. Debugging is just so fundamental to programming that "Any other suggestions other than debugging" just knocked me for a loop. I apologize. If you plug that code into IDE like I did it is obvious, at least it seemed that way to me, the end ifs don't align. I didn't see how the code can run. That was my comment about the end ifs in my last post.
    Please remember next time...elections matter!

  9. #9
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,230

    Re: VS 2010 If Else If Issue

    Hi,

    Give the kid a break guys and do yourself a favour jasonpaul2001 and delete that text in post #6. FunkyDexter beat me to it.

    You have got a lot to learn but give it time and you will get there. You have made a huge mistake by using non-descriptive variable names when doing your calculations and therefore you cannot see the wood for the trees and nor can anyone else for that matter.

    So, if you want to add up a quiz score in a project then use a SINGLE variable only to do the adding of that score and then create a separate PERCENTAGE variable to get the percentage to test against. Therefore those two variables could be called “TotalScore” and “PercentageTotalScore”. Easy to remember and easy to use.

    However, because of your variable names you have missed the obvious! What’s wrong with this:-

    vb.net Code:
    1. Dim scorel3 As Integer = score18 / 6 * 100
    2. Console.WriteLine(score18 / 6 * 100)
    3. If score18 < 65 Then
    4.   'etc

    Hope that helps.

    Cheers,

    Ian

  10. #10
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,471

    Re: VS 2010 If Else If Issue

    Here is a nice debugging tutorial:

    https://www.youtube.com/watch?v=Uk-a7qVa36c
    Please remember next time...elections matter!

  11. #11
    Hyperactive Member
    Join Date
    Mar 2012
    Posts
    311

    Re: VS 2010 If Else If Issue

    Quote Originally Posted by IanRyder View Post
    Hi,

    Give the kid a break guys and do yourself a favour jasonpaul2001 and delete that text in post #6. FunkyDexter beat me to it.

    You have got a lot to learn but give it time and you will get there. You have made a huge mistake by using non-descriptive variable names when doing your calculations and therefore you cannot see the wood for the trees and nor can anyone else for that matter.

    So, if you want to add up a quiz score in a project then use a SINGLE variable only to do the adding of that score and then create a separate PERCENTAGE variable to get the percentage to test against. Therefore those two variables could be called “TotalScore” and “PercentageTotalScore”. Easy to remember and easy to use.

    However, because of your variable names you have missed the obvious! What’s wrong with this:-

    vb.net Code:
    1. Dim scorel3 As Integer = score18 / 6 * 100
    2. Console.WriteLine(score18 / 6 * 100)
    3. If score18 < 65 Then
    4.   'etc

    Hope that helps.

    Cheers,

    Ian
    I agree with everything that IanRyder said. I'm actually surprised that this compiled at all since while noticing the error in the snippet IR provided, I also noticed that score13 was declared here yet the variable was used in the first If-block. If this code did compile, then I'm guessing that the score13 in the If-block was declared in a more general scope (eg at the class level instead of in a method). This means that you now have 2 different variables that both use the same name and could lead to confusion in the future; thus another reason to just use one variable for the score...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width