Thread: adding numbers that user enters as a single string

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    3

    Unhappy adding numbers that user enters as a single string

    Hi all. I've been busting my head for last two days with this problem. It should be fairly simple but since I'm a begginer it's rather confusing. I need to make a program that asks user to enter a number (let's say 34678) and then to output sum of those individual numbers (in this case that should be 28). I came up with this code that I tought will work but...

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    
    void main()
    {
     char *num1 = "", *nume;
     double lnum = 0, numt;
     int i = 0;
     cout << "Enter number: ";
     cin >> num1;
     while (num1[i])
     {
      char *tt = num1[i];
      lnum = strtod(tt, &nume);
      numt = (numt + lnum);
      i++;
     }
     cout << numt;
    }
    Please help. Cheers.

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    38
    You could try this

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    
    void main()
    {
     string num1; 
     int result=0;
     cout << "Enter number: ";
     cin >> num1;
     for(int i=0;i<num1.length();i++){
       if(num1[i]>='0' && num1[i]<='9'){
         result+=(num1[i]-48);//subtract 48 to convert from ascii value
       }//end if
     }//end for   
     cout << result;

  3. #3
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    To make it more portable I would go for this
    Code:
         result+=(num1[i]-'0');
    '0' is not necessarily 48 but it is shure that the numbers 0..9 appear in sequence in any character encoding.
    Kurt

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    38
    Quote Originally Posted by ZuK
    To make it more portable I would go for this
    Code:
         result+=(num1[i]-'0');
    '0' is not necessarily 48 but it is shure that the numbers 0..9 appear in sequence in any character encoding.
    Kurt
    Good point. Small oversight when writing the code in under 5 minutes. I wasn't looking to make sure it was "perfect" just that it ran. It was something just to give him and idea.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by dnysveen
    Good point. Small oversight when writing the code in under 5 minutes. I wasn't looking to make sure it was "perfect" just that it ran. It was something just to give him and idea.
    I didn't mean to correct you just to add some thought. ( I think it is quite easy to improve a solution. Much harder to come up with one in the first place ).
    Kurt

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > void main()
    Not to mention that main returns an int.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    38
    Quote Originally Posted by Salem
    > void main()
    Not to mention that main returns an int.
    I figured he knew how to end his main function. Geeez people

    Zuk I understand. I always like to have everything optimized and working the best way. I knew really thought of using -'0' but I do understand now. Thanks for the insight.

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    3
    Damn it. I knew I was close. I actualy tought of doing -48 thingie but I also tought that there is implemented function for that and that I could use it instead. Thank you for your quick reply. Cheers

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    38
    Quote Originally Posted by metalinspired
    Damn it. I knew I was close. I actualy tought of doing -48 thingie but I also tought that there is implemented function for that and that I could use it instead. Thank you for your quick reply. Cheers
    If you used a char array you could use the atoi function which converts character numbers to integer values. I think there would be less overhead subtracting 48 or '0' because you are not making a function call, which takes overhead.

  10. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    3
    Quote Originally Posted by dnysveen
    If you used a char array you could use the atoi function which converts character numbers to integer values. I think there would be less overhead subtracting 48 or '0' because you are not making a function call, which takes overhead.
    Good point about function call. How ever atoi (just like strtod or strtol) won't work here. Not sure why but I think it is becouse I'm trying to convert single char to int and they only work with strings. Am I correct? I might be babling total nonsense here but hey nobody was born with knowledge of c++

  11. #11
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    You could use stringstreams. More C++ish. You can read the int value(s) from the string as if it were an input stream.
    There is a difference between tedious and difficult.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  2. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM