# A simple calculator

Page 1 of 1

## 14 Replies - 1811 Views - Last Post: 01 November 2017 - 05:18 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=407277&amp;s=0462b6b0d5f3ab535767c620b9e54ff8&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

Reputation: 0
• Posts: 71
• Joined: 04-November 16

# A simple calculator

Posted 30 October 2017 - 10:03 AM

Hello everyone, I am studying visual basic and I am trying to make a calculator with console edition. Here is my code
Module Module1

Sub Main()
Dim num1 As Integer
Dim num2 As Integer
Dim solution As Integer
Dim oprtr As String

Console.Write("Enter an operator ^, +, -, *, /, mod(m): ")

If oprtr = "+" Then
Console.Write("Enter first number: ")
Console.Write("Enter second number: ")

solution = num1 + num2
Console.WriteLine(solution)

ElseIf oprtr = "-" Then
Console.Write("Enter first number: ")
Console.Write("Enter second number: ")

solution = num1 - num2
Console.WriteLine(solution)

ElseIf oprtr = "*" Then
Console.Write("Enter first number: ")
Console.Write("Enter second number: ")

solution = num1 * num2
Console.WriteLine(solution)

ElseIf oprtr = "/" Then
Console.Write("Enter first number: ")
Console.Write("Enter second number: ")

solution = num1 / num2
Console.WriteLine(solution)

ElseIf oprtr = "^" Then
Console.Write("Enter first number: ")
Console.Write("Enter second number: ")

solution = num1 ^ num2
Console.WriteLine(solution)

ElseIf oprtr = "m" Then
Console.Write("Enter first number: ")
Console.Write("Enter second number: ")

solution = num1 Mod num2
Console.WriteLine(solution)

Else
Console.Write("Enter one of them: ^, +, -, *, /, mod(m): ")

End If

End Sub

End Module

in here

Else
Console.Write("Enter one of them: ^, +, -, *, /, mod(m): ")

I wanted to do If the given text is exit, it should quit. Otherwise program should keep on asking more operators and numbers. The following operators should be implemented. Any help? Should I use switch case?

Is This A Good Question/Topic? 0

## Replies To: A simple calculator

### #2 modi123_1

• Suitor #2

Reputation: 13776
• Posts: 54,972
• Joined: 12-June 08

## Re: A simple calculator

Posted 30 October 2017 - 10:11 AM

I would think you would want to use a while loop that4 has a condition checking on user input.

Reputation: 0
• Posts: 71
• Joined: 04-November 16

## Re: A simple calculator

Posted 30 October 2017 - 10:14 AM

modi123_1
Do while oprtr = ^, +, -, *, /, mod(m)
if statements...
something like that ?

This post has been edited by shiftadelete: 30 October 2017 - 10:15 AM

### #4 modi123_1

• Suitor #2

Reputation: 13776
• Posts: 54,972
• Joined: 12-June 08

## Re: A simple calculator

Posted 30 October 2017 - 10:19 AM

Why not something simple "While not the exit character.. do the loop".

Reputation: 0
• Posts: 71
• Joined: 04-November 16

## Re: A simple calculator

Posted 30 October 2017 - 10:25 AM

modi123_1 Can you tell more clear(With codes)? I have been studying since 3 days not good at it yet.

### #6 modi123_1

• Suitor #2

Reputation: 13776
• Posts: 54,972
• Joined: 12-June 08

## Re: A simple calculator

Posted 30 October 2017 - 10:42 AM

I am not going to do the work straight up for you, but break the issue down.

How do you make a while loop?
How can you boolean compare a character?
What should be inside the while loop body?

Reputation: 0
• Posts: 71
• Joined: 04-November 16

## Re: A simple calculator

Posted 30 October 2017 - 12:59 PM

modi123_1 inside loop body my codes will be there. But what will be the condition when create a whilee loop?
here is the question:
Write a Visual Basic program that will help with calculation. This program should read an operator as text (such as +) ask two numbers and perform the operation. If the given operator is not valid, it should list the possible operators. If the given text is exit, it should quit. Otherwise program should keep on asking more operators and numbers. The following operators should be implemented:

^, +, -, *, /, mod

It really wants us to use while loop or something?

### #8 modi123_1

• Suitor #2

