-2

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])
    }
}
3
  • 2
    What is happening?
    – K.Nicholas
    Mar 24, 2016 at 16:50
  • 0) Enable compiler warnings. They are not just to show some techno-babble. 1) Indent your code. 2) Use a debugger and see how many times the loop interates (and where you run into trouble). 3) That has been asked soooooooo many times here already. 4) Alternatively re-read about arrays in your C book. Mar 24, 2016 at 16:59
  • 1
    If I understand this correctly, are you assuming that if the user types "746" say then 'numEntered[0] = 7', 'numEntered[1] = 4' and 'numEntered[2] = 6'. That's not quite the way it works.
    – Skizz
    Mar 24, 2016 at 16:59

4 Answers 4

1

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

4
  • I tried it but their seems to be something wrong with the for loop.
    – Kevin Diaz
    Mar 24, 2016 at 16:56
  • 1
    you do realize that this way you read 3 integers not a 3 digit integer; you can read 8888 9999 11111, which is not what the question was about...
    – Pandrei
    Mar 24, 2016 at 16:57
  • @Pandrei i think the answer is correct! this test of case '8888 9999 11111' you have to deal with users input, using if or while for example, even using getchar(), whats prevents you from typing letters like 'abc' instead of three digit number? Still the same case.. you have to deal with user input, by using some condition to prevents wrong test of cases Mar 24, 2016 at 17:04
  • @HanArantes this is more error prone that getchar...it's easy to type in 2 digits by mistake ..not matter what checking you add for the input
    – Pandrei
    Mar 24, 2016 at 17:14
1

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]);
}
0

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]);
}
}
4
  • I thought of using scanf but the problem is that later I will have to change positions of each number. For example if the numbers read are 129 I have to make another interger 291 then 912.
    – Kevin Diaz
    Mar 24, 2016 at 17:00
  • That you can do it using an integer array. q=a[i]/100; a[i]=a[i]%100;a[i]=a[i]*10+q; put the code inside a for loop
    – srccode
    Mar 24, 2016 at 17:03
  • Oh okay thanks. But do you know what is wrong with my for loop ?
    – Kevin Diaz
    Mar 24, 2016 at 17:09
  • 1
    getchar() returns a single character not an array. And if I suppose its a C program you cant declare a variable inside a for loop.
    – srccode
    Mar 24, 2016 at 17:19
0

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;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.