0

EDIT Sorry, seems I didn't read enough previous questions to figure this one out. Trying out the ideas here led me to the getchar() function, which both solved the newline problem and the while loop.

alk's suggestion in the comments didn't seem to work at all, I could hit Enter as many times as I wanted and it wasn't doing anything.

Thanks to the mod who linked the duplicate question.

++++++++++++++++++++++

So I have an assignment to do using C. One of the components is for the user to input a title. The code snippet I have so far is:

char accept;
char title [30];

do
{
    printf("Please enter a title for the simulation: "); 
    fgets(title,30,stdin);

    for ( i=0; i<30; i++)
    {
        if (title[i] == '\n')   
        {                       
            title [i] = '\0';
            break;
        }
    };
    printf("\nContinue? (Y/N): ");
    scanf(" %c", &accept);

//***Title to be displayed at the end with rest of test conditions***
} while ((accept == 'n') || (accept== 'N'));

Note: The while loop is a small-scale version that will encompass the entire program to give the option of starting over.

As I understand it, the for loop will remove the newline character after the fgets() input, however if the N option is chosen the fgets() is being skipped over again, and I'm really not sure why.

Community
  • 1
  • 1

0 Answers0