Thread: Program question---

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    4

    Question Program question---

    This is the only time I will post a question of this sort. I've done some small programs and I'm learning this on my own. I want to be able to disect a grading program. the one in question follows:

    A program that prompts a user to enter a 2 digit Student ID (an integer ie 01) and a grade (a decimal number ie 89.72). After each entry, display the student’s letter grade and prompt user to either continue with input or stop input. Loop until user has no more entries.

    Use the following table to calculate and display each student’s letter grade:
    A 90-100
    B 80-89
    C 70-79
    D 60-69
    F 0-59

    I'm not familiar with looping yet. Could someone please help out? If not I understand completely... Thanks..

  2. #2
    Still A Registered User DISGUISED's Avatar
    Join Date
    Aug 2001
    Posts
    499
    There are several ways to do this. Here is an easy method.

    Code:
    #include <iostream>
    using namespace std; 
    
    int main() 
    { 
       char choice = 'y'; 
       while(tolower(choice) == 'y') 
       { 
          //do stuff
          cout<<"\nContinue?(y/n):"; 
          cin>>choice;
       }
       return 0; 
    }

  3. #3
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317
    Also testing against the table of grades can be easily done with a switch
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    63
    I dont think a switch statement would be suggestable.

    I would rather go with an if statement.
    tudehopet uses Borland Compiler 5.5
    and Visual C++ 6.

  5. #5
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317
    hmm... given that you have 5 possible letter grades wouldn't that mean cascading IFs, making it harder to read?
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    63
    Well, I still don't see how a switch statement would work.

    Heres what I was thinking:

    char grade;
    if (mark > 100 || mark < 0)
    {
    cout << "error" //validation
    return 0; //exit
    }
    else if (mark > 89 && mark < 101)
    grade = 'A';
    else if (mark > 79 && mark < 90)
    grade = 'B';
    else if (mark > 69 && mark < 80
    grade = 'C';
    else if (mark > 59 && mark < 70)
    grade = 'D';
    else
    grade = 'E';

    If you can show me how to use a switch statement for this, please do.
    I just don't see how it can be possible, because if the syntax is like this:
    switch(choice)
    {
    case 1: //do stuff; break;
    default: //do stuff; break;
    }
    how can that validate a grade?
    tudehopet uses Borland Compiler 5.5
    and Visual C++ 6.

  7. #7
    Registered User
    Join Date
    May 2002
    Posts
    4

    Thumbs up Looping...

    Thank You everyone. Will the last one enable a loop? I'm not familiar with looping? Also How would I incorporate a 2 digit student ID?

  8. #8
    ResurgentBarbecue UnclePunker's Avatar
    Join Date
    May 2002
    Posts
    128
    This is my interpretation of what you asked, I am a beginner myself, loops depend on a condition you put in the parantheses, a while loop checks the condition before it enter the loop and executes the statements, in this case it executes the program while the variable yesno is not equal to 'n' or 'N' so when it equals something else it exits the loop, the for loop uses the integer variable count gives it a starting point (0) and an end point (0) and how much to count up by each time the loop goes round in this case ++ which means add 1, within my loop it has -- which takes 1 off the count so the loop will execute again, I hope this helps, I can understand if it doesn't and I apologise if I have confused you further.

    Code:
    #include <iostream.h>
    
    void main()
    {
    	 int id, count;
    	 float mark;
    	 char yesno;
    
    	 while (yesno!='n'&&yesno!='N')
    	 {
    		  cout<<"Please enter student id: ";
    		  cin>>id;
    		  cout<<"\n\n";
    
    		  cout<<"Please enter student mark: ";
    		  for (count=0; count<=0; count++)
    		  {
    				cin>>mark;
    
    				if (mark<=-1||mark>=101)
    				{
    					 cout<<"error, re-enter";
    					 count--;
    				}
    		  }
    
    		  cout<<"\n\n";
    
    		  if (mark>=90&&mark<=100)
    		  {
    				cout<<"Student "<<id<<" got a grade A";
    		  }
    
    		  if (mark>=80&&mark<=89)
    		  {
    				cout<<"Student "<<id<< " got a grade B";
    		  }
    
    		  if (mark>=70&&mark<=79)
    		  {
    				cout<<"Student "<<id<< " got a grade C";
    		  }
    
    		  if (mark>=60&&mark<=69)
    		  {
    				cout<<"Student "<<id<< " got a grade D";
    		  }
    
    		  if (mark>=0&&mark<=59)
    		  {
    				cout<<"Student "<<id<< " got a grade F";
    		  }
    
    		  cout<<"\n\n";
    		  cout<<"Again?: ";
    		  cin>>yesno;
    		  cout<<"\n\n";
    	 }
    	 cout<<"Thankyou Bye";
    }

  9. #9
    Registered User
    Join Date
    May 2002
    Posts
    4

    Looping....

    Man that program is beautiful, I'm loving C++, I'm learning a lot from you guys on the board. Thank you all very much, especially UnclePunker.

  10. #10
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317
    Originally posted by tudehopet
    I just don't see how it can be possible, because if the syntax is like this:
    switch(choice)
    {
    case 1: //do stuff; break;
    default: //do stuff; break;
    }
    how can that validate a grade?
    You are right, it can't.
    It took me a while to reply because to be honest I made it a task to find a way to support my statement. But the fact is that I couldn't.
    Once again I thought in VB terms and didn't realize the case statement can only accept a constant, instead of the expression i'm so used to. Have to get rid of my last 6 years

    Sorry if i mislead you d21998.
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >void main()
    This is very wrong, main returns an int and anything else is undefined. This is better:
    Code:
    #include <iostream>
    #include <cctype>
    using std::cin;
    using std::cout;
    
    int main()
    {
      int id, mark = 0;
      char yesno = 'y';
    
      do {
        do {
          cout<<"Please enter student id: ";
          cin>>id;
          cout<<"Please enter student mark: ";
          cin>>mark;
        } while ( mark < 0 && 100 > mark );
    
        if (mark>=90)
          cout<<"Student "<<id<<" got a grade A\n";
        else if (mark>=80&&mark<=89)
          cout<<"Student "<<id<< " got a grade B\n";
        else if (mark>=70&&mark<=79)
          cout<<"Student "<<id<< " got a grade C\n";
        else if (mark>=60&&mark<=69)
          cout<<"Student "<<id<< " got a grade D\n";
        else if (mark<=59)
          cout<<"Student "<<id<< " got a grade F\n";
    
        cout<<"Again?: ";
        cin>>yesno;
      } while (toupper(yesno)!='N');
      cout<<"Thank you. Bye\n";
      return 0;
    }
    >You are right, it can't.
    A switch can validate ranges, but you don't want to do it. An if..else if series is far far better than this:
    Code:
    switch ( mark ) {
    case 100:
    case 99:
    case 98:
    case 97:
    .
    .
    .
    case 90:
      cout<<"Student "<<id<<" got a grade A\n";
      break;
    .
    .
    .
    }
    -Prelude
    My best code is written with the delete key.

  12. #12
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    Wink A's for all the students

    I hope that every student get an " A"

    Code:
      for (int i= 1; i<= numbers_of_students; i++){
              cout<< "this student got an:eek: :eek: :eek:  A :eek: :eek: :eek: \n";
              cout<< ":D marry X-man:D "<< endl;
      }
    Why not????
    C++
    The best

  13. #13
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317
    Indulgent!
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

  14. #14
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    A final indulgence?:

    Code:
    #include <iostream>
    #include <cctype>
    using std::cin;
    using std::cout;
    
    int main()
    {
      int id, mark = 0;
      char yesno = 'y';
    
      do {
        do {
          cout<<"Please enter student id: ";
          cin>>id;
          cout<<"Please enter student mark: ";
          cin>>mark;
        } while ( mark < 0 && 100 > mark );
    
        if (mark>=90)
          cout<<"Student "<<id<<" got a grade A\n";
        else if (mark>=80&&mark<=89)
          cout<<"Student "<<id<< " got a grade B\n";
        else if (mark>=70&&mark<=79)
          cout<<"Student "<<id<< " got a grade C\n";
        else if (mark>=60&&mark<=69)
          cout<<"Student "<<id<< " got a grade D\n";
        //else if (mark<=59)  // Not necessary to compare (big deal!)
        else
            cout<<"Student "<<id<< " got a grade F\n";
    
        cout<<"Again?: ";
        cin>>yesno;
      } while (toupper(yesno)!='N');
      cout<<"Thank you. Bye\n";
      return 0;
    }
    Very cool, folks!
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question regarding a this C program
    By cnb in forum C Programming
    Replies: 10
    Last Post: 10-11-2008, 04:43 AM
  2. newb question: probs with this program
    By ajguerrero in forum C Programming
    Replies: 5
    Last Post: 04-19-2006, 08:04 AM
  3. Random Question Assign Program
    By mikeprogram in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2005, 10:04 PM
  4. Question type program for beginners
    By Kirdra in forum C++ Programming
    Replies: 7
    Last Post: 09-15-2002, 05:10 AM
  5. Replies: 8
    Last Post: 03-26-2002, 07:55 AM