-
Oct 3rd, 2015, 08:23 PM
#1
Thread Starter
Member
Multiple Message Boxes
OK so I trying to write a program where it compares 2 integers the user enters and tells you if one is greater than the other or if one is less than the other or if they are equal. I have most of the code figured out but my issue is the answer. I want a different message box to pop up for different answers but the same message box saying the first number is greater than the other keeps popping up. So how can I make it that a different message box pops up for a different answer?
-
Oct 3rd, 2015, 08:29 PM
#2
Re: Multiple Message Boxes
We need to see your code...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 3rd, 2015, 08:35 PM
#3
Thread Starter
Member
Re: Multiple Message Boxes
Sorry I forgot to post it. Here it is:
Dim int1 As Integer
Dim int2 As Integer
Dim Answer As String
Dim Answer2 As String
Dim Answer3 As String
int1 = InputBox(“What is the first number?”, “NumberA”)
int1 = InputBox(“What is the second number?”, “NumberB”)
If int1 > int2 Then
Answer = MsgBox("Number A is greater than Number B")
End If
If int1 < int2 Then
Answer2 = MsgBox("Number A is less than Number B")
End If
If int1 = int2 Then
Answer3 = MsgBox("Number A equals Number B")
End If
End Sub
-
Oct 3rd, 2015, 08:36 PM
#4
Re: Multiple Message Boxes
here's a simple example:
Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim a, b As Integer
Dim msg As String
If Integer.TryParse(TextBox1.Text, a) AndAlso Integer.TryParse(TextBox2.Text, b) Then
If a > b Then
msg = String.Format("{0} is greater than {1}", a, b)
ElseIf b > a Then
msg = String.Format("{0} is greater than {1}", b, a)
Else
msg = String.Format("{0} is equal to {1}", a, b)
End If
MessageBox.Show(msg)
End If
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 3rd, 2015, 08:40 PM
#5
Re: Multiple Message Boxes
Code:
int1 = InputBox(“What is the first number?”, “NumberA”)
Code:
int1 = InputBox(“What is the second number?”, “NumberB”)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 3rd, 2015, 08:47 PM
#6
Thread Starter
Member
Re: Multiple Message Boxes
I did that code but the message boxes aren't popping up
-
Oct 3rd, 2015, 08:50 PM
#7
Re: Multiple Message Boxes
both inputboxes set int1
the other way I showed you is a safer method. try typing 'hello' in your inputbox
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 3rd, 2015, 08:55 PM
#8
Thread Starter
Member
Re: Multiple Message Boxes
I did set both inputboxes int1 and it still doesn't work. Also typing hello breaks it.
-
Oct 3rd, 2015, 09:00 PM
#9
Re: Multiple Message Boxes
Code:
Dim int1 As Integer
Dim int2 As Integer
Dim Answer As String
Dim Answer2 As String
Dim Answer3 As String
int1 = InputBox(“What is the first number?”, “NumberA”)
int2 = InputBox(“What is the second number?”, “NumberB”)
If int1 > int2 Then
Answer = MsgBox("Number A is greater than Number B")
End If
If int1 < int2 Then
Answer2 = MsgBox("Number A is less than Number B")
End If
If int1 = int2 Then
Answer3 = MsgBox("Number A equals Number B")
End If
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 3rd, 2015, 09:01 PM
#10
Re: Multiple Message Boxes
What I did there, see it, you don't
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 3rd, 2015, 09:07 PM
#11
Thread Starter
Member
Re: Multiple Message Boxes
Thanks so much I knew I probably made one little error which messed the whole thing up. Another question I am also trying to write a program that tells someone their mark on the test and then if they passed or failed. So how can I make a message box pop up and then when it closes another pop up? Here is my code:
Dim mark As Double
Dim total As Integer
Dim percent As Double
Dim msg As String
mark = InputBox("What was your mark on the test?", "Mark")
total = InputBox("How many marks where on the test?", "Total marks")
percent = mark / total * 100
percent = MsgBox("You percent on the test is, " & percent)
If percent >= 50 Then
msg.TrimStart("You passed the test")
Else
msg.TrimStart("You failed the test")
End If
End Sub
End Class
-
Oct 3rd, 2015, 09:13 PM
#12
Re: Multiple Message Boxes
TrimStart is used to remove leading spaces from a string...
Code:
If percent >= 50 Then
msg = "You passed the test"
Else
msg = "You failed the test"
End If
MsgBox(msg)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 3rd, 2015, 09:15 PM
#13
Re: Multiple Message Boxes
and it wouldn't be:
Code:
percent = MsgBox("You percent on the test is, " & percent)
it'd be:
Code:
MsgBox("You percent on the test is, " & percent)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 3rd, 2015, 09:19 PM
#14
Thread Starter
Member
Re: Multiple Message Boxes
Thanks and I have one last quick question. I want to add a nesting structure so that if the user enters an invalid mark, they get an error message, otherwise the program continues like normal. How would I go about doing that? I am not sure what nesting code looks like because I am a beginner at coding (which I why I have all these issues). Also sorry if I am troubling you / annoying you with all of this.
-
Oct 3rd, 2015, 09:34 PM
#15
Thread Starter
Member
Re: Multiple Message Boxes
Actually I figured it out but ran into a issue. The message pops up saying that the mark is invalid but then the messages pops up saying the mark and that I passed. So how can I get the only the one message to pops up? Here is the code:
If mark > total Then
MsgBox(“Invalid marks. Start again.”)
End If
percent = mark / total * 100
MsgBox("You percent on the test is, " & percent)
If percent >= 50 Then
msg = "You passed the test"
Else
msg = "You failed the test"
End If
MsgBox(msg)
-
Oct 3rd, 2015, 09:37 PM
#16
Re: Multiple Message Boxes
Code:
If mark > total Then
MsgBox(“Invalid marks. Start again.”)
Return
End If
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 3rd, 2015, 09:40 PM
#17
Thread Starter
Member
Re: Multiple Message Boxes
Thanks and sorry if I annoyed you with all of this. I am new to coding so I am getting used to it.
-
Oct 3rd, 2015, 09:50 PM
#18
Re: Multiple Message Boxes
One thing .Paul showed in his first example, then moved away from is using .TryParse to properly convert a string to a number. InputBox returns ONLY strings. By assigning those to variables that are numeric types you are requiring the compiler to perform an implicit conversion from a string to a number. As long as the string is a number, that works. It isn't as efficient as converting it correctly, but it works. However, when you type in a string that is NOT a number...it crashes. The .TryParse methods (Integer, Double, and so forth all have them) perform the conversions safely and return False if the conversion is not possible. You really should be using them to convert ALL user input.
My usual boring signature: Nothing
-
Oct 3rd, 2015, 10:09 PM
#19
Re: Multiple Message Boxes
As Shaggy told you, your code is not ideal...
I showed you a more professional way to write it.
I'm not angry about the questions.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 4th, 2015, 06:47 AM
#20
Lively Member
Re: Multiple Message Boxes
why there's no such operator like the "?" in C++?
i would've done it easier in C++
Code:
if (num1 == num2)
std::cout << "equal." << std::endl;
else
std::cout << (num1 < num2 ? "first number is smaller" : "first number is greater");
-
Oct 4th, 2015, 09:44 AM
#21
Re: Multiple Message Boxes
Originally Posted by chipp
why there's no such operator like the "?" in C++?
i would've done it easier in C++
Code:
if (num1 == num2)
std::cout << "equal." << std::endl;
else
std::cout << (num1 < num2 ? "first number is smaller" : "first number is greater");
There is, in the form of the If() operator.
Code:
MessageBox.Show(If(percent >= 50, "You passed.", "You failed."))
It doesn't get advertised or used a lot. I've never liked it, syntactically, and also avoid the tertiary operator ?: in C-like languages for similar reasons. Sometimes they make code more pretty. Other times it's just more symbols and cognitive load. For example, I'd say an "easier" version of your code would be:
Code:
char* output;
if (num1 == num2) {
output = "equal.";
else {
output = (num1 < num2) ?
"first number is smaller." :
"first number is greater";
}
std::cout << output << endl;
It's a matter of taste, but "fewer lines of code" is often not the same as "easier to read or maintain". Of course, if we want to play "which language is better" games, VB can hook you up with a Select..Case syntax that's underappreciated and getting explored in languages like Swift and F#. I think it goes something like this:
Code:
Dim output As String = ""
Select Case num1
Case Is = num2
output = "equal"
Case Is < num2
output = "less"
Case Is > num2
output = "greater"
End Select
Console.WriteLine(output)
I think it's pretty neat.
This answer is wrong. You should be using TableAdapter and Dictionaries instead.
-
Oct 8th, 2015, 08:58 AM
#22
Lively Member
Re: Multiple Message Boxes
i just know that select...casecan used that way...
learned one more new thing
thx, bro
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|