declared extra variable, is it wrong?

This is a discussion on declared extra variable, is it wrong? within the C++ Programming forums, part of the General Programming Boards category; Ive just modified one of my program by adding functions into it. I'll show the code here first: Code: #include ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    17

    Question declared extra variable, is it wrong?

    Ive just modified one of my program by adding functions into it. I'll show the code here first:

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    //declaring functions
    int WhatKindOfStudent();
    void CalculateEnglishGrade();
    void CalculateMathGrade();
    void CalculateScienceGrade();
    
    //Functions Overload
    void DisplayGrade(short midterm, short finalExamGrade, short research, short presentation, float finalNumericGrade, char finalLetterGrade);
    void DisplayGrade (short midterm, short finalExamGrade, float finalNumericGrade, char finalLetterGrade);
    void DisplayGrade (short midterm, short finalExamGrade, short research, float finalNumericGrade, char finalLetterGrade);
    //end of functions overload
    //end of declaring functions
    
    
    //Declaring Global Variables + Const
    //declaring english students fixed percentage of marks
        const float ENGLISH_MIDTERM_PERCENTAGE = 0.25;
        const float ENGLISH_FINALEXAM_PERCENTAGE = 0.25;
        const float ENGLISH_RESEARCH_PERCENTAGE = 0.30;
        const float ENGLISH_PRESENTATION_PERCENTAGE = 0.20; //end of english students fixed percentage
    
        //declaring maths students fixed percentage of marks
        const float MATH_MIDTERM_PERCENTAGE = 0.50;
        const float MATH_FINALEXAM_PERCENTAGE = 0.50; //end of math students fixed percentage
    
        //declaring science students fixed percentage of marks
        const float SCIENCE_MIDTERM_PERCENTAGE = 0.40;
        const float SCIENCE_FINALEXAM_PERCENTAGE = 0.40;
        const float SCIENCE_RESEARCH_PERCENTAGE = 0.20; //end of science students fixed percentage
    
        //declaring variables
        short midterm = 0;
        short finalExamGrade = 0;
        short research = 0;
        short presentation = 0;
        float finalNumericGrade = 0;
        char finalLetterGrade;
        char response[256];
        string moreGradesToCalculate;
        //end of variables declaration
        //End of declaration of global variables + const
    
    int main()
    {
        //declaring a local variable
        int iresponse;
        //end of declaration of local variable
    
        //prompt user "whether they want to calculate grades"
        cout<< "Do you want to calculate a grade? Yes/No ";
        cin>> moreGradesToCalculate;
        cin.ignore();
    
    
        //start of for loop for changing userinput to uppercase
        for (unsigned short a = 0; a < moreGradesToCalculate.length(); a++)
        {
            moreGradesToCalculate[a] = toupper (moreGradesToCalculate[a]);
        }
        //end of for loop
        //end of promting user
    
    
        //start of while loop
        while (moreGradesToCalculate == "YES")
        {
            iresponse = WhatKindOfStudent();
    
              //After user had input a valid student type, the following is to calculate the student's grade
              //start of switch statement
              switch (iresponse)
              {
                     //case 1 is english student
                     case 1:
                         CalculateEnglishGrade();
                         DisplayGrade(midterm, finalExamGrade, research, presentation, finalNumericGrade, finalLetterGrade);
    
                         break;
    
    
                          //case 2 is a math student
                          case 2:
                              CalculateMathGrade();
                              DisplayGrade(midterm, finalExamGrade, finalNumericGrade, finalLetterGrade);
    
                              break;
    
    
                               //case 3 is a Science student
                               case 3:
                                   CalculateScienceGrade();
                                   DisplayGrade(midterm, finalExamGrade, research, finalNumericGrade, finalLetterGrade);
    
                                   break;
    
                                    //start of default for the switch statement
                                    default:
                                            cout<< response<< " - is not a valid student type";
                                            return 1;
                                    }
                                    //end of switch statement
    
                                    cout<< endl<< endl<< "Do you have another grade to calculate? Yes/No ";
                                    cin>> moreGradesToCalculate;
                                    cin.ignore();
    
                                    //start of for loop
                                    for (unsigned short a = 0; a < moreGradesToCalculate.length(); a++)
                                    {
                                        moreGradesToCalculate[a] = toupper (moreGradesToCalculate[a]);
                                    }
                                    //end of for loop
                                    }
                                    //end of while loop
    
                                    cout<< "Thanks for using the Grades Calculation program! ";
    
                                    return 0;
                                    }
                                    //end of main
    
                                    //start of int WhatKindOfStudent's function
                                    int WhatKindOfStudent()
                                    {
                                        //promting user for the type of students to calculate
                                        cout<< "*** 1) English, 2) Math, 3) Science *** ";
                                        cin.getline (response, 256);
    
                                        if (strlen(response) == 0)
                                        {
                                            cout<< "You must choose an option";
                                            exit(1); //error #1
                                        }
    
                                        else if ((atoi(response) < 1) || (atoi(response) > 3))
                                        {
                                            cout<< " - is not a valid option.";
                                            exit(2);//error #2
                                        }
    
                                        //return an int of atoi(known as ASCII to Interger)
                                        return atoi(response);
                                    }
                                    //end of WhatKindOfStudent's function
    
                                    //start of void CalculateEnglishGrade()
                                    void CalculateEnglishGrade()
                                    {
                                        //Promt user for "midterm" grade
                                       cout<< "Please enter the Midterm Grade: ";
                                       cin.getline (response,256);
                                       midterm = atoi(response);
                                       //end of "midterm" grade
    
                                      //Promt user for "finalExamGrade"
                                      cout<< "Please enter the Final Examination Grade: ";
                                      cin.getline (response,256);
                                      finalExamGrade = atoi(response);
                                      //end of "finalExamGrade"
    
                                      //promt user for "research" grade
                                      cout<< "Please enter the research Grade: ";
                                      cin.getline (response,256);
                                      research = atoi(response);
                                      //end of "research"
    
                                      //Prompt user for "presentation" grade
                                      cout<< "Please enter the Presentation Grade: ";
                                      cin.getline (response,256);
                                      presentation = atoi(response);
                                      //end of "presentation"
    
                                      finalNumericGrade = (midterm * ENGLISH_MIDTERM_PERCENTAGE) +
                                                 (finalExamGrade * ENGLISH_FINALEXAM_PERCENTAGE) +
                                                        (research * ENGLISH_RESEARCH_PERCENTAGE) +
                                                 (presentation * ENGLISH_PRESENTATION_PERCENTAGE);
    
                                      if (finalNumericGrade >= 93)
                                     {
                                         finalLetterGrade = 'A';
                                     }
    
                                     else if ((finalNumericGrade >=85) && (finalNumericGrade < 93))
                                     {
                                         finalLetterGrade = 'B';
                                     }
    
                                     else if ((finalNumericGrade >= 78) && (finalNumericGrade < 85))
                                     {
                                         finalLetterGrade = 'C';
                                     }
    
                                    else if ((finalNumericGrade >= 70) && (finalNumericGrade < 78))
                                    {
                                        finalNumericGrade = 'D';
                                    }
    
                                    else if (finalNumericGrade < 70)
                                    {
                                        finalNumericGrade = 'F';
                                    }
    
                                    }
                                    //end of void CalculateEnglishGrade()
    
    
                                    //start of void CalculateMathGrade()
                                    void CalculateMathGrade()
                                    {
                                        //Prompt user for "midterm" grade
                                        cout<< "Enter the Midterm Grade: ";
                                        cin.getline(response,256);
                                        midterm = atoi(response);
                                       //end of "midterm" grade
    
                                       //Promt user for "finalExamGrade"
                                       cout<< "Enter the Final Examination Grade: ";
                                       cin.getline(response,256);
                                       finalExamGrade = atoi(response);
                                       //end of "finalExamGrade"
    
                                       finalNumericGrade = (midterm * MATH_MIDTERM_PERCENTAGE) +
                                                   (finalExamGrade * MATH_FINALEXAM_PERCENTAGE);
    
                                       if (finalNumericGrade >= 90)
                                       {
                                           finalLetterGrade = 'A';
                                       }
    
                                       else if ((finalNumericGrade >= 83) && (finalNumericGrade < 90))
                                       {
                                           finalLetterGrade = 'B';
                                       }
    
                                       else if ((finalNumericGrade >= 76) && (finalNumericGrade < 83))
                                       {
                                           finalLetterGrade = 'C';
                                       }
    
                                       else if ((finalNumericGrade >= 65) && (finalNumericGrade < 76))
                                       {
                                           finalNumericGrade = 'D';
                                       }
    
                                       else if (finalNumericGrade < 65)
                                       {
                                           finalNumericGrade = 'F';
                                       }
    
                                    }
                                    //end of void CalculateMathGrade's function
    
                                       //start of void CalculateScienceGrade()
                                       void CalculateScienceGrade()
                                       {
                                           //Prompt user for "midterm" grade
                                           cout<< "Enter the Midterm Grade: ";
                                           cin.getline(response,256);
                                           midterm = atoi(response);
                                          //end of "midterm" grade
    
                                         //Promt user for "finalExamGrade"
                                         cout<< "Enter the Final Examination Grade: ";
                                         cin.getline(response,256);
                                         finalExamGrade = atoi(response);
                                         //end of "finalExamGrade"
    
                                         //Prompt user for "research" grade
                                         cout<< "Enter the Research Grade: ";
                                         cin.getline(response, 256);
                                         research = atoi(response);
                                         //end of "research"
    
                                         finalNumericGrade = (midterm * SCIENCE_MIDTERM_PERCENTAGE) +
                                                   (finalExamGrade * SCIENCE_FINALEXAM_PERCENTAGE) +
                                                   (research * SCIENCE_RESEARCH_PERCENTAGE);
    
                                         if (finalNumericGrade >= 90)
                                         {
                                              finalLetterGrade = 'A';
                                         }
    
                                          else if ((finalNumericGrade >= 80) && (finalNumericGrade < 90))
                                          {
                                              finalLetterGrade = 'B';
                                          }
    
                                          else if ((finalNumericGrade >= 70) && (finalNumericGrade < 80))
                                          {
                                               finalLetterGrade = 'C';
                                          }
    
                                          else if ((finalNumericGrade >= 60) && (finalNumericGrade < 70))
                                          {
                                               finalNumericGrade = 'D';
                                          }
    
                                          else if (finalNumericGrade < 60)
                                          {
                                               finalNumericGrade = 'F';
                                          }
    
                                       }
                                       //end of void CalculateScienceGrade()
    
                                         //start of functions overloading DisplayGrade();
                                         //function display for english student
                                         void DisplayGrade (short midterm, short finalExamGrade, short research, short presentation, float finalNumericGrade, char finalLetterGrade)
                                         {
                                             //display student scores' informations
                                             cout<< "*** ENGLISH STUDENT ***" <<endl <<endl;
                                             cout<< "Midterm Grade is "<< midterm<< endl;
                                             cout<< "Final Exame is: "<< finalExamGrade<< endl;
                                             cout<< "Research is: "<< research<< endl;
                                             cout<< "Presentation is: "<< presentation<< endl<< endl;
                                             cout<< "Final Numeric Grade is: "<< finalNumericGrade<< endl;
                                             cout<< "Final Letter Grade is: "<< finalLetterGrade;
    
                                             //end of english student
                                         }
                                         //end of function overload display for english student
    
                                         //function display for math student
                                         void DisplayGrade (short midterm, short finalExamGrade, float finalNumericGrade, char finalLetterGrade)
                                         {
                                             //display student scores' informations
                                             cout<< "*** MATH STUDENT ***" <<endl <<endl;
                                             cout<< "Midterm Grade is "<< midterm<< endl;
                                             cout<< "Final Exame is: "<< finalExamGrade<< endl;
                                             cout<< "Research is: "<< research<< endl;
                                             cout<< "Presentation is: "<< presentation<< endl<< endl;
                                             cout<< "Final Numeric Grade is: "<< finalNumericGrade<< endl;
                                             cout<< "Final Letter Grade is: "<< finalLetterGrade;
    
                                             //end of math student
                                         }
                                         //end of display for math student
    
                                         //function display for science student
                                         void DisplayGrade (short midterm, short finalExamGrade, short research, float finalNumericGrade, char finalLetterGrade)
                                         {
                                             //display student scores' informations
                                             cout<< "*** SCIENCE STUDENT ***" <<endl <<endl;
                                             cout<< "Midterm Grade is "<< midterm<< endl;
                                             cout<< "Final Exam is: "<< finalExamGrade<< endl;
                                             cout<< "Research is: "<< research<< endl;
                                             cout<< "Presentation is: "<< presentation<< endl<< endl;
                                             cout<< "Final Numeric Grade is: "<< finalNumericGrade<< endl;
                                             cout<< "Final Letter Grade is: "<< finalLetterGrade;
    
                                             //end of science student
                                         }
                                         //end of Display for science student
                                         //end of display for science student's function                      }
    First of all, I must say that Im sorry for the long piece of code. I jsut want to show the source code to you guys, so that you guys can correct about what Im doubting about, declaring extra variable.


    In these codes here:

    Code:
    //start of while loop
        while (moreGradesToCalculate == "YES")
        {
            iresponse = WhatKindOfStudent();
    
              //After user had input a valid student type, the following is to calculate the student's grade
              //start of switch statement
              switch (iresponse)
    I have the iresponse variable. Should I add another iresponse variable in another functions, which is used to be called, from the main() here:

    Code:
    int WhatKindOfStudent()
                                    {
    
                                        int iresponse; //IN THIS VARIABLE A MUST TO BE DECLARED? 
    
                                        //promting user for the type of students to calculate
                                        cout<< "*** 1) English, 2) Math, 3) Science *** ";
                                        cin.getline (response, 256);
    
                                        if (strlen(response) == 0)
                                        {
                                            cout<< "You must choose an option";
                                            exit(1); //error #1
                                        }
    
                                        else if ((atoi(response) < 1) || (atoi(response) > 3))
                                        {
                                            cout<< " - is not a valid option.";
                                            exit(2);//error #2
                                        }
    
                                        //return an int of atoi(known as ASCII to Interger)
                                        return atoi(response);
                                    }
    I actually tried to add in 'extra variable' iresponse in the function WhatKindOfStudent(). It works out just fine. It actually works the same even if I did not add the variable iresponse inside the WhatKindOfStudent's function. Can someone tell me is it a must to add? Is there any advantage to it? If there is advantage, what are thsoe advantage?


    ------------------------------------------------------------------------------------------------------------------

    Another question:

    why can't I declare variable like this way:

    Code:
    int 1response;
    If I declare a variable like the above mentioned, I will get error result like this:

    Code:
    invalid suffix "1response" on interger constant
    in my codeblock's IDE + compiler.

    Can someone help me out? Thanks in advance! I love you guys ^^

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Ive just modified one of my program by adding functions into it.
    You need to fix your indentation. Instead of writing:
    Code:
    int main()
    {
        // ...
        }
        //end of main
    
        int foo()
        {
            // ...
        }
        //end of foo
    You should write:
    Code:
    int main()
    {
        // ...
    }
    
    int foo()
    {
        // ...
    }
    You can still keep the "end of function" comments if you want, but they become less necessary with proper indentation and functions that are not too long.

    why can't I declare variable like this way:
    Because there are naming rules, and according to those rules variable names cannot begin with a digit, but 1response certainly begins with the digit 1.

    Oh, and these should be definitely be local variables, not global variables:
    Code:
    //declaring variables
    short midterm = 0;
    short finalExamGrade = 0;
    short research = 0;
    short presentation = 0;
    float finalNumericGrade = 0;
    char finalLetterGrade;
    char response[256];
    string moreGradesToCalculate;
    //end of variables declaration
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    17
    Quote Originally Posted by laserlight View Post
    You need to fix your indentation. Instead of writing:
    Code:
    int main()
    {
        // ...
        }
        //end of main
    
        int foo()
        {
            // ...
        }
        //end of foo
    You should write:
    Code:
    int main()
    {
        // ...
    }
    
    int foo()
    {
        // ...
    }
    You can still keep the "end of function" comments if you want, but they become less necessary with proper indentation and functions that are not too long.


    Because there are naming rules, and according to those rules variable names cannot begin with a digit, but 1response certainly begins with the digit 1.

    Oh, and these should be definitely be local variables, not global variables:
    Code:
    //declaring variables
    short midterm = 0;
    short finalExamGrade = 0;
    short research = 0;
    short presentation = 0;
    float finalNumericGrade = 0;
    char finalLetterGrade;
    char response[256];
    string moreGradesToCalculate;
    //end of variables declaration
    Lazer, you didn't really quite answer my question >.< Thanks for pointing out my identation's problem and for telling me that 1response is disallowed to be a variable.

    Actually, those local variables, which you've mentioned, must be global variables...or else the program won't simply compile, and even if they did compile, the program won't run like what I want them to do.

    My actual problem is that, is it a must to add:

    Code:
    int iresponse;
    in the WhatKindOfStudent()'s function. I want to know if that's a good thing to declare inside that function, and that if its a good thing, what's good about it.

    Please take a look into my codes and you'll understand what I mean...I'm sorry...

    Can someone help me out?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Lazer, you didn't really quite answer my question >.<
    That's true

    Well, one rule of thumb is to declare variables near first use. In other words, if you might not use a variable, then delay declaring it for as long as you can. By this reasoning, if you will not use a variable, then do not declare it.

    Actually, those local variables, which you've mentioned, must be global variables...or else the program won't simply compile, and even if they did compile, the program won't run like what I want them to do.
    That has more to do with the design of your program than with an actual requirement of C++. If you have been relying on global variables in your functions, then just changing them to local variables in the main() function is not enough. You also need to change those functions to have the appropriate parameters such that you can pass the local variables in main() to those functions when they are called.

    This is actually another example of declaring variables near first use. With global variables, you declare them whether or not you use them. With local variables, you can declare them only in the scope where they are used. This makes them easier to track, and you do not waste time creating variables that you do not actually use.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. variables when declared inside or outside a function
    By jas_atwal in forum C Programming
    Replies: 6
    Last Post: 12-14-2007, 01:42 PM
  3. Where can & can't variables be declared in C?
    By cpjust in forum C Programming
    Replies: 5
    Last Post: 11-17-2007, 05:42 PM
  4. Read multiple data for one declared variable
    By c++dummy in forum C++ Programming
    Replies: 3
    Last Post: 11-04-2007, 01:13 PM
  5. C global variables
    By Dohojar in forum C Programming
    Replies: 18
    Last Post: 02-25-2002, 11:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21