Help w/Nested Loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chiefs
    New Member
    • Oct 2006
    • 8

    #1

    Help w/Nested Loop

    I'm a beginner in C++!!
    #1-Dont know how to loop end when int q(entered); int q is a very large # when ran: printf("\n%i#of Questions\n", q);
    #2-is the line "how many questions Right"?
    #3-Bypassing Welcome to the Multiple Choice Program, Goto Start; and ends when line for questions ((>= '1') && (<= '50')) is True!!

    -Normally this would work, what am I doing wrong

    // Program Q1:Q1.cpp
    # include <iostream.h>
    # include <stdio.h>
    # include <string.h>
    # include <conio.h>

    main()
    {

    char name[30];
    int n=0;
    char c;
    int q;
    int sum;
    int NG;

    ASK:
    printf ("Please enter the Number of Questions (1-50) ?\n", q);
    scanf ("%i", q);

    if ((q >= '1') && (q <= '50'))
    { printf ("Get Ready,(%s), !!!!!!\n",&name );
    goto START;}

    else
    { printf ("Invalid Entry, Try Again!!\n");
    scanf ("%i", q);}
    if ((q >= '1') && (q <= '50'))
    { printf ("Finally, (%s), it took you long enough,!!!\n", &name);
    goto START;}

    else
    { goto END_NOTEST;}


    {START:

    printf ("\nWelcome to the First Multiple Choice Test\n");
    printf ("Created by Jonathan J. Soard, Oct 06\n");

    TEST:
    NG=0;
    for(n=1;n < q;)
    if (n != 'q')
    {goto Q_1;}
    else
    {goto L_Q;}
    {Q_1:
    clrscr();printf ("Q%i --------------#1?\n", n++);
    printf("\n1.Bob ");printf("\n2. Jon");printf("\ n3.Rick");print f("\n4.Roger\n" );scanf ("%s", &c);
    if ((c == 'A'))
    {printf("2 Good\n");
    NG++;goto Q_2;}

    {Q_2:
    clrscr();printf ("Q%i --------------#2?\n", n++);
    printf("\n1.Bob ");printf("\n2. Jon");printf("\ n3.Rick");print f("\n4.Roger\n" );scanf ("%s", &c);
    if ((c == 'A'))
    {printf("2 Good\n");
    NG++;goto Q_3;}

    {Q_3:
    clrscr();printf ("Q%i --------------#3?\n", n++);
    printf("\n1.Bob ");printf("\n2. Jon");printf("\ n3.Rick");print f("\n4.Roger\n" );scanf ("%s", &c);
    if ((c == 'A'))
    {printf("2 Good\n");
    NG++;goto Q_4;}

    {Q_4:
    clrscr();printf ("Q%i --------------#4?\n", n++);
    printf("\n1.Bob ");printf("\n2. Jon");printf("\ n3.Rick");print f("\n4.Roger\n" );scanf ("%s", &c);
    if ((c == 'A'))
    {printf("2 Good\n");
    NG++;goto Q_5;}

    {Q_5:
    clrscr();printf ("Q%i --------------#5?\n", n++);
    printf("\n1.Bob ");printf("\n2. Jon");printf("\ n3.Rick");print f("\n4.Roger\n" );scanf ("%s", &c);
    if ((c == 'A'))
    {printf("2 Good\n");
    NG++;goto L_Q;}

    {L_Q:
    clrscr();printf ("Q%i --------------#6?\n", n++);
    printf("\n1.Bob ");printf("\n2. Jon");printf("\ n3.Rick");print f("\n4.Roger\n" );scanf ("%s", &c);
    if ((c == 'A'))
    {printf("2 Good\n");
    NG++;}

    {print_1:
    sum=NG/q * 1000;
    printf("Answers Overview\n");
    printf("Your answers---");
    printf("\n%i#of Questions\n", q);
    printf("\nGood Answers: %i", NG);
    printf("\nAvera ge: %i" "%\n\n", sum);
    goto END_NOTEST;


    END_NOTEST:
    printf ("Bye Bye (%s), Better Luck next time\n", &name);
    }}}}}}}}}
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Here are some tips
    1. Don't use goto (or labels), generally they are frowned upon because the break the rules of structured programming. I will go so far as saying there are some cases where they can be of use but you will not run into any of these until you become a more experienced programmer. I think all of you gotos can be replace with the use of loop control structures and/or a switch statement.
    2. Do format you code in a sensible manor, this applies particularly to how you have layed out your braces, there are 2 common standards,

      Firstly opening brace is on the same line of code as the statement controling the code block, closing brace is on a line of it's own at the same level as the controling statement, code is indented 1 tab level from controling statement, for example

      Code:
          if (some_condition) {
              // Some code here
          }
      
          for (intialisation_expression; condition_expression; iteration_expression) {
              // Some code here
          }
      or secondly opening brace is on a line of it;'s own at the same level as the statement controling the code block, closing brace is on a line of it's own at the same level as the controling statement, code is indented 1 tab level from controling statement, for example

      Code:
          if (some_condition)
          {
              // Some code here
          }
      
          for (intialisation_expression; condition_expression; iteration_expression)
          {
              // Some code here
          }
      I suggest you use which ever of these appeals to you more.


    By the sounds of it your code gets to the for statement without initialising q, unfortuneately becuase of the layout and the use of goto it is not clear what the execution path is.

    Comment

    • chiefs
      New Member
      • Oct 2006
      • 8

      #3
      Banfa,

      Can you give me an example on replacing the goto with a loop control and switch statements. Can I use code from a another file. I have tried this and get errors. Can you give me an example of linking, reading and going back to the original file?

      Originally posted by Banfa
      Here are some tips
      1. Don't use goto (or labels), generally they are frowned upon because the break the rules of structured programming. I will go so far as saying there are some cases where they can be of use but you will not run into any of these until you become a more experienced programmer. I think all of you gotos can be replace with the use of loop control structures and/or a switch statement.
      2. Do format you code in a sensible manor, this applies particularly to how you have layed out your braces, there are 2 common standards,

        Firstly opening brace is on the same line of code as the statement controling the code block, closing brace is on a line of it's own at the same level as the controling statement, code is indented 1 tab level from controling statement, for example

        Code:
            if (some_condition) {
                // Some code here
            }
        
            for (intialisation_expression; condition_expression; iteration_expression) {
                // Some code here
            }
        or secondly opening brace is on a line of it;'s own at the same level as the statement controling the code block, closing brace is on a line of it's own at the same level as the controling statement, code is indented 1 tab level from controling statement, for example

        Code:
            if (some_condition)
            {
                // Some code here
            }
        
            for (intialisation_expression; condition_expression; iteration_expression)
            {
                // Some code here
            }
        I suggest you use which ever of these appeals to you more.


      By the sounds of it your code gets to the for statement without initialising q, unfortuneately becuase of the layout and the use of goto it is not clear what the execution path is.

      Comment

      Working...