Help with home work PLEASE......

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sprint45
    New Member
    • Oct 2006
    • 7

    Help with home work PLEASE......

    Have the following data...

    A 450 - 500 points
    B 400 - 449 points
    C 350 - 399 points
    D 300 - 349 points
    F 0 - 299 points

    were the letters a,b,c,d,f are the grades needed to be output.

    Must also be a paraellel array

    I wrote the following using the text, but all I get is F for the letter grade. Any insight would help

    Thanks in advance

    Code:
    #include <iostream>
    #include <string>
    #include <algorithm>
    
    using std::cout;
    using std::cin;
    using std::endl;
    using std::string;
    
    int main()
    {
    //declare variables and arrays
    string searchForTotalPoints = "";
    int minPoints[5] = {0, 300, 350, 400, 450};
    string grades[5] = {"F", "D", "C", "B", "A"};
    
    // Get Grade to search for
    cout << "Enter Total Points(Z to Exit): ";
    getline(cin, searchForTotalPoints);
    	
    while (searchForTotalPoints != "X")
    {
    	
    	//locate position of the totalpoints in the minPoints array
    	int y = 0;    
    
    	//keeps track of array subscripts
    	while (y < 5 && grades[y] <= searchForTotalPoints) 
    		y = y + 1;
    		//end while
    
    // if totalPoints found, display grade from grades array
    // otherwise display error message
    	if (y < 5)
    		cout << "Total Points: " << grades[y] << endl;
    	else
    		cout << "Total Points are not valid." << endl;
    	//end if
    
    	//Get totalPoints to search for
    	cout << "Enter Total Points (Z to exit): ";
    	getline(cin, searchForTotalPoints);
    }  //end while
    
    return 0;
    } //end of main function
    Last edited by Banfa; Oct 3 '06, 10:13 PM. Reason: Added [code] ... [/code] tags
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    The problem is this loop

    Code:
    	//locate position of the totalpoints in the minPoints array
    	int y = 0;    
    
    	//keeps track of array subscripts
    	while (y < 5 && grades[y] <= searchForTotalPoints) 
    		y = y + 1;
    		//end while
    You are not using the minPoints array so it will never calculate correctly. Additionally because you are starting at the minimum value and working your way up. minPoints[0] is 0 and this is <= any positive number.

    Use of a symbolic debugger or a large quantity of printf statements should make it clear what you program is doing and why it is wrong.

    Comment

    • Sprint45
      New Member
      • Oct 2006
      • 7

      #3
      So Banfa or anyone else, are you saying i need to change the grades[] to minpoints and invert the arrays from greatest value to least value?

      thanks
      mike

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Yes I think that would probably work, but like I say try it but put in plenty of printf statements so that you can tell exactly what is going on.

        Comment

        • Sprint45
          New Member
          • Oct 2006
          • 7

          #5
          Ok Banfa,

          I feel really stupid now...First, my text mentions nothing about Printf statements so i have no idea what these are..sorry

          2. Switched grades[0] to minPoints[0] and recieved an error so changed it back.

          3. switched lowest to highest to highest to lowest and no all total point are a grade of A

          I think what i'm missing is a way to tell the program which part of the array (1, 2, 3, 4 or 5) tio get the answer from when TotalPoints is inputted but were and what is the statement

          Mike

          Comment

          • Brittany Bragdon
            New Member
            • Oct 2006
            • 1

            #6
            Can some one please help me figure how how to had a counter to my program.
            Here is my program down below and i dont know how to make it so the game will kepp tack of who one each game then total it all up at the very end. All the ++ i thought would do it. but nope, so now i'm stuck.



            Code:
             //Assig3.cpp 
            //Brittany Bragdon
            //Section 101
            //Assignment 3
            //A game of rock, paper, scissors
             
            #include <iostream>
            #include <string>
            using namespace std;
            int main ()
            {
            char player, player_one, player_two, answer;
            string player_one_name, player_two_name;
             
            cout << "Here is how to play!\n"; //Directions to the game
            cout << "Player one please enter P for paper, R for rock, and S scissors. \n";
            cout << "Then press return.\n";
            cout << "Player two please do the same thing with out looking at player ones input.\n";
            cout << "At the end of each round the game will tell you who won \n";
            cout << "Then you will be asked if you want to play again. \n";
            cout << "Please enter Y for yes and N for no. \n";
            cout << "Thank you. \n";
            cout << "Now lets begin the game of Rock, Paper Scissors.\n";
             
            cout << endl; //all the endl statements are to make the game look more clean when playing the game
             
            cout << "Player one please enter your name, then press return.\n";
            cin >> player_one_name;
             
            cout << endl;
             
            cout << "Player two please enter your name, then press return.\n";
            cin >> player_two_name;
             
            do
            {
            cout << endl;
             
            cout << player_one_name << " please enter R, P, or S then press return. \n";
            cin >> player_one;
             
            cout << endl;
             
            cout << player_two_name << " please enter R, P, or S then press return. \n";
            cin >> player_two;
             
            if((player_one == 'r' || player_one == 'R') && (player_two == 'p' || player_two == 'P'))
            cout << player_two_name << " Wins!! Paper covers Rock \n";
            cout << player_two_name << ++ << endl;
             
            else if (( player_one == 'p' || player_one == 'P') && (player_two == 'r' || player_two == 'R'))
            cout << player_one_name << " Wins!! Paper covers Rock \n";
            cout << player_one_name << ++ << endl;
             
            else if (( player_one == 'p' || player_one == 'P' ) && (player_two == 's' || player_two == 'S'))
            cout << player_two_name << " Wins!! Scissors cut Paper\n";
            cout << player_two_name << ++ << endl;
             
            else if (( player_one == 's' || player_one == 'S') && (player_two == 'p' || player_two == 'P'))
            cout << player_one_name << " Wins!! Scissors cut Paper \n";
            cout << player_one_mane << ++ << endl;
             
            else if (( player_one == 's' || player_one == 'S') && ( player_two == 'r' || player_two == 'R'))
            cout << player_two_name << " Wins!! Rock breaks Scissors \n";
            cout << player_two_name << ++ << endl;
             
            else if (( player_one == 'r' || player_one == 'R') && (player_two == 's' || player_two == 'S'))
            cout << player_one_name << " Wins!! Rock breaks Scissors \n";
            cout << player_one_name << ++ << endl;
             
            else if (( player_one == 's' || player_one == 'S') && (player_two == 's' || player_two == 'S'))
            cout << player_one_name << " and " << player_two_name << " Tie Nobody Wins \n";
             
             
            else if (( player_one == 'r' || player_one == 'R' ) && (player_two == 'r' || player_two == 'R'))
            cout << player_one_name << " and " << player_two_name << " Tie Nobody Wins \n";
             
             
            else if (( player_one == 'p' || player_one == 'P') && (player_two == 'p' || player_two == 'P'))
            cout << player_one_name << " and " << player_two_name << " Tie Nobody Wins \n";
             
             
            else
            cout << " Not a valid input \n";
             
            cout << endl;
             
            cout << Player_one_name << "Won " << << " Times" << endl
            cout << Player_two_name << "Won " << << " Times" << endl
             
            cout << " Would you like to play again?\n";
            cin >> answer;
             
            }while(answer == 'y' || answer == 'Y');
             
             
            return 0;
            }
            Last edited by Niheel; Oct 4 '06, 05:23 PM.

            Comment

            • Niheel
              Recognized Expert Moderator Top Contributor
              • Jul 2005
              • 2432

              #7
              Brittany, use code tags when posting code. Makes it more readable to helpers.

              Thanks
              niheel @ bytes

              Comment

              • Sprint45
                New Member
                • Oct 2006
                • 7

                #8
                Brittany

                Sorry, BUT GET UR OWN THREAD,

                Now no one will answer mine because they will only see ur's

                Comment

                • D_C
                  Contributor
                  • Jun 2006
                  • 293

                  #9
                  Sprint45, since your grades are evenly spaced, take a look at the code I posted in this thread.

                  Comment

                  • Sprint45
                    New Member
                    • Oct 2006
                    • 7

                    #10
                    understand your thoughts but how does it fit in the parallel array. Does this become my If statement instead of the ( y < 5 ). if so wouldn't i also have to change the second WHILE statement?

                    thanks for the help
                    mike

                    Comment

                    • D_C
                      Contributor
                      • Jun 2006
                      • 293

                      #11
                      Oops, I missed that part, about the parallel array.

                      You want to find i, assuming minPoints is ascending, such that
                      minPoints[i] <= searchForTotalP oints < minPoints[i+1].
                      In a sense, you need to find the underestimate. The lowest estimate, 0 should always be less than or equal to searchForTotalP oints, that doesn't help.

                      Instead you need to test the highest one first. Since minPoints[0] < minPoints[1] < ... < minPoints[4], you want to test against minPoints[4], or 450, first. If searchForTotalP oints is not greater than or equal to 450, we know that searchForTotalP oints is less than 450.

                      Suppose searchForTotalP oints = 139.
                      Is 139 >= 450? No, so 139 < 450.
                      Is 139 >= 400? No, so 139 < 400 < 450.
                      Is 139 >= 350? No, so 139 < 350 < 400 < 450.
                      Is 139 >= 300? No, so 139 < 300 < 350 < 400 < 450.
                      Is 139 >= 0? Yes, so 0 <= 139 < 300 < 350 < 400 < 450.

                      Therefore i = 0, and 0 <= 139 < 300. So the resulting grade is grades[i], or grades[0] which is "F".

                      Comment

                      Working...