Thread: Looping name in c backwards help

  1. #1
    Registered User
    Join Date
    Jul 2015
    Posts
    4

    Looping name in c backwards help

    Hello all,

    For the most part, I have my menu completed. But I am stuck on looping my name backwards. When it does loop backwards from 1-100, for some reason every even number displays non backwards. Also I would like to be able to prompt the user to resubmit their name if they try entering a number, however doing something like if (name >= 'a' && name <= 'z') it says the operand types are incompatible. Anyways, here is my code so far.




    Code:
       #include<stdlib.h>
       #include<stdio.h>
       #include<string.h>
    
    
    
    
    
    
        
        int main ()
        {
    
    
        //declare variables
        int choice;
        char name[255];
        int loopCounter=0;
        int count;
        int number;
        
        do {
    
    
    
    
        printf("***********************\n");
        printf("--Main Menu---\n");
        printf("***********************\n");
        printf("1.Display Your Name\n");
        printf("2.Display Your Name (From 1-100)\n");
        printf("3.Display your Name Backwards\n");
        printf("4.Display Your Name Backwards (From 1-100)\n");
        printf("5.Quit\n");
        printf("Enter your name:\n");
        scanf("%s", name);
        
        printf("Enter your choice:   ");
        scanf("%i", &choice);
        
        
        
    
    
        switch(choice){
    
    
      
        case 1: 
        
        printf("Your name is %s.\n", name);
             
        break;
        
        case 2:  
        printf("How many times to display your name? (1-100) ");
        scanf("%i", &count);
        if ( 100 < count) {
    
    
        printf("Please enter 1 through 100\n");
    
    
    
    
    
    
        }
        else
    
    
    
    
        while (loopCounter < count )
        {
        loopCounter++;
    
    
        printf("%d %s\n", loopCounter,name);
        
        }
        
        break;
    
    
        case 3:
            printf("Your name backwards is: %s\n\n", strrev(name));
        break;
        case 4:
        
            printf("How many times to display your name backwards? ");
            scanf("%i", &count);
    
    
            if ( 100 < count) {
    
    
            printf("Please enter 1 through 100\n");
    
    
    
    
    
    
            }
            else
    
    
          
            while (loopCounter < count )
            {
            loopCounter++;
    
    
            printf("%d %s\n", loopCounter, strrev(name));
        
            }
            break;
    
    
            case 5:
            printf("Goodbye!!!!\n");
            system("pause");
            break;
    
    
            default:
            printf("Was not 1 through 5\n");
            break;
            }
       
    
    
    
    
    
    
    
    
    
    
    
    
    
    
           } while (choice!=5); 
    
    
    
    
       
           }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Welcome to the forum.

    Please be sure to neatly and consistently indent/format your code. It should look more like this:

    Code:
    #include<stdlib.h>
    #include<stdio.h>
    #include<string.h>
    
    int main ()
    {
        //declare variables
        int choice;
        char name[255];
        int loopCounter=0;
        int count;
        int number;
    
        do
        {
            printf("***********************\n");
            printf("--Main Menu---\n");
            printf("***********************\n");
            printf("1.Display Your Name\n");
            printf("2.Display Your Name (From 1-100)\n");
            printf("3.Display your Name Backwards\n");
            printf("4.Display Your Name Backwards (From 1-100)\n");
            printf("5.Quit\n");
            printf("Enter your name:\n");
            scanf("%s", name);
    
            printf("Enter your choice:   ");
            scanf("%i", &choice);
    
            switch(choice)
            {
                case 1:
                    printf("Your name is %s.\n", name);
                    break;
                case 2:
                    printf("How many times to display your name? (1-100) ");
                    scanf("%i", &count);
    
                    if ( 100 < count)
                    {
                        printf("Please enter 1 through 100\n");
                    }
                    else
                        while (loopCounter < count )
                        {
                            loopCounter++;
                            printf("%d %s\n", loopCounter,name);
    
                        }
                    break;
                case 3:
                    printf("Your name backwards is: %s\n\n", strrev(name));
                    break;
                case 4:
                    printf("How many times to display your name backwards? ");
                    scanf("%i", &count);
    
                    if ( 100 < count)
                    {
                        printf("Please enter 1 through 100\n");
                    }
                    else
                        while (loopCounter < count )
                        {
                            loopCounter++;
                            printf("%d %s\n", loopCounter, strrev(name));
                        }
                    break;
                case 5:
                    printf("Goodbye!!!!\n");
                    system("pause");
                    break;
                default:
                    printf("Was not 1 through 5\n");
                    break;
            }
    
        } while (choice!=5);
    }
    While the compiler does not care, it makes it easier for anyone reading the code (especially you) to follow the flow of logic by eye.



    When it does loop backwards from 1-100, for some reason every even number displays non backwards.
    This is because you're calling "strrev()" in the loop. So the first time, it reverses the string (so it's backwards) and prints it. The next time, it reverses the string again (so it's forwards again) and prints it. Etc etc.

    The fix should be obvious.

    You should also reset "loopCounter" to zero before each loop. Currently, it retains the last value it counted up to. This will throw off calculations if the user tries to run option 2 or 4 more than once.

    Also note that "strrev()" is not a standard function. If it's working for you with your current set-up, that's fine, but just be aware.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Print backwards? Can someone please help me?
    By matthayzon89 in forum C Programming
    Replies: 11
    Last Post: 04-23-2010, 07:30 AM
  2. Backwards
    By cyberCLoWn in forum C++ Programming
    Replies: 1
    Last Post: 04-07-2004, 08:11 AM
  3. Backwards Up-Down
    By Cactus_Hugger in forum Windows Programming
    Replies: 1
    Last Post: 11-08-2003, 05:56 PM
  4. Backwards Compatability...
    By Jperensky in forum Linux Programming
    Replies: 6
    Last Post: 06-18-2003, 06:43 PM
  5. How Backwards Functions
    By JLBSchreck in forum C++ Programming
    Replies: 14
    Last Post: 06-06-2003, 08:17 PM

Tags for this Thread