How to go back to parts of the program?

This is a discussion on How to go back to parts of the program? within the C Programming forums, part of the General Programming Boards category; Hello, Im new to this forum. Currently I am beginning to learn C and I have started a little quiz ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    8

    Post How to go back to parts of the program?

    Hello, Im new to this forum. Currently I am beginning to learn C and I have started a little quiz program and need help with one aspect of it

    Code:
    #include<stdio.h>
    main()
    {
         int i;
         char str[]="You are correct!";
         char str2[]="You are incorrect, please start over";
         char str3[]="yes";
         char str4[]="no";
         char str5[5];
         
         printf("Welcome to the quiz\n");
         printf("There are 5 questions\nplease type yes or no to the questions\n");
         printf("If you get a question wrong, start over!\n");
         
         printf("I am a girl. yes or no?\n");//question 1
         gets(str5);
         i = 0;
         if (str5 == str4){
                  printf"Please start over\n");
    How can i make it so that the program goes back to printf("Welcome to the quiz\n"); when there is an incorrect answer

    any other improvements on my code would be nice, im always happy to learn
    Thanks!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you feel like being structured, you want to use a loop. You can use a goto, if you have the proper safety equipment.

    Also, you should note that the condition "str5==str4" will always and forever be false. Two different arrays (which str4 and str5 are) can never compare equal. To compare the strings that str4 and str5 may be, you should use strcmp in the <string.h> header.

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    8
    I fixed up the comparison of the strings but still am confused about the jumping. how is the goto used?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you search the board using "goto" as a keyword, you will find some examples of its use. Further than that I will not go.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The main() function calls the menu() function. The menu() function has a huge do-while loop in it, so everything branches out from the menu() according to the user's choice, and then it returns to the large do-while loop, back in menu.

    Code:
    int menu()   {
        char choice;  
       //etc.
    
       do   {
    
           system("cls");
           print your header here
           print a space or two
           print the body of your menu, here
    
           Get user's choice
           choice = tolower(choice);
    
           switch(choice)   {
    
             case 'a': handle_case_a(); output_from_a(); break;
             case 'b': handle_case_b(); break;
             //etc.
    
             default: print "Oops! I don't have a response for that choice. Please try again."
    
           }
    
       }while(choice != 'q');
       return 0;
    }
    Something along those lines should put you on the right track. *Please*, don't use "goto's", for crying out loud!
    Last edited by Adak; 07-16-2008 at 01:07 AM.

  6. #6
    Registered User
    Join Date
    Jul 2008
    Posts
    8

    Angry Goto oh no....

    after the first post I resulted to goto but i know its a bad habit so I need to change it!! here is my program with goto
    Code:
     next:
         do{
         printf("Magnum opus means great work. yes or no?\n");//question 1
         gets(str5);
         } while (strcmp (str3, str5) != 0);//correct answer is yes
         puts ("Correct answer!");
         
         printf("A paragon is a self contradictory statement. yes or no?\n");//question 2
         gets(str5);
         if (strcmp (str4, str5) != 0){ //correct answer is no 
                    printf("%s\n", str2);
                    c = getc(stdin);
                    goto next;
                    }
         else{
              printf("A dystopia is a perfect world. yes or no?\n");
              }
              gets(str5);
         if (strcmp (str4, str5) != 0){ //correct answer is no 
                     printf("%s\n", str2);
                      c = getc(stdin);
                    goto next;
                    }

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Adak's post should let you figure out how to eliminate the goto.

    Another hint:
    Code:
    do {
        /* ... */
    } while(quit == 0);
    @tabstop: probably not a good idea to mention goto to someone just learning C . . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Instead of the ugly line "goto next", why not use the more elegant "continue".

    I have nothing against goto's - they are exactly what the assembly language is probably using. But we're not assembly compilers, and we don't want goto's in a program except in a very few circumstances, because they are SO EASY to abuse, and make the resulting program SO HARD to follow, when you have to modify/extend/or correct it.

    True story:
    When I was learning to program, the instructor related how he had been offered $50,000 if he would re-write a current program in use by the college. (This was when $50,000 was worth 3-5 X as much as it is, today - so a *lot* of money.)

    You guessed it, it was an older program, that was riddled with goto's - goto's here, goto's there, goto's coming out your ears, nose, and well, you name it.

    He declined, and so did every contractor who reviewed samples of the source code. The "noodle" effect of the goto's was just *unbelievable*. The program was eventually re-written from the ground up, because of it.

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    gotos and gets() - ugh.
    Worse still, while you're still proud of it, you're beyond help.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    * noops's Avatar
    Join Date
    Jun 2008
    Posts
    108
    One improvement is to avoid cryptic names for your variables. str4, str3, are not very descriptive. Name them something humanreadable like 'yes_answer', 'no_answer'.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  2. my server program auto shut down
    By hanhao in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-13-2004, 09:49 PM
  3. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  4. Clocking a program, and parts of it
    By Boksha in forum C Programming
    Replies: 4
    Last Post: 03-18-2002, 05:02 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21