1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

fgets() problem in a C Programming project

Discussion in 'Software Development' started by kylesellers, Aug 4, 2009.

Thread Status:
Not open for further replies.
Advertisement
  1. kylesellers

    kylesellers Thread Starter

    Joined:
    Aug 4, 2009
    Messages:
    4
    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:
    Code:
    #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!
     
  2. Sponsor

  3. InuyashaITB

    InuyashaITB

    Joined:
    Jun 20, 2009
    Messages:
    20
  4. InuyashaITB

    InuyashaITB

    Joined:
    Jun 20, 2009
    Messages:
    20
    Lol dude, are you going to CoC?
     
  5. kylesellers

    kylesellers Thread Starter

    Joined:
    Aug 4, 2009
    Messages:
    4
    Yep, I sure am. Your link is broken, by the way.
     
  6. InuyashaITB

    InuyashaITB

    Joined:
    Jun 20, 2009
    Messages:
    20
  7. kylesellers

    kylesellers Thread Starter

    Joined:
    Aug 4, 2009
    Messages:
    4
    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.
     
  8. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/849215