converting string to ints

This is a discussion on converting string to ints within the C# Programming forums, part of the General Programming Boards category; I am working on an assignment to create a Complex number calculator. In this assignment I am to ask the ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    22

    converting string to ints

    I am working on an assignment to create a Complex number calculator. In this assignment I am to ask the user for input to the calculator. We are given a sample run output that looks like this.
    Enter operand1: 3 4
    operand1: (3, 4)
    Enter operation : +
    Enter operand2: 1 2
    (3, 4) + (1, 2) = (4, 6)
    My question is how would I take from the user: an integer followed by a space followed by another integer and convert that into two seperate accessible int values that I can save as real and imaginary values. Thank you for your time.

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    The standard solution to your problem is scanf(). The first argument is a pattern of your input, other arguments are variables where you store the data itself.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    22
    I was looking for a solution in c# programming. Thank you though.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    Oh right, my bad, I use the "what's new" link and this time I didn't pay attention to the thread category.

    While waiting for the C# experts (I'm not), I checked online and it looks like you will have to use String.Split() to take care of the space and Int.Parse() method to get your integers. That's not very attractive, but if your input syntax is fixed, it should be fine.
    Last edited by root4; 09-18-2012 at 07:16 AM.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    41
    Yep, use split to get two strings, then use Int32.TryParse (it's better than Int32.Parse as it will gracefully handle when users type in garbage rather than numbers).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. converting ints to strings?
    By rodrigorules in forum C++ Programming
    Replies: 2
    Last Post: 11-25-2009, 03:49 AM
  2. converting ints to chars
    By e66n06 in forum C Programming
    Replies: 4
    Last Post: 07-28-2007, 03:52 PM
  3. Converting Chars to Ints
    By sycorax in forum C++ Programming
    Replies: 2
    Last Post: 09-06-2005, 10:40 PM
  4. Converting strings to ints
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 04-25-2002, 02:48 PM
  5. converting ints to arrarys
    By deleeuw in forum C++ Programming
    Replies: 6
    Last Post: 11-02-2001, 01:31 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21