CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Apr 2010
    Posts
    9

    Unhappy I'm a noob, HELP!

    So I'm writing an exercise/check-for-comprehension program. I have to write a program that asks, "What letter grade do you deserve?" Then, the user replies by entering in a letter (A,B, or C; D and F are left out because of the gap between the letters, and this is a novice program, so assume the user types A, B, or C). The program then says,"This is the grade you are going to get:" and displays one letter grade lower than the user entered. In other words, if I enter an A, I get a B, and if I enter a B, I get a C, and finally, if put C, I get a D.
    I'm not allowed to use the IF and ELSE techniques. I'm guessing, by the nature of this program, I have to use pointer arithmetic methods.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: I'm a noob, HELP!

    Well... although strictly speaking it is not portable, in practice, you will be using an ASCII based character set, so the characters 'A', 'B', 'C' and 'D' are in sequence such that 'A' + 1 == 'B', 'B' + 1 == 'C' and 'C' + 1 == 'D'. You can make use of this to compute the answer.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: I'm a noob, HELP!

    You can't use if? What about use a switch?

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: I'm a noob, HELP!

    What if you guess D?

  5. #5
    Join Date
    Aug 2008
    Posts
    902

    Re: I'm a noob, HELP!

    Quote Originally Posted by ZainAsad View Post
    So I'm writing an exercise/check-for-comprehension program. I have to write a program that asks, "What letter grade do you deserve?" Then, the user replies by entering in a letter (A,B, or C; D and F are left out because of the gap between the letters, and this is a novice program, so assume the user types A, B, or C). The program then says,"This is the grade you are going to get:" and displays one letter grade lower than the user entered. In other words, if I enter an A, I get a B, and if I enter a B, I get a C, and finally, if put C, I get a D.
    I'm not allowed to use the IF and ELSE techniques. I'm guessing, by the nature of this program, I have to use pointer arithmetic methods.
    Code:
    char grade = cin.get();
    cout << ++grade;
    ??? That was pretty easy.

  6. #6
    Join Date
    Feb 2010
    Posts
    28

    Re: I'm a noob, HELP!

    This might help

    Code:
    #include <iostream>
    #include <string>
    #include <cmath>
    #include <algorithm>
    using namespace std;
    
    int main()
    {
     char letterGrade;
     
     
     cout <<"What letter grade do you deserve?   ";
     cin  >> letterGrade;
     
     letterGrade = toupper(letterGrade);
       // changes the letter if it is lower case to uppercase
     
     while( int(letterGrade) < 65 || int(letterGrade) > 70) // (A-F) from 65-70
      {
        cout <<"You did not enter a valid choice." << endl;
    	cout <<"Enter a letter from A-F :";
    	cin  >> letterGrade;
    	
    	letterGrade = toupper(letterGrade);
      }
     
     //subract one letter grade
     
     switch(letterGrade)
     {
     case 'A':
     
       cout <<"The grade you deserve is a B!" << endl;
       break;
     case 'B':
       
       cout <<"The grade you deserve is a C!" << endl;
       break;
     case 'C':
     
       cout <<"The grade you deserve is a D!" << endl;
       break; 
     case 'D':
     
       cout <<"The grade you deserve is an F!" << endl;
       break;
     case 'F':
       break;
       cout <<"The grade you deserve is an F!" << endl;
     }
    
     return 0;
    }
    You can substitute the pointer arithmetic what the user above me has written
    Last edited by yellowMonkeyxx; April 18th, 2010 at 04:40 PM.

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: I'm a noob, HELP!

    Pointer arithmetic is hardly required. Simply realizing that chars are basically integers is enough.

  8. #8
    Join Date
    Apr 2010
    Posts
    9

    Re: I'm a noob, HELP!

    Oh my God, I feel stupid as hell now.

  9. #9
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: I'm a noob, HELP!

    Quote Originally Posted by yellowMonkeyxx View Post
    This might help

    You can substitute the pointer arithmetic what the user above me has written
    The user above you didn't use pointer arithmetic, and please don't do somebody's homework for them.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured