By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,605 Members | 1,473 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,605 IT Pros & Developers. It's quick & easy.

Help w/Nested Loop

P: 8
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);
}}}}}}}}}
Nov 12 '06 #1
Share this Question
Share on Google+
2 Replies


Banfa
Expert Mod 5K+
P: 8,916
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.
Nov 13 '06 #2

P: 8
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?

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.
Nov 15 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.