Connecting Tech Pros Worldwide Forums | Help | Site Map

Help w/Nested Loop

Newbie
 
Join Date: Oct 2006
Posts: 8
#1: Nov 12 '06
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.Ri ck");printf("\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.Ri ck");printf("\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.Ri ck");printf("\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.Ri ck");printf("\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.Ri ck");printf("\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.Ri ck");printf("\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("\nAverage: %i" "%\n\n", sum);
goto END_NOTEST;


END_NOTEST:
printf ("Bye Bye (%s), Better Luck next time\n", &name);
}}}}}}}}}

Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,202
#2: Nov 13 '06

re: Help w/Nested Loop


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

    Expand|Select|Wrap|Line Numbers
    1.     if (some_condition) {
    2.         // Some code here
    3.     }
    4.  
    5.     for (intialisation_expression; condition_expression; iteration_expression) {
    6.         // Some code here
    7.     }
    8.  
    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

    Expand|Select|Wrap|Line Numbers
    1.     if (some_condition)
    2.     {
    3.         // Some code here
    4.     }
    5.  
    6.     for (intialisation_expression; condition_expression; iteration_expression)
    7.     {
    8.         // Some code here
    9.     }
    10.  
    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.
Newbie
 
Join Date: Oct 2006
Posts: 8
#3: Nov 15 '06

re: Help w/Nested Loop


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?

Quote:

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

    Expand|Select|Wrap|Line Numbers
    1.     if (some_condition) {
    2.         // Some code here
    3.     }
    4.  
    5.     for (intialisation_expression; condition_expression; iteration_expression) {
    6.         // Some code here
    7.     }
    8.  
    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

    Expand|Select|Wrap|Line Numbers
    1.     if (some_condition)
    2.     {
    3.         // Some code here
    4.     }
    5.  
    6.     for (intialisation_expression; condition_expression; iteration_expression)
    7.     {
    8.         // Some code here
    9.     }
    10.  
    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.

Reply