Menu

Help for homework!!!

#10lincoln
2007-11-20
2012-09-26
  • #10lincoln

    #10lincoln - 2007-11-20

    Please don't write strange things.I'm new in C programming.(C standard)And so far i learned printf,scanf,if statement,switch,while,do/while,for loops.I don't know functions yet.Please explain clearly.I delivered my homework.I want to know that what I did wrong.And I want to understand more.

    Question :

    Write a program that enters university numbers and the grades (int) of an unknown number of students.

    The program should display a menu as shown below and the user will enter a choice to perform one of the options (use switch).

    1. Display the letter grade of the student.
    2. Display the average of the student grades entered so far.
    3. Display the highest grade entered so far and the student’s number.
    4. Display the lowest grade entered so far and the student’s number.
    5. Enter the university number and grade of a new student.
    6. Quit

    Your program should stop when the user chooses Quit. After that, the program should display the number of students entered, and who got the highest grade and the lowest grade.

    Put some reasonable checks in your program. For example, before a user enters the first grade and university number, all other choices are not valid. Grades must be in a range, between 0 and 100 therefore, the program should check that all the grades entered are in this range.

    .cpp File :


    include<stdio.h>

    int main()
    {
    int x,unumber,grade,counter,highest=0,lowest,sum=0,largestID,lowestID;
    float av;
    grade=highest=lowest;

       printf(&quot;1.  Display the letter grade of the student.\n&quot;);
       printf(&quot;2.  Display the average of the student grades entered so far.\n&quot;);
       printf(&quot;3.  Display the highest grade entered so far and the student number.\n&quot;);
       printf(&quot;4.  Display the lowest grade entered so far and the student number.\n&quot;);
       printf(&quot;5.  Enter the university number and grade of a new student.\n&quot;);
       printf(&quot;6.  Quit\n&quot;);
       printf(&quot;\n\n&quot;);
    
       scanf(&quot;%d&quot;,&amp;x);
    
       if(x==5)
    
               switch(x)
    {
    
               case 5 :
    
               printf(&quot;First enter your university number.\n&quot;);
               scanf(&quot;%d&quot;,&amp;unumber);
               printf(&quot;Enter your grade.\n&quot;);
               scanf(&quot;%d&quot;,&amp;grade);
               sum=sum+grade;
    
               case 1 :
    
                       if(grade&lt;=100 &amp;&amp; grade&gt;=90)
                           printf(&quot;A\n&quot;);
    
                       if(grade&lt;90 &amp;&amp; grade&gt;=80)
                           printf(&quot;B\n&quot;);
    
                       if(grade&lt;80 &amp;&amp; grade&gt;=70)
                           printf(&quot;C\n&quot;);
    
                       if(grade&lt;70 &amp;&amp; grade&gt;=60)
                           printf(&quot;D\n&quot;);
    
                       if(grade&lt;60 &amp;&amp; grade&gt;=0)
                           printf(&quot;F\n&quot;);
    
       }
               for(counter=1;;)
    
               {
    
       printf(&quot;1.  Display the letter grade of the student.\n&quot;);
       printf(&quot;2.  Display the average of the student grades entered so far.\n&quot;);
       printf(&quot;3.  Display the highest grade entered so far and the student number.\n&quot;);
       printf(&quot;4.  Display the lowest grade entered so far and the student number.\n&quot;);
       printf(&quot;5.  Enter the university number and grade of a new student.\n&quot;);
       printf(&quot;6.  Quit\n&quot;);
       printf(&quot;\n\n&quot;);
    
       scanf(&quot;%d&quot;,&amp;x);
    
               switch(x)
    {
    
               case 5 :
    
               printf(&quot;First enter your university number.\n&quot;);
               scanf(&quot;%d&quot;,&amp;unumber);
               printf(&quot;Enter your grade.\n&quot;);
               scanf(&quot;%d&quot;,&amp;highest);
               counter++;
    
               case 1 :
                   {
    
                       if(highest&lt;=100 &amp;&amp; highest&gt;=90)
                           printf(&quot;A\n&quot;);
    
                       if(highest&lt;90 &amp;&amp; highest&gt;=80)
                           printf(&quot;B\n&quot;);
    
                       if(highest&lt;80 &amp;&amp; highest&gt;=70)
                           printf(&quot;C\n&quot;);
    
                       if(highest&lt;70 &amp;&amp; highest&gt;=60)
                           printf(&quot;D\n&quot;);
    
                       if(highest&lt;60 &amp;&amp; highest&gt;=0)
                           printf(&quot;F\n&quot;);
    
                   }
    
               case 2 :
                   sum=sum+highest;
                   av=(float)sum/counter;
    
                   printf(&quot;%f is the average grade.\n&quot;,av);
    
               case 3 :
    
                       if(highest&gt;grade)
                       {
                           grade=highest;
                           largestID=unumber;
    
                           printf(&quot;Highest grade is %d.\n&quot;,grade);
                           printf(&quot;Student number : %d\n&quot;,largestID);
    
                       }
    
               }
    
      if(x==6)
      {
                   printf(&quot;%d student(s) entered.\n&quot;,counter);
                   printf(&quot;Highest grade is %d.\n&quot;,grade);
      }
          break;
    
               }
    
       return 0;
    

    }

    I couldn't find the lowest grade.I couldn't find the student's number in both 3. and 4. And everytime i input 2 average grade is changing even i don't enter a new grade.And i don't want the letter grade to be shown,but it's shown in the program.And the last problem how can i quit the program using switch when i input 6?

     
    • #10lincoln

      #10lincoln - 2007-11-22

      OK.No need to have help.I completed my homework properly.Thanks a lot Osito.Your assistance was very useful and helpful.

       
    • #10lincoln

      #10lincoln - 2007-11-20

      My target is not to make you do my homework.

       
    • Osito

      Osito - 2007-11-20

      A couple of things I noticed...

      This doesn't make sense:

      if(x==5)
      switch(x)
      {
      case 5 :

      There's no point in having a switch statement inside of that if statement. You can only get to the switch(x) if x is 5, so the other cases can't happen anyway.

      Also, I don't know if this is what you wanted or not, but when you list the cases you need a break; at the end of each unless you want the code in the next case to execute as well. So you should have

      case 1:
      ...
      break;
      case 2:
      ...
      break;

      To exit on input==6, you could try {return 0;} although I'm not sure that works properly. You could also put the whole infinite loop in a while statement. Start with int quit = 0; while(quit!=1) { all your code goes here } and then when input==6 set quit to 1 so next time through the loop it quits.

       
    • #10lincoln

      #10lincoln - 2007-11-20

      Should I use break; after each case?

       
      • Osito

        Osito - 2007-11-20

        That depends on what you want your code to do. If you put a break after the code within each case, you will execute only that code. For any case that doesn't have a break at the end, you will continue to execute code until you reach the next break or the end of the switch. Both ways can be useful. If you just want to use it like an if then else else else else statement, use a break at the end of each case. But say you had two cases, "enter name" and "enter grade", and you wanted the user to be able to enter a grade at any time but force them to enter at least one grade after entering a name. You could have code like this:

        case (name_entry_id): {code goes here}
        case (grade_entry_id): {code goes here} break;

        So that when grade_entry_id is selected it only does the grade entry code, but when name_entry_id is selected it does both name and grade because it keeps going until the break at the end of the grade section.

         
    • #10lincoln

      #10lincoln - 2007-11-20

      Should i use brackets for if statements that are in case 1 and case 3?

       
      • Osito

        Osito - 2007-11-20

        It never hurts to use brackets everywhere. I usually put brackets around my statements following an if, even if it's a single statement. That way if I add a second statement to the if, it still works. If you forget to add the brackets with the second statement, you'll have problems.

         
    • #10lincoln

      #10lincoln - 2007-11-20

      i put if(x==5) because of that :

      before a user enters the first grade and university number, all other choices are not valid.

       
      • Osito

        Osito - 2007-11-20

        That's fine, but there's no point in having a switch(x) inside the if(x==5) because x will always be 5 inside that if statement.

         
    • #10lincoln

      #10lincoln - 2007-11-20

      yes yo're right.

       
    • #10lincoln

      #10lincoln - 2007-11-20

      average grade is always changing even i don't input a grade.I input 2 grades 80 and 86.Then when i pressed 2 button to display average of 80 and 86 i got 83.It's correct but when i pressed 2 button again average grade was 126 and when i press 2 average was 169 and like that.How can I fix this problem?

       
      • Osito

        Osito - 2007-11-20

        The problem is that every time you display the average, you are doing additional calculations:

        case 2 :
        sum=sum+highest;
        av=(float)sum/counter;

        If case 2 is used to display the average, it should do only that, and not do any calculations. Each time the user enters a grade, it should increment the counter and add the new grade to the sum. Then in case 2 you can simply print out the quantity (sum/counter). You might need to check that counter is >0 first if you haven't already. That way if you attempt to display the average and no grades have been entered, you won't get a divide by zero error.

         
    • #10lincoln

      #10lincoln - 2007-11-20

      thanks a lot.I borrowed your valuable time very much.I will ask some other questions tomorrow.

       
    • Kurgusov

      Kurgusov - 2007-11-21

      >>> int x,unumber,grade,counter,highest=0,lowest,sum=0,largestID,lowestID;
      float av;
      grade=highest=lowest; <<<

      Am I wrong here to think that grade is being assigned a garbage value!?

       
    • #10lincoln

      #10lincoln - 2007-11-21

      I edited .cpp file.Last case is below.I have two problems :

      1)In 3. case i can't display the highest grade with correct student number.I think I did everythink correct but something is wrong.
      2)How can I display lowest grade with student number?Should I use another variable?Can you explain it?


      include<stdio.h>

      int main()
      {
      int x,unumber,grade,counter,highest=0,lowest,sum=0,largestID,lowestID,quit=0;
      float av;

         printf(&quot;1.  Display the letter grade of the student.\n&quot;);
         printf(&quot;2.  Display the average of the student grades entered so far.\n&quot;);
         printf(&quot;3.  Display the highest grade entered so far and the student number.\n&quot;);
         printf(&quot;4.  Display the lowest grade entered so far and the student number.\n&quot;);
         printf(&quot;5.  Enter the university number and grade of a new student.\n&quot;);
         printf(&quot;6.  Quit\n&quot;);
         printf(&quot;\n\n&quot;);
      
         scanf(&quot;%d&quot;,&amp;x);
      
                 switch(x)
      {
      
                 case 5 :
      
                 printf(&quot;First enter your university number.\n&quot;);
                 scanf(&quot;%d&quot;,&amp;unumber);
                 printf(&quot;Enter your grade.\n&quot;);
                 scanf(&quot;%d&quot;,&amp;grade);
                 if(grade&lt;=100 &amp;&amp; grade&gt;=0)
                 sum=sum+grade;
      
         }
                 for(counter=1;;)
                 {
         while(quit!=1){
      
         printf(&quot;1.  Display the letter grade of the student.\n&quot;);
         printf(&quot;2.  Display the average of the student grades entered so far.\n&quot;);
         printf(&quot;3.  Display the highest grade entered so far and the student number.\n&quot;);
         printf(&quot;4.  Display the lowest grade entered so far and the student number.\n&quot;);
         printf(&quot;5.  Enter the university number and grade of a new student.\n&quot;);
         printf(&quot;6.  Quit\n&quot;);
         printf(&quot;\n\n&quot;);
      
         scanf(&quot;%d&quot;,&amp;x);
      
                 switch(x)
      {
      
                 case 5 :
      
                 printf(&quot;First enter your university number.\n&quot;);
                 scanf(&quot;%d&quot;,&amp;unumber);
                 printf(&quot;Enter your grade.\n&quot;);
                 scanf(&quot;%d&quot;,&amp;highest);
      
                 if(highest&lt;=100 &amp;&amp; highest&gt;=0)
                 {
                 counter++;
                 sum=sum+highest;
                 }
                 break;
      
                 case 1 :
      
                     if(counter==1)
                     {
                         if(grade&lt;=100 &amp;&amp; grade&gt;=90)
                             printf(&quot;A\n&quot;);
      
                         if(grade&lt;90 &amp;&amp; grade&gt;=80)
                             printf(&quot;B\n&quot;);
      
                         if(grade&lt;80 &amp;&amp; grade&gt;=70)
                             printf(&quot;C\n&quot;);
      
                         if(grade&lt;70 &amp;&amp; grade&gt;=60)
                             printf(&quot;D\n&quot;);
      
                         if(grade&lt;60 &amp;&amp; grade&gt;=0)
                             printf(&quot;F\n&quot;);
                     }
                     else
                     {
                         if(highest&lt;=100 &amp;&amp; highest&gt;=90)
                             printf(&quot;A\n&quot;);
      
                         if(highest&lt;90 &amp;&amp; highest&gt;=80)
                             printf(&quot;B\n&quot;);
      
                         if(highest&lt;80 &amp;&amp; highest&gt;=70)
                             printf(&quot;C\n&quot;);
      
                         if(highest&lt;70 &amp;&amp; highest&gt;=60)
                             printf(&quot;D\n&quot;);
      
                         if(highest&lt;60 &amp;&amp; highest&gt;=0)
                             printf(&quot;F\n&quot;);
                     }
                         break;
      
                 case 2 :
      
                     av=(float)sum/counter;
      
                     printf(&quot;%f is the average grade.\n&quot;,av);
      
                     break;
      
                 case 3 :
      
                         if(highest&gt;grade)
      
                             grade=highest;
                             largestID=unumber;
      
                             printf(&quot;Highest grade is %d.\n&quot;,grade);
                             printf(&quot;Student number : %d\n&quot;,largestID);
      
                         break;
      
                 case 6 :
                     quit=quit+1;
                     printf(&quot;%d student(s) entered.\n&quot;,counter);
                     printf(&quot;Highest grade is %d.\n&quot;,grade);
      
                 }
      
         }
      
                 }
      
         return 0;
      

      }

       
    • #10lincoln

      #10lincoln - 2007-11-22

      Come on.I'm waiting for your messages.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.