Thread: goto and Yes or No problem

  1. #1
    poisonmist
    Join Date
    Jan 2013
    Posts
    10

    goto and Yes or No problem

    Hi! I'm a newbie. I need to make a program to input grades, then in the end, it will ask if you want to repeat or not. "Do you want to repeat [Y/N]?"

    If yes, it will repeat again. If no, the program will terminate. I made Y work, but not N. So it's like stuck at repeating, and I don't know how to stop it. I tried to imitate the codes from other forums but it doesn't seem to work.

    By the way, is there a way to make this shorter?

    Code:
     
    #include <stdio.h>
    #include <conio.h>
    int main(){
     
    int Z,X,C,V,B,N,M,A,S,D,F,G,H,J,K,L,Q,W,E,R;
    char ans;
    first:
    
        printf(“Input the grades of 20 students. \n\n\n”);
     
    printf("Student 1's grade: \n");
    scanf("%d",&Z);
    if ((Z>=90)&&(Z<=100))
        printf(“Excellent\n\n”);
    else if (Z>=80)
        printf(“Above Average\n\n”);
    else if (Z>=70)
        printf(“Average\n\n”);
    else if (Z>=60)
        printf(“Satisfactory\n\n”);
    else
        printf(“Failure\n\n”);
     
    printf("Student 2's grade: \n");
    scanf("%d",&X);
    if ((X>=90)&&(X<=100))
        printf(“Excellent\n\n”);
    else if (X>=80)
        printf(“Above Average\n\n”);
    else if (X>=70)
        printf(“Average\n\n”);
    else if (X>=60)
        printf(“Satisfactory\n\n”);
    else
        printf(“Failure\n\n”);
     
    printf("Student 3's grade: \n");
    scanf("%d",&C);
    if ((C>=90)&&(C<=100))
        printf(“Excellent\n\n”);
    else if (C>=80)
        printf(“Above Average\n\n”);
    else if (C>=70)
        printf(“Average\n\n”);
    else if (C>=60)
        printf(“Satisfactory\n\n”);
    else
        printf(“Failure\n\n”);
    printf("Student 4's grade: \n");
    scanf("%d",&V);
    if ((V>=90)&&(V<=100))
        printf(“Excellent\n\n”);
    else if (V>=80)
        printf(“Above Average\n\n”);
    else if (V>=70)
        printf(“Average\n\n”);
    else if (V>=60)
        printf(“Satisfactory\n\n”);
    else
        printf(“Failure\n\n”);
     
    printf("Student 5's grade: \n");
    scanf("%d",&B);
    if ((B>=90)&&(B<=100))
        printf(“Excellent\n\n”);
    else if (B>=80)
        printf(“Above Average\n\n”);
    else if (B>=70)
        printf(“Average\n\n”);
    else if (B>=60)
        printf(“Satisfactory\n\n”);
    else
        printf(“Failure\n\n”);
     
    printf("Student 6's grade: \n");
    scanf("%d",&N);
    if ((N>=90)&&(N<=100))
        printf(“Excellent\n\n”);
    else if (N>=80)
        printf(“Above Average\n\n”);
    else if (N>=70)
        printf(“Average\n\n”);
    else if (N>=60)
        printf(“Satisfactory\n\n”);
    else
        printf(“Failure\n\n”);
     
    printf("Student 7's grade: \n");
    scanf("%d",&M);
    if ((M>=90)&&(M<=100))
        printf(“Excellent\n\n”);
    else if (M>=80)
        printf(“Above Average\n\n”);
    else if (M>=70)
        printf(“Average\n\n”);
    else if (M>=60)
        printf(“Satisfactory\n\n”);
    else
        printf(“Failure\n\n”);
     
    printf("Student 8's grade: \n");
    scanf("%d",&A);
    if ((A>=90)&&(A<=100))
        printf(“Excellent\n\n”);
    else if (A>=80)
        printf(“Above Average\n\n”);
    else if (A>=70)
        printf(“Average\n\n”);
    else if (A>=60)
        printf(“Satisfactory\n\n”);
    else
        printf(“Failure\n\n”);
     
    printf("Student 9's grade: \n");
    scanf("%d",&S);
    if ((S>=90)&&(S<=100))
        printf(“Excellent\n\n”);
    else if (S>=80)
        printf(“Above Average\n\n”);
    else if (S>=70)
        printf(“Average\n\n”);
    else if (S>=60)
        printf(“Satisfactory\n\n”);
    else
        printf(“Failure\n\n”);
     
    printf("Student 10's grade: \n");
    scanf("%d",&D);
    if ((D>=90)&&(D<=100))
        printf(“Excellent\n\n”);
    else if (D>=80)
        printf(“Above Average\n\n”);
    else if (D>=70)
        printf(“Average\n\n”);
    else if (D>=60)
        printf(“Satisfactory\n\n”);
    else
        printf(“Failure\n\n”);
     
    printf("Student 11's grade: \n");
    scanf("%d",&F);
    if ((F>=90)&&(F<=100))
        printf(“Excellent\n\n”);
    else if (F>=80)
        printf(“Above Average\n\n”);
    else if (F>=70)
        printf(“Average\n\n”);
    else if (F>=60)
        printf(“Satisfactory\n\n”);
    else
        printf(“Failure\n\n”);
     
    printf("Student 12's grade: \n");
    scanf("%d",&G);
    if ((G>=90)&&(a<=100))
        printf(“Excellent\n\n”);
    else if (G>=80)
        printf(“Above Average\n\n”);
    else if (G>=70)
        printf(“Average\n\n”);
    else if (G>=60)
        printf(“Satisfactory\n\n”);
    else
        printf(“Failure\n\n”);
     
    printf("Student 13's grade: \n");
    scanf("%d",&H);
    if ((H>=90)&&(H<=100))
        printf(“Excellent\n\n”);
    else if (H>=80)
        printf(“Above Average\n\n”);
    else if (H>=70)
        printf(“Average\n\n”);
    else if (H>=60)
        printf(“Satisfactory\n\n”);
    else
        printf(“Failure\n\n”);
     
    printf("Student 14's grade: \n");
    scanf("%d",&J);
    if ((J>=90)&&(J<=100))
        printf(“Excellent\n\n”);
    else if (J>=80)
        printf(“Above Average\n\n”);
    else if (J>=70)
        printf(“Average\n\n”);
    else if (J>=60)
        printf(“Satisfactory\n\n”);
    else
        printf(“Failure\n\n”);
     
    printf("Student 15's grade: \n");
    scanf("%d",&K);
    if ((K>=90)&&(K<=100))
        printf(“Excellent\n\n”);
    else if (K>=80)
        printf(“Above Average\n\n”);
    else if (K>=70)
        printf(“Average\n\n”);
    else if (K>=60)
        printf(“Satisfactory\n\n”);
    else
        printf(“Failure\n\n”);
     
    printf("Student 16's grade: \n");
    scanf("%d",&L);
    if ((L>=90)&&(L<=100))
        printf(“Excellent\n\n”);
    else if (L>=80)
        printf(“Above Average\n\n”);
    else if (L>=70)
        printf(“Average\n\n”);
    else if (L>=60)
        printf(“Satisfactory\n\n”);
    else
        printf(“Failure\n\n”);
     
    printf("Student 17's grade: \n");
    scanf("%d",&Q);
    if ((Q>=90)&&(Q<=100))
        printf(“Excellent\n\n”);
    else if (Q>=80)
        printf(“Above Average\n\n”);
    else if (Q>=70)
        printf(“Average\n\n”);
    else if (Q>=60)
        printf(“Satisfactory\n\n”);
    else
        printf(“Failure\n\n”);
     
    printf("Student 18's grade: \n");
    scanf("%d",&W);
    if ((W>=90)&&(W<=100))
        printf(“Excellent\n\n”);
    else if (W>=80)
        printf(“Above Average\n\n”);
    else if (W>=70)
        printf(“Average\n\n”);
    else if (W>=60)
        printf(“Satisfactory\n\n”);
    else
        printf(“Failure\n\n”);
     
    printf("Student 19's grade: \n");
    scanf("%d",&E);
    if ((E>=90)&&(E<=100))
        printf(“Excellent\n\n”);
    else if (E>=80)
        printf(“Above Average\n\n”);
    else if (E>=70)
        printf(“Average\n\n”);
    else if (E>=60)
        printf(“Satisfactory\n\n”);
    else
        printf(“Failure\n\n”);
     
    printf("Student 20's grade: \n");
    scanf("%d",&R);
    if ((R>=90)&&(R<=100))
        printf(“Excellent\n\n”);
    else if (R>=80)
        printf(“Above Average\n\n”);
    else if (R>=70)
        printf(“Average\n\n”);
    else if (R>=60)
        printf(“Satisfactory\n\n”);
    else
        printf(“Failure\n\n”);
    
    printf("Do you want to repeat [Y\N]?");
       scanf("%c",ans);
       if(ans=='Y'||ans=='y')
          clrscr;
          goto first:
    
    getch();
    return 0;
    }
    Last edited by zenox_ruiz; 01-25-2013 at 10:17 PM.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    By the way, is there a way to make this shorter?
    Use a loop instead of goto.

  3. #3
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    have you learned about arrays? This would be an extremely appropriate scenario for one.

  4. #4
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Well you can use goto to create a loop and then increment a variable so that you can just have something like:
    Code:
    int student = 0;
    ...
    loop:
      printf("Student %d grade: \n", student);
    scanf("%d",&E);
    if ((E>=90)&&(E<=100))
        printf(“Excellent\n\n”);
    else if (E>=80)
        printf(“Above Average\n\n”);
    else if (E>=70)
        printf(“Average\n\n”);
    else if (E>=60)
        printf(“Satisfactory\n\n”);
    else if (E<60)
        printf("F sucker!\n\n");
    if (++student < 20) goto loop;
    That shortens your code by a factor of 20....now the real way to do this however would be to use a for loop. Which apparently you haven't learned yet?
    Code:
    for (int x = 0; x < 20; ++x) {
    ... /* some operations * / ...
    ...
    }

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    Code:
       if(ans=='Y'||ans=='y')
          clrscr;
          goto first:
    The "clrscr;" ends the if stataement, so the only thing that a 'Y' or 'y' does is clear the screeen.

    It should look more like this:

    Code:
       if(ans=='Y'||ans=='y')
       {  clrscr;
          goto first:
       }
    Last edited by megafiddle; 01-25-2013 at 11:11 PM.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    If you notice, you are using different variables for each student. Since you are not storing the grade for any reason
    beyond determining each grade, student by student, you can use the same variable for each student.

    Now if you change the code sections to all use the same variable for the grade, you will notice that the only thing
    that differs in each case is the student # in the input prompt:

    Code:
    printf("Student 1's grade: \n");
    So you need to construct a loop that counts from 1 to 20, and change the input prompt to incorporate the count value
    into the prompt. Then you just need the code for one case inside the loop.
    That will be your inner loop. The outer loop will test for 'Y' and repeat the entire inner loop.
    Hope that makes sense.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I shudder to think what would have resulted if the program was meant to ask for 1000 grades rather than 20.
    This wont compile either, due to the "smart quotes" that have been created by passing this code through a program such as Microsoft Word.

    I think line 22 says it all.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    poisonmist
    Join Date
    Jan 2013
    Posts
    10
    Quote Originally Posted by camel-man View Post
    have you learned about arrays? This would be an extremely appropriate scenario for one.
    No we haven't learned about it yet, but I'm currently searching for tutorials.

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    As your program is originally intended to run, you wouldn't need arrays, as you are not using the grade values
    for anything other than immediate output.

    However, programs of that type often store such information in arrays. You could then for example, input all the grades
    first, and then print out all the grade evaluations later. Arrays are a natural storage type for that type of program.

    I would look up loops and nested loops. That it what you need to shorten or simplify the program.

  10. #10
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by zenox_ruiz View Post
    By the way, is there a way to make this shorter?
    Notice in your code that you are repeating the following over and over:
    Code:
    printf("Student 1's grade: \n");
    scanf("%d",&Z);
    if ((Z>=90)&&(Z<=100))
        printf(“Excellent\n\n”);
    else if (Z>=80)
        printf(“Above Average\n\n”);
    else if (Z>=70)
        printf(“Average\n\n”);
    else if (Z>=60)
        printf(“Satisfactory\n\n”);
    else
        printf(“Failure\n\n”);
        
    printf("Student 2's grade: \n");
    scanf("%d",&Z);
    if ((Z>=90)&&(Z<=100))
        printf(“Excellent\n\n”);
    else if (Z>=80)
        printf(“Above Average\n\n”);
    else if (Z>=70)
        printf(“Average\n\n”);
    else if (Z>=60)
        printf(“Satisfactory\n\n”);
    else
        printf(“Failure\n\n”);
    This is the natural application of a for loop. Which part changes? Only the student number. So replace that with `i'. How high does i go? From 1 to 10. So it is like this

    Code:
    for (int i=1; i <= 10; i++) {
        printf("Student %d's grade: \n", i);
        scanf("%d",&Z);
        if ((Z>=90)&&(Z<=100))
            printf(“Excellent\n\n”);
        else if (Z>=80)
            printf(“Above Average\n\n”);
        else if (Z>=70)
            printf(“Average\n\n”);
        else if (Z>=60)
            printf(“Satisfactory\n\n”);
        else
            printf(“Failure\n\n”);
    }
    A similar idea is used for your "repeating yes/no" question.

    Code:
    int want_repeat=1;
    while (want_repeat != 0) {
       /* ... */
       printf("Do you want to repeat? Press 1 for Yes or 0 for No: ");
       scanf("%d", &want_repeat);
    }

  11. #11
    poisonmist
    Join Date
    Jan 2013
    Posts
    10
    Quote Originally Posted by c99tutorial View Post
    Notice in your code that you are repeating the following over and over:
    Code:
    printf("Student 1's grade: \n");
    scanf("%d",&Z);
    if ((Z>=90)&&(Z<=100))
        printf(“Excellent\n\n”);
    else if (Z>=80)
        printf(“Above Average\n\n”);
    else if (Z>=70)
        printf(“Average\n\n”);
    else if (Z>=60)
        printf(“Satisfactory\n\n”);
    else
        printf(“Failure\n\n”);
        
    printf("Student 2's grade: \n");
    scanf("%d",&Z);
    if ((Z>=90)&&(Z<=100))
        printf(“Excellent\n\n”);
    else if (Z>=80)
        printf(“Above Average\n\n”);
    else if (Z>=70)
        printf(“Average\n\n”);
    else if (Z>=60)
        printf(“Satisfactory\n\n”);
    else
        printf(“Failure\n\n”);
    This is the natural application of a for loop. Which part changes? Only the student number. So replace that with `i'. How high does i go? From 1 to 10. So it is like this

    Code:
    for (int i=1; i <= 10; i++) {
        printf("Student %d's grade: \n", i);
        scanf("%d",&Z);
        if ((Z>=90)&&(Z<=100))
            printf(“Excellent\n\n”);
        else if (Z>=80)
            printf(“Above Average\n\n”);
        else if (Z>=70)
            printf(“Average\n\n”);
        else if (Z>=60)
            printf(“Satisfactory\n\n”);
        else
            printf(“Failure\n\n”);
    }
    A similar idea is used for your "repeating yes/no" question.

    Code:
    int want_repeat=1;
    while (want_repeat != 0) {
       /* ... */
       printf("Do you want to repeat? Press 1 for Yes or 0 for No: ");
       scanf("%d", &want_repeat);
    }

    I finally made the array work, but the [Y/N] doesn't work well. It's always stuck at "Do you want to repeat? [Y/N]"
    Whenever you press any button, the same message appears. Is there anyway to exit the program, besides closing the compiler?

  12. #12
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by zenox_ruiz View Post
    I finally made the array work, but the [Y/N] doesn't work well. It's always stuck at "Do you want to repeat? [Y/N]"
    Whenever you press any button, the same message appears. Is there anyway to exit the program, besides closing the compiler?
    You'd have to post the code to get help on why it's not working.

  13. #13
    poisonmist
    Join Date
    Jan 2013
    Posts
    10
    Quote Originally Posted by c99tutorial View Post
    You'd have to post the code to get help on why it's not working.
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    int main(){
    clrscr;
    int z[20],zenox;
    char x;
    for(zenox=0;zenox<20;zenox++)
    loop:
    	{
    	printf("Enter the grade for student %d:\n",zenox+1);
    	scanf("%d",&z[zenox]);
    
    
    	if((z[zenox]>100)||(z[zenox]<0))
    		{
    		printf("Invalid grade\n");
    		zenox--;
    		}
    	if(z[zenox]>=90)
    		printf("Excellent\n\n");
    	else if(z[zenox]>=80)
    		printf("Above Average\n\n");
    	else if(z[zenox]>=70)
    		printf("Average\n\n");
    	else if(z[zenox]>=60)
    		printf("Satisfactory\n\n");
    	else
    		printf("Failure\n\n");
    	}
    	printf("Do you want to repeat? [Y/N]");
    	scanf("%c",x);
    	if((x=='Y')||(x=='y'))
    		{
    		clrscr();
    		goto loop;
    		}
    	if((x!='Y')||(x!='y'))
    		{
    		exit(0);
    		}
    getch ();
    return 0;
    }

  14. #14
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    I think you have some problems with the code

    clrscr;

    What does this do??

    This line:

    Code:
    scanf("%c",x);
    should probably be

    Code:
    scanf(" %c",&x);
    Also the way you are combining a for loop with a goto statement does not really make sense. Using goto as if it is a loop construct is not correct. Use while... for... etc. Do not use goto unless you are jumping downwards.

    EDIT: Try fixing your indentation. Each new { makes a new level of indentation. This will help see how the loop constructs are operating. The way it is now, it might be a bit confusing to see the program flow.

  15. #15
    poisonmist
    Join Date
    Jan 2013
    Posts
    10
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    int main()
    {
    clrscr();
    int z[20],zenox;
    char x;
    printf("Do you want to continue? [Y/N]");
    scanf("%c",&x);
    while((x=='y')||(x=='Y'))
    for(zenox=0;zenox<20;zenox++)
        {
        printf("Enter the grade for student %d:\n",zenox+1);
        scanf("%d",&z[zenox]);
    
    
        if((z[zenox]>100)||(z[zenox]<0))
            {
            printf("Invalid grade\n");
            zenox--;
            }
        if(z[zenox]>=90)
        printf("Excellent\n\n");
        else if(z[zenox]>=80)
        printf("Above Average\n\n");
        else if(z[zenox]>=70)
        printf("Average\n\n");
        else if(z[zenox]>=60)
        printf("Satisfactory\n\n");
        else
        printf("Failure\n\n");
    
    
        }
    getch ();
    return 0;
    }
    clrscr; was supposed to be clrscr();

    With this code, it keeps on starting at 21 and there are multiple "Do you want to continue? [Y/N]" at the beginning.
    Still finding a way to make it work.

    EDIT: I changed the line of "for(zenox=0;zenox<20;zenox++)". Now it starts at one, but the loop never ends.
    Last edited by zenox_ruiz; 01-26-2013 at 09:06 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem in goto command
    By learning_grc in forum C Programming
    Replies: 29
    Last Post: 10-03-2011, 01:54 PM
  2. Problem with goto
    By Tropicalia in forum C++ Programming
    Replies: 36
    Last Post: 09-30-2006, 07:33 PM
  3. goto help?
    By Zenomori in forum C++ Programming
    Replies: 5
    Last Post: 05-03-2005, 03:54 PM
  4. goto
    By DeepFyre in forum C++ Programming
    Replies: 15
    Last Post: 01-27-2005, 06:57 AM
  5. goto again...
    By volk in forum C++ Programming
    Replies: 4
    Last Post: 04-29-2003, 07:25 PM