4

I am trying to read a string into a char array with a length chosen by the user. The problem is that getchar() doesn't stop reading until the user manually enters a newline by pressing enter, based on my code. I have read through other's threads, and I understand why I'm not able to do it this way, it's just completely contradictory to my assignment handout.

int chPrompt(int nchars);
void strInput(char str[], int nchars);

int main(void) {
    int nchars = chPrompt(nchars);
    char str[nchars];

    strInput(str, nchars);

    return 0;
}

int chPrompt(int nchars) {
    printf("How many chars do you need to input? >");

    return scanf("%i", &nchars);
}

void strInput(char str[], int nchars) {
    int i = 0;

    while((str[i] = getchar()) != '\n') {
        if(i > nchars-1)
            break;
        i++;
    }
    str[i] = '\0';

    //Troubleshooting
    printf("%s %d", str, strlen(str));
}

This is what the handout says:

Input a string from the keyboard (spaces included) using the technique we talked about (while with getchar(), not gets() , fgets()or scanf() ), augmented so that it will input any amount up to, but no more than, 80 characters. Be sure that there’s a null in the proper location after the input.

The technique we talked about in class was the while loop with getchar assignment to char array.

My question: My professor is very adamant about his instructions. In this handout, he is specifically telling me to input any amount up to, but no more than, 80. This is contradicting the functionality of getchar, correct? Is there any way to limit the length of a string, using this 'technique'?

On some of the threads I found, people mentioned it might be OS dependent. So, if that matters, I am on Windows 8.1.

turbo
  • 1,233
  • 14
  • 36
LukeFi
  • 51
  • 1
  • 1
  • 5
  • `char str[nchars];` → `char str[nchars+1];`. You missed space for null character. What you are reffering to is "buffered input mode". – Grzegorz Szpetkowski Nov 17 '14 at 00:22
  • @GrzegorzSzpetkowski Taken care of! Thanks for the reminder. Is there any way to work around the buffered input mode? Or am I misinterpreting the handout? – LukeFi Nov 17 '14 at 00:27
  • "This is contradicting the functionality of getchar, correct?" -- No, not correct. You seem to have getchar mixed up with fgets. – Jim Balter Nov 17 '14 at 00:45
  • It's not clear for me what your professor is asking for. If he means to take literally "up to, but no more than, 80 characters" in the sense, that no more characters can be "physically" typed in console, then it's not possible to do in portable way in C. – Grzegorz Szpetkowski Nov 17 '14 at 00:59
  • @GrzegorzSzpetkowski If that is the case, then I believe this is solved :) Thank you. – LukeFi Nov 17 '14 at 01:08

3 Answers3

5

Op's code is close.

"getchar() doesn't stop reading until the user manually enters a newline by pressing enter" is incorrect.
Typical user input is line buffered. Nothing is given to the program until Enter occurs. At that time the entire line is given to the program. getchar() consumes 1 char at a time from stdin.

1) Need to allocate sufficient buffer memory @Grzegorz Szpetkowski
2) Read input as an int and read extra as needed.
3) Do not return the value from scanf() as the number of to read.
4) Read remaining line after reading the number of char to be read. @Grzegorz Szpetkowski

getchar() returns an unsigned char or EOF. That is typically 257 different results. Reading getchar() into a char loses that distinction.

void strInput(char str[], int nchars) {
  int i = 0;
  int ch;
  while((ch = getchar()) != '\n' && ch != EOF ) {
    if (i < nchars) {
       str[i++] = ch;
    }
  }
  str[i] = '\0';
}

int main(void) {
  int nchars = chPrompt(nchars);
  char str[nchars + 1];  // + 1
  strInput(str, nchars);

  //Troubleshooting
  printf("'%s' %zu", str, strlen(str));

  return 0;
}

int chPrompt(int nchars) {
  printf("How many chars do you need to input? >");
  if (scanf("%i", &nchars) != 1) {
    printf("Unable to read #\n"); 
    exit(-1);
  }

  // Consume remaining text in the line
  int ch;
  while((ch = getchar()) != '\n' && ch != EOF );

  return nchars;
}

Note: strlen() returns type size_t, not int, this may/may not be the same on your platform, best to use the right format specifier "%zu" with strlen(). Alternatively use:

