Looping and relational (if else) question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jwatson
    New Member
    • Nov 2006
    • 8

    Looping and relational (if else) question

    hey guys need help or some guidances please. Basically its a complicated looping and relational question.

    Write a program to calculate te percentage score of the mark of the students.The program will read in any number of marks in integer, and print out the percentage score of the five categories. You should reject all the invalid marks from the input

    The grade should follow the table below
    mark grade
    <50 fail
    50-59 D
    60-69 C
    70-79 B
    80-89 A
    90-100 Distinction


    sample output:
    Any mark to enter?yes or no? : yes
    please enter the mark : 55
    Any mark to enter?yes or no? : yes
    please enter the mark : 75
    Any mark to enter?yes or no? : yes
    please enter the mark : -5
    Please enter valid mark between 0 and 100. <----- guide/help needed here
    Any mark to enter?yes or no? : yes
    please enter the mark : 85
    Any mark to enter?yes or no? : yes
    please enter the mark : 35
    Any mark to enter?yes or no? : yes
    please enter the mark : 65
    Any mark to enter?yes or no? : yes
    please enter the mark : 72
    Any mark to enter?yes or no? : yes
    please enter the mark : 44
    Any mark to enter?yes or no? : yes
    please enter the mark : 81
    Any mark to enter?yes or no? : no
    Total entry is 8 <------guide/help needed here
    The percentage score in grade Fail is 37.5%
    The percentage score in grade D is 12.5%
    The percentage score in grade C is 12.5%
    The percentage score in grade B is 37.5%
    The percentage score in grade A is 37.5%
    The percentage score in grade Distinction is 0%

    thanks... i will be happy if anyone can give me help on this prog.
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    [QUOTE=jwatson]You should reject all the invalid marks from the input
    Any mark to enter?yes or no? : yes
    please enter the mark : 55
    Any mark to enter?yes or no? : yes
    please enter the mark : -5
    Please enter valid mark between 0 and 100. <----- guide/help needed here
    Any mark to enter?yes or no? : yes
    please enter the mark : 85[QUOTE]

    All right, let's think about this logically.

    You will need two different loops. First, you want to keep executing the process while the user has input. You should have a string variable holding the response from the "Any mark to enter?" question - if the string equals "yes", keep going, right? You'll have to get the first user input before you start the loop, so the code might look like this:

    Code:
    cout << "Any mark to enter?yes or no? : ";
    cin >> decision;
    
    while (decision == "yes" || decision == "YES" || decision == "Yes") { // Checks any possible mispelling of "yes"
       cout << "please enter the mark : ";
       ...
       ...
       ...
       cout << "Any mark to enter?yes or no? : ";
       cin >> decision;  // Gets user input to determine if the loop continues
    }
    Also inside this loop, you can have an integer variable called count while will keep track of every time the loop executes - i.e. every time the user enters a new mark.

    Next, when determining whether the mark entered is legal, you can take two approaches. Either you repeat the statement "please enter the mark : " or a similar prompt while the user enters bad input (stopping when valid input is entered) or, as in the example above, simply skip to the next "Any mark" question.

    For the first solution, you will use a smaller loop similar to the large outer loop, except you will use a do...while loop (since, logically, if the user wants to enter information, they should be able to try at least once). Inside the do...while loop, you can prompt the user for input, input their mark into your variable, and loop while the input is less than 0 or greater than 100. Once the input is valid, this loop will exit, and you will stop asking for input.

    Code:
    do {
       cout << "please enter the mark : ";
       cin >> num;
    } while (num is bad input);
    For the second solution, you prompt the user once for input. If the input is valid, no problems! Continue with calculations. Otherwise, you need to display the warning message ("Please enter valid mark between 0 and 100."), ask the "Any marks to enter?" question, input into decision, and then use the continue statement to stop performing calculations and go directly to the conditional statement at the end of the loop.

    Code:
    cout << "please enter the mark : ";
    cin >> num;
    if (num is bad input) {
       cout << "Please enter valid mark between 0 and 100." << endl;
       cout << "Any mark to enter?yes or no? : ";
       cin >> decision;
       continue;
    } else {
       // Continue with calculations
    }
    Both of these will work, though in slightly different ways. Come back if you need more help.

    Comment

    • jwatson
      New Member
      • Nov 2006
      • 8

      #3
      thanks i for the help..... let me try writing it.

      Comment

      • jwatson
        New Member
        • Nov 2006
        • 8

        #4
        i tried build this for a part that ive done but after i type "yes" in the program, it doesnt execute to the while loop??? strange... and help or error in my prog?

        #include <stdio.h>

        main(){

        int mark,count;
        char decision;

        printf("Any mark to enter?yes or no? : ");
        scanf("%d",&dec ision);

        while (decision == "yes" || decision == "YES" || decision == "Yes")
        {
        printf("please enter the mark : ");
        scanf("%d",&mar k);
        if (mark<=0){
        printf("Please enter valid mark between 0 and 100.");
        }
        else{
        printf("Any mark to enter?yes or no? : ");
        scanf("%d",&dec ision);
        }


        }

        return (0);
        }

        Comment

        • svsandeep
          New Member
          • Nov 2006
          • 15

          #5
          Originally posted by jwatson
          i tried build this for a part that ive done but after i type "yes" in the program, it doesnt execute to the while loop??? strange... and help or error in my prog?

          #include <stdio.h>

          main(){

          int mark,count;
          char decision;

          printf("Any mark to enter?yes or no? : ");
          scanf("%d",&dec ision);

          while (decision == "yes" || decision == "YES" || decision == "Yes")
          {
          printf("please enter the mark : ");
          scanf("%d",&mar k);
          if (mark<=0){
          printf("Please enter valid mark between 0 and 100.");
          }
          else{
          printf("Any mark to enter?yes or no? : ");
          scanf("%d",&dec ision);
          }


          }

          return (0);
          }
          Hi,

          From first look... this is what i found.

          Have a close look at this line in your program...

          scanf("%d",&dec ision);

          you are using %d???(It is used for integers) also "decision" is declared as a character which mean it wont be able to accommodate "YES"!!!!.

          Regards,
          ShaggY@FtF

          Comment

          • Ganon11
            Recognized Expert Specialist
            • Oct 2006
            • 3651

            #6
            Yes, you will have to change decision to a string, or test if character == 'y' or 'Y'

            Comment

            • thefarmer
              New Member
              • Nov 2006
              • 55

              #7
              hi there,

              i think you need a prototype on this, because your function does'nt declare on it, you should put #include <string.h> , #include <iostream.h> on your header


              before your while statement place

              char car[5] ="yes" ; //and the others, coz if not you will see a compiler warning

              do not use %d coz its not an integer

              actually theres a lot of problem in your program, your declaration are lack of supporting functions and prototypes, try to revised it again

              try to simplified your while statement by using this:

              while ( stricmp( decision, "yes") !=0)

              then proceed to others,

              ok,

              regards,

              Comment

              • jwatson
                New Member
                • Nov 2006
                • 8

                #8
                hey guys i still cant get this work i have tried my best. but i still cant get the "yes" choice to work.... and the Total count part of the program. i would appreciated if anyone can give me a complete brief solution of this program.
                Heres what i have done so far... pretty messy but i tried being new to C programming:
                #include <stdio.h>

                main(){

                int mark,count;
                char decision;

                printf("Any mark to enter?yes or no? : ");
                scanf("%c",&dec ision);

                while (decision=='y')
                {
                printf("please enter the mark : ");
                scanf("%d",&mar k);
                if (mark>=0){
                printf("Any mark to enter?yes or no? : ");
                scanf("%d",&dec ision);

                }
                else{
                printf("Please enter valid mark between 0 and 100.");
                break;
                }
                }I am stuck for the rest of the program... HELP PLS


                return (0);
                }

                Comment

                Working...