0

Here is the code:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define BUFFER 512


void getCount(int *numCount, int *count);
int sumNumbers(int *numSum, int *sumNumOutput);

int main(void) {

  printf("Enter a number greater than 0: ");

  char string[BUFFER]; 
  int numMain = 0;
  int countMain = 0;
  int sumNumMain = 0;

  fgets(string, BUFFER, stdin); // gets user input and stores it in string

  numMain = atoi(string); // converts the string to numerical and sets sum to the value. If there is a letter in the string, it will be zero.

  int numCountMain = numMain;


int numSumNum = numMain;

  getCount(&numCountMain, &countMain); // gets how many integers there are
  sumNumbers(&numSumNum, &sumNumMain); 

  printf("Count: %d\n", countMain);
//  printf("Sum: %d\n", sumNumMain);
  return 0;
}

//shows how many integers were entered
void getCount(int *numCount, int *count){

  while(*numCount > 0){

  *numCount /= 10;
  ++*count;
}
return;
}

int sumNumbers(int *numSum, int *sumNumOutput){ // make it so that it isolates a number, then adds it to a universal sum variable
  int increment = 1;
  int count = 0;

  while(*numSum > 0){ // gets the count of the number

    while(*numSum > 0){

      *numSum /= increment;
      ++count;
      printf("numSum: %d\n",*numSum);
      increment *= 10;
    }
  }
}

Let's say I put in 12345 as the number. It counts the number of digits in there just fine, but when it gets to isolating the individual digits using division, it skips over the third number. In the case of 12345, it would be: 12345 1234 12 0

I'm thinking this is a case of the increment running amok, but I can't find a fix for this. Also I know when I fix this, it will not solve the problem that I have to isolate the individual numbers. That's where the increment comes in and I know I have to use the modulus, but if someone can help me out with that after I take care of this, that would be great too.

Also, in case it isn't obvious, the code that has the problem I'm assuming is the bottom lines.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
DFT95
  • 27
  • 5
  • What is the reason for `while(*numSum > 0){ while(*numSum > 0){` vs just one `while(*numSum > 0){`? – chux - Reinstate Monica Oct 18 '18 at 02:25
  • Did you mean to have two nested while loops with the same condition? Your best bet to solve this is to take a look at it in a debugger or work it out on paper. Do you need to divide by larger and larger numbers if you're also dividing the number you started with? Think about what `/=` is doing while `increment` is increasing. You might also think about getting rid of all the pointers and just returning the value instead, especially since your function says it returns an int and you do not. – Retired Ninja Oct 18 '18 at 02:27
  • Corner case. Do you consider zero to have 0 or 1 digit as with `getCount()`? – chux - Reinstate Monica Oct 18 '18 at 02:29
  • Yea that was an oversight on my part, I saw it immediately after I posted this and got rid of it – DFT95 Oct 18 '18 at 02:42

1 Answers1

1

You are dividing by 1, 10, 100, 1000. So you are getting 12345, 1234, 12.

Try

while (*numSum > 0) {
  ++count;
  printf("numSum: %d\n",*numSum);
  *numSum /= 10; 
}
Nate Anderson
  • 18,334
  • 18
  • 100
  • 135
Doug Currie
  • 40,708
  • 1
  • 95
  • 119