printf("'%s' %d", str, (int) strlen(str));
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • For some reason, after inputting nchars, it outputs the printf I was troubleshooting with and then exits the program. I reverted to my original code with ctrl+z and now my code is doing the same. Not so sure what went wrong :S – LukeFi Nov 17 '14 at 00:49
  • 1
    @LukeFi: Note that `scanf()` call leaves "orphaned" `'\n'` character in `stdin`. Use `while (getchar() != '\n');` (you might put semicolon in separate line with indentation for clarity) to get rid of it. It is little tricky to combine `scanf()` with forthcoming `getchar()`. – Grzegorz Szpetkowski Nov 17 '14 at 00:52
  • @LukeFi Grzegorz Szpetkowski is correct. Per your post, looks like you can hard code the length 80 into the code and not query for it. – chux - Reinstate Monica Nov 17 '14 at 00:54
  • @LukeFi Alternatively after reading the number, `int ch; while ((ch = getchar()) != '\n' && ch != EOF);` to clean-out remaining `char` in `chPrompt()`. – chux - Reinstate Monica Nov 17 '14 at 00:57
  • @GrzegorzSzpetkowski I have been getting headaches about that newline in stdin for hours! Didn't realize it was left there by the scanf. I always thought the newline after scanf was just an output. Thank you. – LukeFi Nov 17 '14 at 01:03
  • @chux My professor demonstrated how to clear out the stdin, the way you did, but never gave us any scenario when it might be necessary, other than garbage data in memory. Thank you. – LukeFi Nov 17 '14 at 01:05
  • @chux One more thing. What is the purpose of exit(-1)? – LukeFi Nov 17 '14 at 01:20
  • @LukeFi `exit()` will exit the program. The post has not described what should happen should the user enter something like "abc" when asked "How many chars do you need to input?", so this answer demonstrates the detection of the errant input and exits the program. You may wish to handle it otherwise. – chux - Reinstate Monica Nov 17 '14 at 01:28
  • @chux Thank you, for clarifying :) PS: the %zu specifier literally ouputs "zu" (without quotes of course). I will have to use the latter for dealing with strlen(). – LukeFi Nov 17 '14 at 01:38
  • @LukeFi What compiler are you using? Maybe an old one? – chux - Reinstate Monica Nov 17 '14 at 02:20
  • @chux I am using the compiler in Dev-C++(5.7.1). To be more specific, I think it's the TDM-GCC 4.8.1 64-bit version. It's the compiler that is available in the computer lab on my campus. I figured I would avoid complications by installing what was available at school on my own computers. Would you recommend another one? – LukeFi Nov 17 '14 at 06:05
  • @LukeFi For school, suggest becoming somewhat proficient with one environment and then move to another such as [Eclipse](https://eclipse.org/cdt/) and another for maximum learning. – chux - Reinstate Monica Nov 17 '14 at 12:47
0

This code could be corrected in few more places (e.g. counting the characters inputted so that you allow the user to input no more than 80 characters, etc.) but this will point you in the right direction:

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

void strInput(char str[], int nchars);

int main(void) {
    int nchars = 0;

    printf("How many chars do you need to input?\n");
    scanf("%d\n", &nchars);

    char str[nchars+1];

    strInput(str, nchars);

    return 0;
}

void chPrompt(int nchars) {
}

void strInput(char str[], int nchars) {
    int i = 0;

        char c;
    while((c = getchar()) != '\n' && i <= (nchars-1)) {
            str[i] = c;
            i++;
    }
    str[i] = '\0';

    printf("%s %d\n", str, (int)strlen(str));
}
syntagma
  • 23,346
  • 16
  • 78
  • 134
  • I applied the changes, but for some reason the program exits right after the nchars prompt. The only output is the printf statement I was using for troubleshooting. – LukeFi Nov 17 '14 at 00:41
  • It exits as there is nothing else to do. Change it whatever you'd like. – syntagma Nov 17 '14 at 00:43
  • Okay, I think it is matching the criteria on the handout now. It reads the specified amount to the string, but the getchar is still filling up the buffer until the user manually enters a newline by pressing enter. Is there no way around that? Thank you for the help :) – LukeFi Nov 17 '14 at 00:59
0

little change to above answers,Try this it is not giving any "buffer full error"

  #include<stdio.h>
#include<string.h>
void getinput(char str1[],int s){
    int i=0;
    printf("inside fn\n");
    char c;
    while((c=getchar())!=EOF && i<=(s-1))
    { str1[i++]=c;
    }
    str1[i]='\0';}
void main()
{ int size;
    printf("enter the no. of characters \n");
    scanf("%d", &size);
    char str1[size+1];
    getinput(str1,size);
    printf("%s \n",str1);
}
meivinay
  • 9
  • 4