Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C
/* Program to accept marks and assign a class */

#include<stdio.h>
#include<conio.h>
void main()
{
 int m;
 char ch;
 clrscr();
 printf("ENTER MARKS (0-100) : ");
 scanf("%d",&m);

 s:
 if(m<40)
	printf("\nResult :\n %d is FAIL",m);
 else if(m>=40 && m<50)
	printf("\nResult :\n %d is THIRD CLASS",m);
 else if(m>=50 && m<60)
	printf("\nResult :\n %d is SECOND CLASS",m);
 else if(m>=60 && m<70)
	printf("\nResult :\n %d is FIRST CLASS",m);
 else if(m>=70 && m<=100)
	printf("\nResult :\n %d is DISTINCTION",m);
 else
	printf("\n %d is Invalid Marks",m);

	printf("\n Do you want to check another mark : Type (Y/N)");
	printf("\n Enter choice : ",ch);
	scanf("%c",&ch);
	if(ch == 'Y' || 'y')
		goto s;
	else if(ch == 'N' || 'n')
		goto end;
	else
		printf("\nInvalid selection",ch);

end:
getch();
 }


This is the code. The programs works fine except the last part. It shows the 'grade' and then asks for another entry and then same thing repeats. I want the program to ask for another entry with a choice Y or N. Please help me with this. I am new to this programming..
Posted
Updated 13-Sep-13 3:38am
v2
Comments
CHill60 13-Sep-13 9:50am    
Your s: label is in the wrong place. "Wrong place" meaning in your program in the first place, but as it's there you probably wanted it to be above printf("ENTER MARKS (0-100) : ");

Your if statements are wrong, replace
C
if(ch == 'Y' || 'y')

with
C
if(ch == 'Y' || ch == 'y')


'y' will evaluate as true so the condition is always true.

And the same goes for the if-statements checking for no.

Hope this helps,
Fredrik
 
Share this answer
 
v2
Comments
nv3 13-Sep-13 11:09am    
Good catch. 5.
Fredrik Bornander 13-Sep-13 12:06pm    
Thank you, good sir.
"if(ch == 'Y' || 'y')"

'y' is always true. :D


You probably want:

if(ch == 'Y' || ch == 'y')


Please eliminate the gotos, I recommend a do/while.


"
else if(ch == 'N' || 'n')
goto end;
else
printf("\nInvalid selection",ch);

"

That's silly.
 
Share this answer
 
Comments
OriginalGriff 13-Sep-13 9:53am    
I missed that - I just saw the gotos and my mind rebelled, I suspect... :laugh:
First things first: Forget you ever saw the keyword "goto" or how to create labels. Do not use them again for at least four years of "C" coding - if you need them, you have designed your code wrong. After four years, you should know enough to understand where you should use them, but you will also know enough to probably not need to either...

If you had done this using loop constructs it would be obvious what your problem is:
C++
int main(void)
    {
    int m;
    char ch;
    bool done = false;
    clrscr();
    printf("ENTER MARKS (0-100) : ");
    scanf("%d",&m);

    while (!done)
        {
        if(m<40)
            printf("\nResult :\n %d is FAIL",m);
        else if(m>=40 && m<50)
            printf("\nResult :\n %d is THIRD CLASS",m);
        else if(m>=50 && m<60)
            printf("\nResult :\n %d is SECOND CLASS",m);
        else if(m>=60 && m<70)
            printf("\nResult :\n %d is FIRST CLASS",m);
        else if(m>=70 && m<=100)
            printf("\nResult :\n %d is DISTINCTION",m);
        else
            printf("\n %d is Invalid Marks",m);

        printf("\n Do you want to check another mark : Type (Y/N)");
        printf("\n Enter choice : ",ch);
        scanf("%c",&ch);
        if(ch == 'Y' || ch == 'y')
            done = false;
        else if(ch == 'N' || ch == 'n')
            done = true;
        else
            printf("\nInvalid selection",ch);
        }
    return 0;
    }

Now, move the loop construct so that the prompt for and input of the new value are within the loop...

[edit]Missed the lack of conditional testing :doh: - OriginalGriff[/edit]
 
Share this answer
 
v3
Comments
PIEBALDconsult 13-Sep-13 9:59am    
And what is it you're supposed to do with an Invalid selection?
OriginalGriff 13-Sep-13 10:10am    
Ignore it and do what you did last time? :InnocentWhistleSmiley:
nv3 13-Sep-13 11:08am    
Nice answer. I hope OP follows your four-year rule.
H.Brydon 14-Sep-13 21:10pm    
+5 although I'd say 10 years.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900