Reputation: 13776
• Posts: 54,972
• Joined: 12-June 08

## Re: A simple calculator

Posted 30 October 2017 - 01:21 PM

Perhaps you should hang around more before you utilize the rep system.

Again - refresh yourself on the rules and asking people to do your work for you.

You haven't answered my three questions above which, when put together, would be your solution.

Reputation: 0
• Posts: 71
• Joined: 04-November 16

## Re: A simple calculator

Posted 30 October 2017 - 01:40 PM

modi123_1 hahah I clicked rep button wrongly and couldn't get it back. You're saying that you are not gonna do my homework but instead of criticize me you could help more.

1)we do while loop in visual basic like this:
do while
...
loop

2)we didn't learn it yet, so I don't have to that information in the codes.

3)My first code(which is I wrote the topic will come inside of loop.

You see, these answer is not enough to solve the question. First of all my English is quite weak so I am in trouble the understand the question. As I understand the question wants...

1)Firstly the program should ask user to enter a operator
2)İf the operator is valid program should ask the numbers.
3)İf the operator isn't valid program should list operators.
4)When the user enter valid operator again program should ask the numbers.

AM I RIGHT?

### #10 RamonRobben

Reputation: 82
• Posts: 545
• Joined: 19-May 14

## Re: A simple calculator

Posted 30 October 2017 - 02:18 PM

A quick google search gave me a few results. Make sure to study them and I think you will find the problem you made:

https://codereview.s...mple-calculator

Reputation: 0
• Posts: 71
• Joined: 04-November 16

## Re: A simple calculator

Posted 30 October 2017 - 02:25 PM

Module Module1

Sub Main()
Dim num1 As Integer
Dim num2 As Integer
Dim solution As Integer
Dim oprtr As Integer

Console.WriteLine("'2' is for subtraction")
Console.WriteLine("'3' is for multiplication")
Console.WriteLine("'4' is for division")
Console.WriteLine("'5' is for modulus")
Console.WriteLine("'6' is for exponential")
Console.WriteLine("'7' is for bitwise or ")
Console.WriteLine("'8' is for bitwise and")

Console.Write("Choose the operator: ")

Select Case oprtr
Case 1
Console.Write("Enter first number: ")
Console.Write("Enter second number: ")
solution = num1 + num2
Console.Write("solution is=")
Console.WriteLine(solution)

Case 2
Console.Write("Enter first number: ")
Console.Write("Enter second number: ")
solution = num1 - num2
Console.Write("solution is=")
Console.WriteLine(solution)

Case 3

Console.Write("Enter first number: ")
Console.Write("Enter second number: ")
solution = num1 * num2
Console.Write("solution is=")
Console.WriteLine(solution)
Case 4

Console.Write("Enter first number: ")
Console.Write("Enter second number: ")
solution = num1 / num2
Console.Write("solution is=")
Console.WriteLine(solution)
Case 5
Console.Write("Enter first number: ")
Console.Write("Enter second number: ")
solution = num1 Mod num2
Console.Write("solution is=")
Console.WriteLine(solution)

Case 6
Console.Write("Enter first number: ")
Console.Write("Enter second number: ")
solution = num1 ^ num2
Console.Write("solution is=")
Console.WriteLine(solution)

Case 7
Console.Write("Enter first number: ")
Console.Write("Enter second number: ")
solution = num1 Or num2
Console.Write("solution is=")
Console.WriteLine(solution)

Case 8
Console.Write("Enter first number: ")
Console.Write("Enter second number: ")
solution = num1 And num2
Console.Write("solution is=")
Console.WriteLine(solution)

Case Else
Console.WriteLine("Enter valid operator: ")
Console.WriteLine()
Console.WriteLine("'2' is for subtraction")
Console.WriteLine("'3' is for multiplication")
Console.WriteLine("'4' is for division")
Console.WriteLine("'5' is for modulus")
Console.WriteLine("'6' is for exponential")
Console.WriteLine("'7' is for bitwise or ")
Console.WriteLine("'8' is for bitwise and")
End Select

End Sub

End Module

I am gonna cry how I am going to make this loop

### #12 IronRazer

• Custom Control Freak

Reputation: 1470
• Posts: 3,725
• Joined: 01-February 13

## Re: A simple calculator

