Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
113 views
in Technique[技术] by (71.8m points)

c - Using a for loop to add integers from an array

I'm writing a program that takes in your student number(8 digits long), prints each digit on its own new line, and then gets the sum of all the digits in the number (E.g. Student Number - 20305324, Sum - 19)

#include <stdio.h>
#include <string.h>

int main(void) {
  char student_number[8];
  int i = 0;
  int sum = 0;

  printf("Enter your student number: ");
  scanf("%s", student_number);

  // ensures input is only 8 digits - WORKS
  while (strlen(student_number) < 8 || strlen(student_number) > 8){
    printf("Enter your student number: ");
    scanf("%s", student_number);    
  }

  // prints each digit of the student number on a new line - WORKS
  while (student_number[i] != ''){
    printf("%c
", student_number[i]);
    i++;
  }
  

  // sum all the digits in the student number and print - DOESN'T WORK 
  for (i=0;i<8;i++){
    sum = sum + student_number[i];
    printf("%d
", sum);
  }

  printf("Sum of the numbers is %d", sum);



}

OUTPUT

The problem I'm encountering is when my for loop attempts to add each digit in the student number. The output I expect here is 19, but for some reason the sum evaluates to some bizarre number like 403

}Output

Would someone mind pointing out where exactly the fault in my for loop is or if it is elsewhere? Thanks :)

question from:https://stackoverflow.com/questions/65944428/using-a-for-loop-to-add-integers-from-an-array

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Firstly, your array char student_number[8]; cannot hold 8-character string because there are no room for terminating null character. You must allocate one more element.

Then, you should convert the characters to corresponding numbers. Character codes for digits are defined to be continuous, so this can be done by subtracting '0' from the character code.

Also you should set a limit of length of string to read via scanf() to avoid buffer overrun. One more good practice is checking the return values of scanf() to see if something is successfully read.

Fixed code:

#include <stdio.h>
#include <string.h>

int main(void) {
  char student_number[10]; // *** allocate enough elements (one more than needed to catch too long input)
  int i = 0;
  int sum = 0;

  printf("Enter your student number: ");
  if(scanf("%9s", student_number) != 1){ // *** limit the length to read and check the result
    fputs("read error
", stderr);
    return 1;
  }

  // ensures input is only 8 digits - WORKS
  while (strlen(student_number) < 8 || strlen(student_number) > 8){
    printf("Enter your student number: ");
    if(scanf("%9s", student_number) != 1){ // *** limit the length to read and check the result
      fputs("read error
", stderr);
      return 1;
    }
  }

  // prints each digit of the student number on a new line - WORKS
  while (student_number[i] != ''){
    printf("%c
", student_number[i]);
    i++;
  }
  

  // sum all the digits in the student number and print -DOESN'T WORK 
  for (i=0;i<8;i++){
    sum = sum + (student_number[i] - '0'); // *** convert characters to numbers before adding
    printf("%d
", sum);
  }

  printf("Sum of the numbers is %d", sum);



}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...