Thread: goto and Yes or No problem

  1. #16
    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);
    for(zenox=0;zenox<20;zenox++)
    if((x=='y')||(x=='Y'))
    	{
    	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");
    	}
    else
    exit (0);
    getch ();
    return 0;
    }
    Now the loop ends at 20. I made [Y/N] work. But I just need to let "Do you want to continue" reappear after the loop reaches 20.

  2. #17
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by zenox_ruiz View Post
    Now the loop ends at 20. I made [Y/N] work. But I just need to let "Do you want to continue" reappear after the loop reaches 20.
    It seems like you're mixing up the Y/N part in with your "getting the student grades" part

    1. Make the program work one time without any kind of Y/N question.
    2. Wrap the code with the following:

    Code:
    char x = 'Y';
    while (x == 'Y') {
        /* ... */
        printf("Do you want to continue? [Y/N]");
        scanf(" %c",&x);
    }
    The part ... should be only the code that does the student grades part. Also the space in " %c" is required.

  3. #18
    poisonmist
    Join Date
    Jan 2013
    Posts
    10

    Wink

    It worked without the Y/N
    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++)
         {
         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");
         }
    exit (0);
    getch ();
    return 0;
    }

    Then by wrapping it, it worked!
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    int main()
    {
    clrscr();
    char x ='Y';
    while (x=='Y') {
    	{
    	int z[20],zenox;
    	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");
    		}
    	}
    	printf("Do you want to continue? [Y/N]");
    	scanf(" %c",&x);
    }
    exit(0);
    getch ();
    return 0;
    }
    But is there anyway that it could use either Y or y? I tried it this way
    Code:
    char x ='Y'||'y';
    while ((x=='Y')||(x=='y')) {
    But it didn't seem to work.
    Last edited by zenox_ruiz; 01-26-2013 at 10:35 AM.

  4. #19
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by zenox_ruiz View Post
    Code:
    char x ='Y'||'y';
    while ((x=='Y')||(x=='y')) {
    But it didn't seem to work.
    Your while condition is correct, but the assignment

    char x = 'Y' || 'y';

    is like saying this:

    char x = (bool)('Y' || 'y');

    It means the expression will evaluate to true or false, so treating x as a character from this point on doesn't work as expected.

  5. #20
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Please... any time you feel compelled to initialise the while loop variable to the exact value that ensures the loop body will always be entered the first time, just use the do..while loop that was meant for this purpose.
    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"

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