10 Replies - 497 Views - Last Post: 03 January 2013 - 08:16 AM Rate Topic: -----

#1 Untalented  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 134
  • Joined: 31-January 12

Simple Addition (Console)

Posted 03 January 2013 - 05:17 AM

Module Simple_calc

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

        Console.Write("Enter First Number: ")
        num1 = Console.ReadLine()

        Console.Write("Enter Second Number: ")
        num2 = Console.ReadLine()

        sum = num1 + num2

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

        Console.ReadLine()

    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  Icon User is online

  • D.I.C Lover

Reputation: 431
  • View blog
  • Posts: 2,990
  • Joined: 12-January 10

Re: Simple Addition (Console)

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() )

Was This Post Helpful? 1
  • +
  • -

#3 Untalented  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 134
  • Joined: 31-January 12

Re: Simple Addition (Console)

Posted 03 January 2013 - 06:28 AM

View PostDarenR, 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: ")
        num1 = Integer.Parse(Console.ReadLine())


        Console.Write("Enter Second Number: ")
        num2 = Integer.Parse(Console.ReadLine())


        sum = num1 + num2

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

        Console.ReadLine()

    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

Was This Post Helpful? 0
  • +
  • -

#4 DarenR  Icon User is online

  • D.I.C Lover

Reputation: 431
  • View blog
  • Posts: 2,990
  • Joined: 12-January 10

Re: Simple Addition (Console)

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) 

Was This Post Helpful? 0
  • +
  • -

#5 Untalented  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 134
  • Joined: 31-January 12

Re: Simple Addition (Console)

Posted 03 January 2013 - 06:46 AM

View PostDarenR, 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.

Was This Post Helpful? 0
  • +
  • -

#6 DarenR  Icon User is online

  • D.I.C Lover

Reputation: 431
  • View blog
  • Posts: 2,990
  • Joined: 12-January 10

Re: Simple Addition (Console)

Posted 03 January 2013 - 07:40 AM

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


this is tested and working
Was This Post Helpful? 0
  • +
  • -

#7 Untalented  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 134
  • Joined: 31-January 12

Re: Simple Addition (Console)

Posted 03 January 2013 - 07:58 AM

View PostDarenR, 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: ")
        num1 = Integer.Parse(Console.ReadLine())


        '


        Console.Write("Enter Second Number: ")
        num2 = Integer.Parse(Console.ReadLine())


        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)


        Console.ReadLine()

    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

Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3177
  • View blog
  • Posts: 10,639
  • Joined: 12-December 12

Re: Simple Addition (Console)

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)
Was This Post Helpful? 2
  • +
  • -

#9 Untalented  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 134
  • Joined: 31-January 12

Re: Simple Addition (Console)

Posted 03 January 2013 - 08:10 AM

View Postandrewsw, 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

Was This Post Helpful? 0
  • +
  • -

#10 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1817
  • View blog
  • Posts: 4,625
  • Joined: 14-March 10

Re: Simple Addition (Console)

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.
Was This Post Helpful? 1
  • +
  • -

#11 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3177
  • View blog
  • Posts: 10,639
  • Joined: 12-December 12

Re: Simple Addition (Console)

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

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1