简体   繁体   中英

How do I save integers read by getchar() to an array?

I am trying to make a program that will use getchar() to read three integers and store it in an array but something is wrong in my code.

#include <stdio.h>
int main( )
{
    printf("Please enter three digit number 100 to 999: ");
    int numEntered[2];
    numEntered = getchar();
    for (int i = 0; i<3; i++) {
        printf("%d", numEntered[i])
    }
}

Try this:

#include <stdio.h>

int main(){

    printf("Please enter three digit number 100 to 999: ");

    int numEntered[3];

    for (int i = 0; i<3; i++){
        scanf("%d", &numEntered[i]);
        printf("%d", numEntered[i]);
    }

    return 0;  
}

you need to read a value inside the for loop! Second thing, by reading with getchar(), you are getting the ascii value of the character, so.. if you read "1" and print with %d, you actually printing 49!

See the ascii table here: http://www.asciitable.com/index/asciifull.gif

let's try and and think about the problem here:

  • do you want to read and store an integer value? if yes -> use scanf
  • do you want to read a number digit by digit? if yes -> use getchar
  • do you want to make sure what you read has exactly 3 digits? if yes...what do you do when it does not?
  • if reading digit by digit, make sure you are reading numbers; getchar reads characters -> use atoi funtion or check ascii value;

Putting it all together(some assumptions were made):

int main()
{
   char digits[3]; // don't use ints to store chars...
   printf("enter the 3 digit number - 100 to 999: ");   

   for (int i=0;i<3;i++) // only the first 3 chars are read
   {
       char c = getchar();
       if (char < '0') || (char > '9')
       {
           printf("invalid digit!");
           exit(0);
       }
       digits[i] = c;
   }

   printf("the number entered is: %c%c%c", digits[0],digits[1],digits[2]);
}

You don't use getchar to get integer values. Use scanf() instead.

Try this:

#include <stdio.h>
int main( )
{    
printf("Please enter three digit number 100 to 999: ");
int numEntered[3];  
for (int i = 0; i<3; i++){
   scanf("%d",&a[i]);
   printf("%d", numEntered[i]);
}
}

numEntered = getchar();
(1) It can not be assigned to the array itself.
(2) getchar() reads one character.


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

int main(void){
    char digits[4]  = {0};//4: 3 character + NUL character
    int ch, value;

    do {
        printf("Please enter three digit number 100 to 999: ");
        for(int i = 0; i < 3 && (ch = getchar()) != '\n' && ch != EOF; ++i){
            digits[i] = ch;
        }
        if(ch != '\n')
            while((ch = getchar()) != '\n')
                if(!isspace(ch))//invalid input
                    *digits = 0;

        value = atoi(digits);
    } while(value < 100 || 999 < value);

    char *rotate_left(char digits[4]);

    printf("%d\n", value);
    printf("%d\n", atoi(rotate_left(digits)));
    printf("%d\n", atoi(rotate_left(digits)));

    return 0;
}

char *rotate_left(char digits[4]){
    char temp = digits[0];
    digits[0] = digits[1];
    digits[1] = digits[2];
    digits[2] = temp;
    return digits;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM