Jump to content

Absolute Beginner - Test Application Help!

- - - - -

  • Please log in to reply
10 replies to this topic

#1
aaronoafc

aaronoafc

    Newbie

  • Members
  • Pip
  • 5 posts
Hi,

I'm a programming beginner and currently learning VB.net as a starting language. I have just been making a little test console application using visual studio asking about favourite football teams, please could someone tell me what I'm doing wrong? Below is the code, now I want the user to be able to type in their team "Oldham, Man city etc" Then it to come up with the message I have put there "Good lad, BOOOO" however At the minute it lets the user type in say "oldham" then when we hit enter it just closes the application down with out coming up with the corresponding message.

code:

Dim uservalue As String = Console.ReadLine()

Console.WriteLine("What football team do you support? Oldham, Manchester City, or Manchester United?")
Console.ReadLine()

If uservalue = "oldham" Then
Console.WriteLine("Good lad latics!")
Console.ReadLine()
ElseIf uservalue = "Manchester City" Then
Console.WriteLine("BOOOOOOOOOOOOOOOOO!!!!")
Console.ReadLine()
ElseIf uservalue = "Manchester United" Then
Console.WriteLine("BOOOOOOOOOOOOOOOOO!!!!")
Console.ReadLine()
End If

Thanks
Aaron

#2
lespauled

lespauled

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 232 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash
I would create a variable for output.

Something like:

Dim strOutput as String = string.empty

Then inside the if's you simply assign strOutput to whatever variable.

After the if, put the writeline and readline. So, they are not repeated everywhere.

#3
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,248 posts
  • Programming Language:Java, C#, PHP
  • Learning:C, C++, C#, PHP, Transact-SQL, Assembly, Scheme
Shouldn't

Dim uservalue As String = Console.ReadLine()

Come after
 Console.WriteLine("What football team do you support? Oldham, Manchester City, or Manchester United?") 
so that the user knows what they should input?

Also, it would probably be a much better idea to use case-insensitive comparisions because I might type in Oldham but your code is checking for oldham (lower case) and "Oldham" == "oldham" is false.

Edited by chili5, 23 December 2011 - 09:30 AM.
Fixed typo


#4
lespauled

lespauled

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 232 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash
Also add a provision for an invalid input

#5
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,248 posts
  • Programming Language:Java, C#, PHP
  • Learning:C, C++, C#, PHP, Transact-SQL, Assembly, Scheme
That would be as simple as adding this:

Else
   throw new Exception("Invalid input")
End If


#6
lespauled

lespauled

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 232 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash
or

Else
strOutput = "Invalid input"
End If

#7
aaronoafc

aaronoafc

    Newbie

  • Members
  • Pip
  • 5 posts
Thanks for getting back to me, I've cleaned it up a bit more and moved a couple things around however I don't understand where you put about moving the write lines and read lines near the IF???? Could you provide an example if possible?

furthermore the two parts of code you wrote at the end didn't work, they threw up an error. I simply copied and pasted it into visual studio, just need a part of code at the end so that when someone enters anything in that isn't a suggested answer (such as Chelsea) it could say "invalid" or something along those lines.

#8
lespauled

lespauled

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 232 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash
Sure. In each if statement, you are setting and displaying the values:

If uservalue = "oldham" Then
Console.WriteLine("Good lad latics!")
Console.ReadLine()

ElseIf uservalue = "Manchester City" Then
Console.WriteLine("BOOOOOOOOOOOOOOOOO!!!!")
Console.ReadLine()

ElseIf uservalue = "Manchester United" Then
Console.WriteLine("BOOOOOOOOOOOOOOOOO!!!!")
Console.ReadLine()


Instead, just set the variable inside the if statements, as in

If uservalue = "Manchester United" Then
strOutput = ""BOOOOOOOOOOOOOOOOO!!!!"

etc.

Then after the last END IF,

Console.WriteLine(strOutput)

---------- Post added at 01:32 PM ---------- Previous post was at 01:32 PM ----------

followed by Console.ReadLine()

#9
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,248 posts
  • Programming Language:Java, C#, PHP
  • Learning:C, C++, C#, PHP, Transact-SQL, Assembly, Scheme

aaronoafc said:

Thanks for getting back to me, I've cleaned it up a bit more and moved a couple things around however I don't understand where you put about moving the write lines and read lines near the IF???? Could you provide an example if possible?

furthermore the two parts of code you wrote at the end didn't work, they threw up an error. I simply copied and pasted it into visual studio, just need a part of code at the end so that when someone enters anything in that isn't a suggested answer (such as Chelsea) it could say "invalid" or something along those lines.

For lespauled's code:

Did you add the strOutput variable? If you did that correctly, the code lespauled posted should work.

My code if you put in after the last end it SHOULD throw an error. The whole point of exceptions is if the user inputs invalid data, the program either catches the error and does some kind of corrective action (like maybe asking the user for a new set of input) or if the program does not catch the error the program should terminate. We probably don't want the program to proceed if something bad happens.

Sample code (in C#, as I don't have VB installed in my version of Visual Studio):

string uservalue = string.Empty;
            string output = string.Empty;

            Console.WriteLine("What football team do you support? Oldham, Manchester City, or Manchester United?");
            uservalue = Console.ReadLine().ToLower(); // tolower for case-insensitivity

            if (uservalue == "oldham")
            {
                output = "Good lad latics!";
            } else if (uservalue == "manchester city")
            {
                output = "BOOOOOOOOOOOOOOOOO!!!!";
            } else if (uservalue == "mancester united")
            {
                output = "BOOOOOOOOOOOOOOOOO!!!!";
            } else
            {
                throw new Exception("Invalid input.");
            }

            Console.WriteLine(output);
            Console.ReadLine();

Edited by chili5, 23 December 2011 - 02:52 PM.
Changed "" to string.Empty and added ToLower call for case-insensitivity checks.


#10
aaronoafc

aaronoafc

    Newbie

  • Members
  • Pip
  • 5 posts
Quality guys, thanks for the help, about 99% there now, please see below code, the only problem now is with this code at the minute if the user inputs some incorrect data such as "chelsea"..it comes up invalid input which is correct.

However, then the user can re-type one of the teams suggested (oldham, man u etc) however it doesn't come up with the output "good lad!" or "BOOOOO" it just comes up with the "type exit to exit" code.

Thanks
Aaron

#11
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,248 posts
  • Programming Language:Java, C#, PHP
  • Learning:C, C++, C#, PHP, Transact-SQL, Assembly, Scheme
You need some kind of loop to do that. Make a variable isValid = false

Then
while (!isValid) {

}

Put all your code from above in the loop and in the if statement where the user inputs valid data set isValid to true.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users