Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. C++

C++

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
5 Posts 5 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Member_15281733
    wrote on last edited by
    #1

    #include #include #include #include #define MAX_RESPONSE_LENGTH 5

    using namespace std;
    int main()

     {
          const float ENGLISH\_MIDTERM\_PERCENTAGE       = .25;
          const float ENGLISH\_FINALEXAM\_PERCENTAGE     = .25;
          const float ENGLISH\_RESEARCH\_PERCENTAGE      = .30;
          const float ENGLISH\_PRESENTATION\_PERCENTAGE  = .20;
    
          const float SCIENCE\_MIDTERM\_PERCENTAGE    = .40;
          const float SCIENCE\_FINALEXAM\_PERCENTAGE  = .40;
          const float SCIENCE\_RESEARCH\_PERCENTAGE   = .20;
    
          const float MATHS\_MIDTERM\_PERCENTAGE    = .50;
          const float MATHS\_FINALEXAM\_PERCENTAGE  = .50;
    
                float finalNumericGrade = 0;
                char finalLetteredGrade;
                char response \[MAX\_RESPONSE\_LENGTH\];
    
                string moreGradesToCalculate;
    
            short midtermGrade       = 0;
            short finalExamGrade     = 0;
            short researchGrade      = 0;
            short presentationGrade  = 0;
    
                     cout << "Do you have a grade to calculate? \\n";
                     cin >> moreGradesToCalculate;
    
                     for(int x = 0; x < moreGradesToCalculate.length();x++){
                        moreGradesToCalculate\[x\] = toupper(moreGradesToCalculate\[x\]);
                     }
    
                       while(moreGradesToCalculate == "YES" ){
    
                cout << "Designate a student type to calculate.\\n\\n";
                cout << "1.English           2.Science\\n\\n"
                     << "3.Maths \\n";
                cin >> response;
    
               if(strlen(response) == 0){
                 cout << "You must select a student type.\\n" << endl;
                      return 0;
               }
    
                if((atoi(response) < 1) || (atoi(response) > 3)) {
                  cout << response << " - is not a valid student type.";
                    return 1;
                }
    
              switch (atoi(response))
              {
              case 1:
                cout << "Midterm Grade      : ";
                cin >> response;
                midtermGrade = atoi(response);
                cout << "Final Exam Grade   : ";
                cin >> response;
                finalExamGrade = atoi(response);
                cout << "Research Grade     : ";
                cin >> response;
                researchGrade = atoi(response);
                cout << "Presentation Grade :
    
    OriginalGriffO D L 3 Replies Last reply
    0
    • M Member_15281733

      #include #include #include #include #define MAX_RESPONSE_LENGTH 5

      using namespace std;
      int main()

       {
            const float ENGLISH\_MIDTERM\_PERCENTAGE       = .25;
            const float ENGLISH\_FINALEXAM\_PERCENTAGE     = .25;
            const float ENGLISH\_RESEARCH\_PERCENTAGE      = .30;
            const float ENGLISH\_PRESENTATION\_PERCENTAGE  = .20;
      
            const float SCIENCE\_MIDTERM\_PERCENTAGE    = .40;
            const float SCIENCE\_FINALEXAM\_PERCENTAGE  = .40;
            const float SCIENCE\_RESEARCH\_PERCENTAGE   = .20;
      
            const float MATHS\_MIDTERM\_PERCENTAGE    = .50;
            const float MATHS\_FINALEXAM\_PERCENTAGE  = .50;
      
                  float finalNumericGrade = 0;
                  char finalLetteredGrade;
                  char response \[MAX\_RESPONSE\_LENGTH\];
      
                  string moreGradesToCalculate;
      
              short midtermGrade       = 0;
              short finalExamGrade     = 0;
              short researchGrade      = 0;
              short presentationGrade  = 0;
      
                       cout << "Do you have a grade to calculate? \\n";
                       cin >> moreGradesToCalculate;
      
                       for(int x = 0; x < moreGradesToCalculate.length();x++){
                          moreGradesToCalculate\[x\] = toupper(moreGradesToCalculate\[x\]);
                       }
      
                         while(moreGradesToCalculate == "YES" ){
      
                  cout << "Designate a student type to calculate.\\n\\n";
                  cout << "1.English           2.Science\\n\\n"
                       << "3.Maths \\n";
                  cin >> response;
      
                 if(strlen(response) == 0){
                   cout << "You must select a student type.\\n" << endl;
                        return 0;
                 }
      
                  if((atoi(response) < 1) || (atoi(response) > 3)) {
                    cout << response << " - is not a valid student type.";
                      return 1;
                  }
      
                switch (atoi(response))
                {
                case 1:
                  cout << "Midterm Grade      : ";
                  cin >> response;
                  midtermGrade = atoi(response);
                  cout << "Final Exam Grade   : ";
                  cin >> response;
                  finalExamGrade = atoi(response);
                  cout << "Research Grade     : ";
                  cin >> response;
                  researchGrade = atoi(response);
                  cout << "Presentation Grade :
      
      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      Compiling does not mean your code is right! :laugh: Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send. So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging. Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:

      Input Expected output Actual output
      1 2 1
      2 4 4
      3 6 9
      4 8 16

      Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input. So with that, you can look at the code and it's obvious that it's somewhere here:

      int Double(int value)
      {
      return value * value;
      }

      Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to). Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line. If not, why not? How does it differ? Hopefully, that should help you locate which part of that code has a problem, and what the problem is. This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      1 Reply Last reply
      0
      • M Member_15281733

        #include #include #include #include #define MAX_RESPONSE_LENGTH 5

        using namespace std;
        int main()

         {
              const float ENGLISH\_MIDTERM\_PERCENTAGE       = .25;
              const float ENGLISH\_FINALEXAM\_PERCENTAGE     = .25;
              const float ENGLISH\_RESEARCH\_PERCENTAGE      = .30;
              const float ENGLISH\_PRESENTATION\_PERCENTAGE  = .20;
        
              const float SCIENCE\_MIDTERM\_PERCENTAGE    = .40;
              const float SCIENCE\_FINALEXAM\_PERCENTAGE  = .40;
              const float SCIENCE\_RESEARCH\_PERCENTAGE   = .20;
        
              const float MATHS\_MIDTERM\_PERCENTAGE    = .50;
              const float MATHS\_FINALEXAM\_PERCENTAGE  = .50;
        
                    float finalNumericGrade = 0;
                    char finalLetteredGrade;
                    char response \[MAX\_RESPONSE\_LENGTH\];
        
                    string moreGradesToCalculate;
        
                short midtermGrade       = 0;
                short finalExamGrade     = 0;
                short researchGrade      = 0;
                short presentationGrade  = 0;
        
                         cout << "Do you have a grade to calculate? \\n";
                         cin >> moreGradesToCalculate;
        
                         for(int x = 0; x < moreGradesToCalculate.length();x++){
                            moreGradesToCalculate\[x\] = toupper(moreGradesToCalculate\[x\]);
                         }
        
                           while(moreGradesToCalculate == "YES" ){
        
                    cout << "Designate a student type to calculate.\\n\\n";
                    cout << "1.English           2.Science\\n\\n"
                         << "3.Maths \\n";
                    cin >> response;
        
                   if(strlen(response) == 0){
                     cout << "You must select a student type.\\n" << endl;
                          return 0;
                   }
        
                    if((atoi(response) < 1) || (atoi(response) > 3)) {
                      cout << response << " - is not a valid student type.";
                        return 1;
                    }
        
                  switch (atoi(response))
                  {
                  case 1:
                    cout << "Midterm Grade      : ";
                    cin >> response;
                    midtermGrade = atoi(response);
                    cout << "Final Exam Grade   : ";
                    cin >> response;
                    finalExamGrade = atoi(response);
                    cout << "Research Grade     : ";
                    cin >> response;
                    researchGrade = atoi(response);
                    cout << "Presentation Grade :
        
        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        Member 15281733 wrote:

        PLEASE HELP ME EXAMINE THE LOGICAL ERROR IN THIS CODE.... 1. THE STRLEN() FUNCTION THAT CHECK IF THE USER PRESS THE ENTER KEY WITHOUT PROVING A RESPONSE TO THE QUESTION. 2. THE DEFAULT KEYWORD IN THE SWITCH STATEMENT THAT CHECKS A NON-INTEGER RESPONSE FROM THE USER.

        You've failed to explain exactly what the problem is. How are we, therefore, to know what your program is supposed to do?

        "One man's wage rise is another man's price increase." - Harold Wilson

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

        1 Reply Last reply
        0
        • M Member_15281733

          #include #include #include #include #define MAX_RESPONSE_LENGTH 5

          using namespace std;
          int main()

           {
                const float ENGLISH\_MIDTERM\_PERCENTAGE       = .25;
                const float ENGLISH\_FINALEXAM\_PERCENTAGE     = .25;
                const float ENGLISH\_RESEARCH\_PERCENTAGE      = .30;
                const float ENGLISH\_PRESENTATION\_PERCENTAGE  = .20;
          
                const float SCIENCE\_MIDTERM\_PERCENTAGE    = .40;
                const float SCIENCE\_FINALEXAM\_PERCENTAGE  = .40;
                const float SCIENCE\_RESEARCH\_PERCENTAGE   = .20;
          
                const float MATHS\_MIDTERM\_PERCENTAGE    = .50;
                const float MATHS\_FINALEXAM\_PERCENTAGE  = .50;
          
                      float finalNumericGrade = 0;
                      char finalLetteredGrade;
                      char response \[MAX\_RESPONSE\_LENGTH\];
          
                      string moreGradesToCalculate;
          
                  short midtermGrade       = 0;
                  short finalExamGrade     = 0;
                  short researchGrade      = 0;
                  short presentationGrade  = 0;
          
                           cout << "Do you have a grade to calculate? \\n";
                           cin >> moreGradesToCalculate;
          
                           for(int x = 0; x < moreGradesToCalculate.length();x++){
                              moreGradesToCalculate\[x\] = toupper(moreGradesToCalculate\[x\]);
                           }
          
                             while(moreGradesToCalculate == "YES" ){
          
                      cout << "Designate a student type to calculate.\\n\\n";
                      cout << "1.English           2.Science\\n\\n"
                           << "3.Maths \\n";
                      cin >> response;
          
                     if(strlen(response) == 0){
                       cout << "You must select a student type.\\n" << endl;
                            return 0;
                     }
          
                      if((atoi(response) < 1) || (atoi(response) > 3)) {
                        cout << response << " - is not a valid student type.";
                          return 1;
                      }
          
                    switch (atoi(response))
                    {
                    case 1:
                      cout << "Midterm Grade      : ";
                      cin >> response;
                      midtermGrade = atoi(response);
                      cout << "Final Exam Grade   : ";
                      cin >> response;
                      finalExamGrade = atoi(response);
                      cout << "Research Grade     : ";
                      cin >> response;
                      researchGrade = atoi(response);
                      cout << "Presentation Grade :
          
          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          if((finalNumericGrade >= 83 ) & (finalNumericGrade < 90 ))

          If you reach this statement the grade must be less than 90, because you already checked that in the previous statement. So all you need is

          if(finalNumericGrade >= 83)

          And similar for the following tests. But putting all your code into main like that makes it so much more difficult to debug. Split the different parts out into functions to make it easier to see where your errors may be.

          C 1 Reply Last reply
          0
          • L Lost User

            if((finalNumericGrade >= 83 ) & (finalNumericGrade < 90 ))

            If you reach this statement the grade must be less than 90, because you already checked that in the previous statement. So all you need is

            if(finalNumericGrade >= 83)

            And similar for the following tests. But putting all your code into main like that makes it so much more difficult to debug. Split the different parts out into functions to make it easier to see where your errors may be.

            C Offline
            C Offline
            charlieg
            wrote on last edited by
            #5

            Yeah, that might go through a C++ compiler, but it's not really C++. I just didn't want to pile on. OP - debugger is your friend.

            Charlie Gilley <italic>Stuck in a dysfunctional matrix from which I must escape... "Where liberty dwells, there is my country." B. Franklin, 1783 “They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups