Page 1 of 1

## 10 Replies - 630 Views - Last Post: 03 January 2013 - 08:16 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=305439&amp;s=42c4bac24ce360d07ee78618c1e5ffc1&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Untalented

Reputation: 1
• Posts: 143
• Joined: 31-January 12

Posted 03 January 2013 - 05:17 AM

```Module Simple_calc

Sub Main()
Dim num1, num2, sum As Integer

Console.Write("Enter First Number: ")

Console.Write("Enter Second Number: ")

sum = num1 + num2

Console.Write("The sum is " & sum)
'Console.Write("The sum of "& num1 "and" & num2 "is" & sum)

End Sub

End Module

```

How can i make it like this " The sum of x and x is x "

It is like this in c++
```cout<<"The sum of"<<num1<<"and"<<num2<<"is"<<sum;
```

Thanks

Is This A Good Question/Topic? 0

## Replies To: Simple Addition (Console)

### #2 DarenR

• D.I.C Lover

Reputation: 516
• Posts: 3,458
• Joined: 12-January 10

Posted 03 January 2013 - 06:02 AM

you need to convert your input to integers 1st-- yes you declare an integer but you dont actually convert them to integers

like

```num1 = integer.parse(Console.ReadLine() )
```

### #3 Untalented

Reputation: 1
• Posts: 143
• Joined: 31-January 12

Posted 03 January 2013 - 06:28 AM

DarenR, on 03 January 2013 - 06:02 AM, said:

you need to convert your input to integers 1st-- yes you declare an integer but you dont actually convert them to integers

like

```num1 = integer.parse(Console.ReadLine() )
```

This is my new code
```Module Simple_calc

Sub Main()
Dim num1, num2, sum As Integer

Console.Write("Enter First Number: ")

Console.Write("Enter Second Number: ")

sum = num1 + num2

'Console.Write("The sum is " & sum)
Console.Write("The sum of " &num1 "and" &num2 "is" & sum)

End Sub

End Module
```

How to write line 17 properly? It has an error "Comma,')' or a valid expression continuation expected"

I tried this
``` Console.Write("The sum of " & num1, "and" & num2, "is" & sum)
```

But the output is

Quote

Enter first number: 10
Enter second number: 10
The sum of 10

How can i make it something like this

Quote

Enter first number: 10
Enter second number: 10
The sum of 10 and 10 is 20

Thanks

This post has been edited by Untalented: 03 January 2013 - 06:31 AM

### #4 DarenR

• D.I.C Lover

Reputation: 516
• Posts: 3,458
• Joined: 12-January 10

Posted 03 January 2013 - 06:41 AM

```Console.Write("The sum of " &num1 "and" &num2 "is" & sum)
```

try something like

``` Console.Write("The sum of " num1 + "and" + num2 +"is" + sum)
```

### #5 Untalented

Reputation: 1
• Posts: 143
• Joined: 31-January 12

Posted 03 January 2013 - 06:46 AM

DarenR, on 03 January 2013 - 06:41 AM, said:

```Console.Write("The sum of " &num1 "and" &num2 "is" & sum)
```

try something like

``` Console.Write("The sum of " num1 + "and" + num2 +"is" + sum)
```

It has the same error like this
```Console.Write("The sum of  " & num1 "and" & num2 "is"& sum)
```

Quote

Error 1 Comma, ')', or a valid expression continuation expected.

### #6 DarenR

• D.I.C Lover

Reputation: 516
• Posts: 3,458
• Joined: 12-January 10

Posted 03 January 2013 - 07:40 AM

```    Console.Write("The sum of " & num1 + "and" & num2 + "is" & sum)
```

this is tested and working

### #7 Untalented

Reputation: 1
• Posts: 143
• Joined: 31-January 12

Posted 03 January 2013 - 07:58 AM

DarenR, on 03 January 2013 - 07:40 AM, said:

```    Console.Write("The sum of " & num1 + "and" & num2 + "is" & sum)
```

this is tested and working

Still not working

```Module Simple_calc

Sub Main()
Dim num1, num2, sum As Integer

Console.Write("Enter First Number: ")

'

Console.Write("Enter Second Number: ")

sum = num1 + num2

'Console.Write("The sum is " & sum)
'Console.Write("The sum of  " & num1 "and" & num2 "is"& sum)
'Console.Write("The sum of " num1 + "and" + num2 +"is" + sum)
Console.Write("The sum of " & num1 + "and" & num2 + "is" & sum)

End Sub

End Module
```

Errors

Quote

Conversion from string "and" to type 'Double' is not valid.

Quote

A first chance exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll

### #8 andrewsw

• I'm not here to twist your niblets

Reputation: 4707
• Posts: 17,467
• Joined: 12-December 12

Posted 03 January 2013 - 08:03 AM

The concatenation operator in VB.Net is &. You also need to ensure that you put spaces either side of the &, because &something has a different meaning.

Console.Write("The sum of " & num1 & " and " & num2 & " is " & sum)

### #9 Untalented

Reputation: 1
• Posts: 143
• Joined: 31-January 12

Posted 03 January 2013 - 08:10 AM

andrewsw, on 03 January 2013 - 08:03 AM, said:

The concatenation operator in VB.Net is &. You also need to ensure that you put spaces either side of the &, because &something has a different meaning.

Console.Write("The sum of " & num1 & " and " & num2 & " is " & sum)

Thank you very much! I have this on my note
```Console.Write("The sum of "&num1& " and " &num2& " is " &sum)
```
but when i tried it at home it's not working, i thought the code was wrong so i tried looking for alternatives. i missed the spaces.

Thank you again.

This post has been edited by Untalented: 03 January 2013 - 08:14 AM

### #10 smohd

• Critical Section

Reputation: 1820
• Posts: 4,627
• Joined: 14-March 10

Posted 03 January 2013 - 08:13 AM

And always dont try to concatenate strings using + sign, it works fine if both operand are strings, but blames out if one of them is a numeric value. This is because + has another meaning in numeric manipulation, so it will always try to convert your string to a number.

### #11 andrewsw

• I'm not here to twist your niblets

Reputation: 4707
• Posts: 17,467
• Joined: 12-December 12

Posted 03 January 2013 - 08:16 AM

No worries.

If we mistakenly place the ampersand next to a value, e.g. &somevalue, then the & is treated as a type-qualifier symbol. This is a hangover from VB6.

Actually.. it's really on the other side:

somevalue& 'Long
somother% 'Integer

& on the left is for a hex-value.

This post has been edited by andrewsw: 03 January 2013 - 08:57 AM