Friday, July 31, 2009

Question with turbo C.. Regarding if else statements.?

Ok so this is the problem.. "Make a program that will validate the prelim, midterm, pre-final and final grade of a student. Grades must not accept grades that are greater than 100 and less than 50. Compute for the semestral grade/average. Get the 20% of prelim, midterm, prefinal and 40% of final grade. If sem.grade is greater than or equal to 75 display PASSED otherwise FAILED." So that was the problem and this is a part of what i did:





printf("What is the prelim grade?: \n");


scanf("%d", %26amp;prel);


if(prel%26gt;100 || prel%26lt;50)


printf("Prelim Grade is not valid!!! \n");


else


printf("What is the midterm grade?: \n");


scanf("%d", %26amp;midterm);





Now my question is if the prelim grade is not valid and already displayed the GRADE IS NOT VALID, how can i go back to the source once i push enter once again? And what should i do to keep it from asking the grades until it receives a valid date which is not greater than 100 and less than 50...?? please help, i am almost out of my wits!! thanks.

Question with turbo C.. Regarding if else statements.?
First, when you are giving the user a choice that he must stay within a certain number, you have to lock him in with a while loop. Not an if statement.





scanf("%d", %26amp;prel);





while(prel%26gt;100 || prel%26lt;50)


{


printf("Prelim Grade is not valid!!! \n");


printf("Enter grade between 50 and 100: \n");


scanf("%d", %26amp;prel);


}





Do the same for the other.


Once you get VALID numbers, go on with the program to determine the rest.


Do one thing at a time, not all of it. Or you get errors and confusion.
Reply:use the goto statement





hell:


{


printf("What is the prelim grade?: \n");


scanf("%d", %26amp;prel);


if(prel%26gt;100 || prel%26lt;50)


{


printf("Prelim Grade is not valid!!! \n");


goto hell();


}


else


printf("What is the midterm grade?: \n");


scanf("%d", %26amp;midterm);


}


No comments:

Post a Comment