Posted 30 October 2017 - 02:39 PM

When you don't understand how to use a Statement, Class, Method, or whatnot, the first thing to do is search the msdn documents for it. They usually give you an example of how it is used. When searching, a good method is to use the name of the Statement, Class, Method, or whatnot and add "vb.net" to it such as "Using a While loop Vb.net". That will usually give you the msdn links in the top few search results. It usually has other results for other people that have asked the same question which usually have answers too. For example...

Google Search Results For - "using a while loop vb.net"

The first result at the top of the page is the msdn document which has examples of using a While loop (link below).
While...End While Statement (Visual Basic)

The second link is a tutorial on using a While loop (link below).
Dot Net Perls - While-loops

Google can be one of your best friends if you use it wisely. 8)

Reputation: 0
• Posts: 71
• Joined: 04-November 16

## Re: A simple calculator

Posted 30 October 2017 - 02:42 PM

I am just having trouble that what should be the condition is...
do while condition
...
loop

### #14 IronRazer

• Custom Control Freak

Reputation: 1470
• Posts: 3,725
• Joined: 01-February 13

## Re: A simple calculator

Posted 30 October 2017 - 03:11 PM

Well, you are going to want to have a way to exit the loop and end the program, right. This is what modi123_1 was hinting at. Instead of testing the users input for all the functions to keep looping, just keep looping until the user enters something like... Oh say the word End.

For example...
Sub Main()

Console.WriteLine("end - Will exit the program.")

Console.WriteLine("Enter the function number or (end) to quit.")

Dim UserInput As String = ""
UserInput = Console.ReadLine() 'get the users first input

While UserInput <> "end" 'keep looping until the UserInput is equal to the word 'end'

Console.WriteLine("Doing the Select Case and showing the answer....")

Console.WriteLine("Enter the function number or (end) to quit.")
UserInput = Console.ReadLine() 'get the users input again at the end of the loop
End While

End Sub

Also be aware that there is also a Do...Loop you could use too.

Reputation: 0
• Posts: 71
• Joined: 04-November 16

## Re: A simple calculator

Posted 01 November 2017 - 05:18 AM

IronRazer
thank you so much! I did it. I have to improve my problem solving skill. It was hell easy.

Module Module1

Sub Main()
Dim num1 As Integer
Dim num2 As Integer
Dim solution As Integer
Dim oprtr As String = ""

Console.WriteLine("If you want to exit the program, type end.")
While oprtr <> "end"
Console.WriteLine("Enter the operator or (end) to quit.")
Select Case oprtr
Case "+"
Console.Write("Enter first number: ")
Console.Write("Enter second number: ")
solution = num1 + num2
Console.Write("solution is=")
Console.WriteLine(solution)
Case "-"
Console.Write("Enter first number: ")
Console.Write("Enter second number: ")
solution = num1 - num2
Console.Write("solution is=")
Console.WriteLine(solution)
Case "*"
Console.Write("Enter first number: ")
Console.Write("Enter second number: ")
solution = num1 * num2
Console.Write("solution is=")
Console.WriteLine(solution)
Case "/"
Console.Write("Enter first number: ")
Console.Write("Enter second number: ")
solution = num1 / num2
Console.Write("solution is=")
Console.WriteLine(solution)
Case "mod"
Console.Write("Enter first number: ")
Console.Write("Enter second number: ")
solution = num1 Mod num2
Console.Write("solution is=")
Console.WriteLine(solution)
Case "^"
Console.Write("Enter first number: ")
Console.Write("Enter second number: ")
solution = num1 ^ num2
Console.Write("solution is=")
Console.WriteLine(solution)
Case "or"
Console.Write("Enter first number: ")
Console.Write("Enter second number: ")
solution = num1 Or num2
Console.Write("solution is=")
Console.WriteLine(solution)
Case "and"
Console.Write("Enter first number: ")
Console.Write("Enter second number: ")
solution = num1 And num2
Console.Write("solution is=")
Console.WriteLine(solution)
Case Else
Console.WriteLine("Enter valid operator:^, +, -, *, /, mod,bitwise and, bitwise or ")
Console.WriteLine()

End Select

End While

End Sub

End Module

This post has been edited by shiftadelete: 01 November 2017 - 05:21 AM