# fgets() problem in a C Programming project



## kylesellers (Aug 4, 2009)

I found this forum because of this thread: http://forums.techguy.org/software-development/845981-solved-c-code-block-help.html

I assume that we are in the same class. I went about the project in a different way using fgets() instead of scanf() since scanf() would only accept a single word.

My problem is that when I enter my menu selection in, it is passing the \n into the respective while loops. At least I think that is happening. I have spent the last 4 days trying to figure out what is going on here and since the project is due tonight I got desperate and decided to ask for help here (I hope this isn't considered cheating!)

Can anyone spot where I'm going wrong? This is my final project and I've already dumped at least 20 hours into it at this point, so I appreciate any help.

Here is my code so far:

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



int main (void)
{
    int MAX = 80;
    char choice;
    char input[MAX];
    char output[MAX];
    printf("Enter the operation of your choice:\n");
    printf("u. Convert to uppercase  l. convert to lowercase\n");
    printf("a. append string         b. print backwards\n");
    printf("c. compare strings\n");
    printf("q. quit\n");      
    
    while ((choice = getchar()) != 'q')
    {
        switch (choice)
        {
            case 'u' :  
            {
                printf("Enter a string:\n");
                while (fgets(input, MAX, stdin) != NULL)
                {
                    for(int i = 0; i <= strlen(input); i++)
                        output[i] = toupper(input[i]);
                    fputs(output, stdout);
                }
            }   
                break;
                
            case 'l' :  
            {
                printf("Enter a string:\n");
                while (fgets(input, MAX, stdin) != NULL)
                {
                    for(int i = 0; i <= strlen(input); i++)
                        output[i] = tolower(input[i]);
                    fputs(output, stdout);
                }
            }   
                break;
                
            case 'a' :  
            {
                char input[MAX + 23];
                
                printf("Enter a string:\n");
                while (fgets(input, MAX, stdin) != NULL)
                {
                    strncat(input, " and a million dollars.", 23); // 23 is the length of " and a million dollars"
                    fputs(input, stdout);
                }
            }   
                break;
                
            case 'b' :  
            {
                int len = strlen(input);
                
                printf("Enter a string:\n");
                while (fgets(input, MAX, stdin) != NULL)
                    for(int i = 0; i < len; i++)
                    {
                        output[len - i] = input[i];
                        output[len + 1] = '\0';
                        fputs(output, stdout);
                    }
            }       
                break;                                               
                
            case 'c' :
            {
                char input2[MAX];
                
                printf("Enter a first string:\n");
                while (fgets(input, MAX, stdin) != NULL)
                {
                    printf("Enter a second string:\n");
                    while (fgets(input2, MAX, stdin) != NULL)
                        if (strstr(input, input2) != NULL)
                            printf("Yes! The second string is contained in the first string!  Crazy, huh?\n");
                        else
                            printf("Sorry, the second string is NOT within the first string.  What'd you expect?\n");
                }
            }
                break;
                
                
            default  :  printf("Please enter u, l, a, b, c, or q.\n");
                break;
                
        }
        
        printf("Enter the operation of your choice:\n");
        printf("u. Convert to uppercase  l. convert to lowercase\n");
        printf("a. append string         b. print backwards\n");
        printf("c. compare strings\n");
        printf("q. quit\n");      
        getchar();
        
    }    
    
    return 0;
}
```
Thanks for any help. This C Programming class is actually my first programming class, and I've been a little overwhelmed with it. Luckily, next wemester is Web Development, which looks like it will be a lot more forgiving!


----------



## InuyashaITB (Jun 20, 2009)

Here is my latest code

http://scootypuffjr.0catch.com/temp/code
 
What happened to me was i needed to add a second gets() statement and that fixed mine skipping


----------



## InuyashaITB (Jun 20, 2009)

Lol dude, are you going to CoC?


----------



## kylesellers (Aug 4, 2009)

Yep, I sure am. Your link is broken, by the way.


----------



## InuyashaITB (Jun 20, 2009)

here this one should work
http://www.scootypuffjr.0catch.com/

Go to the bottom of the page and click on the console window


----------



## kylesellers (Aug 4, 2009)

Okay, I see what you did. I did something similar and just added an extra getchar() to eat the extra input.

Now, I'm just trying to bet my backwards function working without having to resort to swapping individual letters. I am trying to get my input array copied over to an output and print that.


----------

