0

I am very new to C. Trying to practice with input and output now. I am trying to use fgets to read in a sentence and convert it to all upper case letters, but for some reason it's not reading it. I HAVE ADDED *c in MY SCANF AND IT STILL DOES NOT WORK. Any suggestions and tips are greatly appreciated! Thanks in advance.

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

#define ARR_SIZE 100

void modify(int pInt[100], int input);

void modify2(char *pString[100]);

int main() {
    printf("Enter a size for your array:\n");
    int input;
    scanf("%d", &input);
    printf("Great! You now have an array of size %d.\n", input);
    int array[ARR_SIZE];
    printf("Now please enter %d numbers.\n", input);
    int i;
    for (i = 0; i < input; i++) {
        scanf("%d*c", &array[i]);
    }
    printf("Awesome. All %d slots in the array are now filled.\n", input);
    printf("Here is how your array looks:\n");
    int j;
    for (j = 0; j < input; j++) {
        printf(" %d |", array[j]);
    }
    printf("\n");
    modify(array, input);
    printf("Now your array looks like this:\n");
    for (j = 0; j < input; j++) {
        printf(" %d |", array[j]);
    }
    printf("\n");

    printf("Experiment #2:\n");
    printf("Enter a sentence:\n");
    char sentence[ARR_SIZE];
    fgets(sentence, ARR_SIZE, stdin);
    modify2(sentence);
    printf("Now your sentence is upper case: %s\n", sentence);
    return 0;
}

void modify2(char *pString[100]) {
    int i = 0;
    while (pString[i]) {
        pString[i] = toupper(pString[i]);
        i++;
    }
}

void modify(int pInt[100], int input) {
    int i;
    for (i = 0; i < input; i++) {
        pInt[i] *= 2;
    }
}
taralee98
  • 127
  • 1
  • 12
  • 1
    Please see [scanf() leaves the newline char in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer). – Weather Vane Mar 04 '19 at 17:58
  • There are multiple posts on so that address this issue. See https://stackoverflow.com/search?q=why+fgets+skips++a+line. Example: https://stackoverflow.com/questions/35247514/program-is-skipping-fgets-without-allowing-input – R Sahu Mar 04 '19 at 17:58
  • "new to C" and "scanf" in the same sentence is a recipe for disaster. If you are new to C, do not use scanf at all. If you are an intermediate or advanced user (whatever those terms mean!) you should also avoid scanf, but that's beyond the scope of this question. – William Pursell Mar 04 '19 at 18:15

0 Answers0