Like Tree1Likes

string question

This is a discussion on string question within the C Programming forums, part of the General Programming Boards category; im trying to add a number like 1234567891112131415 and 1234567891234567891234556. i stored them both as string1 and string2 but i ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    5

    Angry string question

    im trying to add a number like 1234567891112131415 and 1234567891234567891234556. i stored them both as string1 and string2 but i cant figure out how to get the answer.i think i need to use strtol, but i cant figure out what am i doing wrong. any help?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <math.h>
    
    int main() {
         // variables
         char string1[512];
         char string2[512];
        
         //opening
         printf("Enter a whole number:");
         scanf_s("%s", string1);
         printf("Enter another whole number:");
         scanf_s("%s", string2);
         //adding trying to use strtol function
         long strtol(char *string1, char** string2);
    
        
    
         //results
         printf("The sum is:%s",  );
        
         //exiting
         getchar();
         getchar();
         return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How do you normally add long numbers on paper?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    5
    giggle giggle, i guess you dont know the answer either.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,313
    Your guess is wrong. So, how do you normally add long numbers on paper?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    5
    by adding, but what im adding is too big for regular int, float, long long int, i need to add it as string but cant figure out how to.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,313
    Quote Originally Posted by rupert
    by adding
    Okay, maybe you have good mental calculation skills. Now, recall how most of your classmates back in grade/primary school would do such a calculation on paper, say, adding 8263127 with 5273609. Replicate that method in your program.

    Quote Originally Posted by rupert
    but what im adding is too big for regular int, float, long long int
    I'm not a neuroscientist, but I'm fairly certain that your brain does not work using those integer types, and neither did you bother with them on paper, back when you were taught how to perform addition.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Add just like you would do when you calculate manually.
    Write down the steps when you add.
    eg.
    Code:
    32324
       343

  8. #8
    Registered User
    Join Date
    May 2011
    Posts
    5
    ok, i guess im not making myself clear.read the title befor you post a comment, it didnt say how do i add, im not new new to c programming, im just stuck on a problem and need help. i know how to add small numbers, i made myself clear with i need to add too large numbers ie.1234567890123456789 + 9999999999999999999999999999, and get an answer, but the answer is too big for int, long int, long long int, or float value. so i think i need to use strings? any guesses?

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    In my house
    Posts
    32
    if you want to code how to calculate large numbers you will have to do one of two things:

    1.) Invest some time looking for Binary-coded decimal (BCD) code in C, and how to use it.

    2.) Create functions to calculate the numbers bit by bit.
    For example, "134" + "396" would mean nothing because they are both in strings, so you'd have to make a function to add the strings. (I know its a small number, but this is just an example)

    You'd start with "4" + "6". First you'd need to turn them into ints, calculate it (which would be 10), then turn it into a string which would equal "10". Then continuing with "30" + "90" + "10" which would equal "130". Then finishing it off by adding "100" with "300" and "130" which would equal "530".

    Something like this would require you to know recursion (or if you want to just use loops, its possible) and a small knowledge of the standard library to turn the strings to ints, calculate them, and then back to strings.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by rupert View Post
    giggle giggle, i guess you dont know the answer either.
    Hmmm... I'm guessing they don't teach this in school any more...

    Have you ever done addition on paper... that is, without a calculator?

    Sit down with a pencil and paper and add... 538 and 389 ....

    Code:
     538
    +389
    ====
    Working right to left...
    9+8 is 17 ... write 7, carry the 1
    8+3 is 11 + 1 ... write 2 carry the 1
    5+3 is 8 +1 = 9
    so 538 + 389 = 927

    That is the process you need to code.

    The first half of any solution is understanding the problem...

    (Alternatively ... http://www.youtube.com/watch?v=rLprXHbn19I )
    Last edited by CommonTater; 05-30-2011 at 11:04 PM.

  11. #11
    Registered User
    Join Date
    May 2011
    Posts
    5
    so your saying rite a code that reads up and down rite to left?
    i didnt think you could do that. how would that work. also if i rite a code to do that would i just store it as regualr int or still use string. im confused.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Like I said, how do you add two numbers on paper?
    Code:
    12345   <-- string 1
     6789   <-- string 2
    -----
    ?????   <-- any guess as to what this would be?
    Now, how do you add those? You start with the 1s column, add them, and carry if you need to.

    Now how do you get the number from the character? Subtract the character '0' from it.


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by rupert View Post
    so your saying rite a code that reads up and down rite to left?
    i didnt think you could do that. how would that work. also if i rite a code to do that would i just store it as regualr int or still use string. im confused.
    Of course you can do that... But I'm not going to hand you the answer (and neither should anyone else).

    A sizeable chunk of a programmer's skill is the analytical skill to understand the nuts and bolts of a problem then applying logical thinking to work out a solution... Writing code is the second to last step in the process...

    So break it down into the smallest steps... What has to happen first?

  14. #14
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by rupert View Post
    so your saying rite a code that reads up and down rite to left?
    i didnt think you could do that. how would that work. also if i rite a code to do that would i just store it as regualr int or still use string. im confused.
    You need to learn how to read and write proper English before you can push yourself to learn how to add. If you want to be taken seriously, act like you take yourself seriously not like a bloody joker.
    rags_to_riches likes this.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by claudiu View Post
    You need to learn how to read and write proper English before you can push yourself to learn how to add. If you want to be taken seriously, act like you take yourself seriously not like a bloody joker.
    Assuming that English is his first language?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. string question
    By insanoflex in forum C++ Programming
    Replies: 2
    Last Post: 01-21-2011, 09:50 AM
  2. A question about string
    By winggx in forum C Programming
    Replies: 6
    Last Post: 03-23-2010, 04:16 PM
  3. A question about string
    By zouyu1983 in forum C Programming
    Replies: 2
    Last Post: 11-28-2006, 07:10 PM
  4. string question
    By na_renu in forum C Programming
    Replies: 1
    Last Post: 01-08-2006, 06:43 PM
  5. int to string question
    By Blizzarddog in forum C++ Programming
    Replies: 13
    Last Post: 11-10-2003, 10:23 AM

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