0

I'm working in C and I want to make a loop that will make my code keep asking for input until I input ""?

char inputarray[1000];
int size;
printf("\nEnter a string: ");
fgets(inputarray,1000,stdin);  
size = strlen(inputarray);
printf("%d \n", size);
printf("%s \n", inputarray);
4
  • Also I'm new to stack overflow and don't know how to make a new line when inputting code. Will be glad to hear any solutions Commented Nov 14, 2020 at 17:52
  • You might want to check out about memcmp or strcmp functions Commented Nov 14, 2020 at 18:12
  • And you might want to clear out what the exit condition is Commented Nov 14, 2020 at 18:16
  • Just as a side note: You should normally always check the return value of fgets to see if an end-of-file or error condition occurred. For example, if standard input has been redirected to come from a file, then it is likely for an end-of-file condition to occur. However, this is not related to your problem, so you can ignore this advice for now. Commented Nov 14, 2020 at 18:17

1 Answer 1

1

I guess you are looking for something like this:

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

int main(void)
{
  char inputarray[1000];
  int size;

  do
  {
    printf("\nEnter a string: ");
    if(!fgets(inputarray,1000,stdin)) /* Check for EOF , or NULL upon return */
    {
      return -1;
    }
    size = strlen(inputarray)-1; /* Ignoring '\n' */
    printf("%d \n", size);
    printf("%s ", inputarray); /* No need for '\n' since 
                                fgets stores every character including newline in the buffer */

  }while(strcmp(inputarray,"\n")); /* Meaning strcmp doesnt return 0 */

  return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.