Thread: Couple questions

  1. #1
    Registered User
    Join Date
    Nov 2010

    Couple questions

    For one of my classes, we have to write a C program that will add two 10,000 digit numbers.

    The first one is, how do I add strings? I can't just do str1+str2 can I?

    And second, to bring in strings I was going to just use file and open it in the command line, but I forgot how, so if anyone could possibly inform me that would be great.

  2. #2
    Registered User
    Join Date
    Jun 2009
    Heh, you are going to have to try something and post your attempt before anyone helps in any detail, but to add strings of digits, just line them up like you did in grade 3 and add the digits in the positions together, taking care to "carry the one" where necessary.

  3. #3
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    to bring in strings I was going to just use file and open it in the command line
    I'm going to take this as meaning you want to specify an input file, in which case piping the input would be enough. In the command line, on Windows or Linux:
    command < input.txt
    This tells the OS to send input.txt as standard input (such that you read from the file wherever scanf or std::cin is used)
    Consider this post signed

  4. #4
    Join Date
    Oct 2007
    Inside my computer
    You have to remember that adding strings and summing numbers stored in string format are not the same definitions. To add strings, you would use strcat, but that wouldn't work if you have two strings which are numbers you want to add.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Sep 2006
    If you had a file named bigNums.txt, with two rows of long numbers you needed to add up, you could get the input by re-directing stdin:

    fget(buff1, sizeof(buff1), stdin); //puts the first row into the buff1 char array.
    buff1 needs to be at least 1 char larger than the number of digits in the first row of digits, in the file. That allows fgets() to add in the end of string char: '\0'.

    Repeat that for buff2, and make a third char array for your carries, and you'll need yet another large char array, for the answer. With that, you should be off to the races.

    0000000000000 <---the carry array, set by you, to all zeroes
    1234567891911 <---number on the first row, goes into buff1
    7733558822004 <--number on the second row, into buff2
    <---answer - another large buffer array of type char.

    Question is, did you mean mathematical addition of these long strings of digits, or did you mean adding one string, onto the end of the other string (concatenation)?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. couple of questions concerning multi core cpu's
    By Masterx in forum Tech Board
    Replies: 6
    Last Post: 10-07-2009, 10:39 AM
  2. A Couple of Questions
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 12-03-2005, 06:47 PM
  3. Couple of simple directdraw questions.
    By Deo in forum Game Programming
    Replies: 3
    Last Post: 05-25-2005, 07:55 AM
  4. Studying for Final, Couple Questions...
    By stewade in forum C Programming
    Replies: 4
    Last Post: 05-10-2004, 05:02 PM
  5. A couple of OOP questions
    By codec in forum C++ Programming
    Replies: 5
    Last Post: 05-03-2004, 07:18 PM