I want to make sure I am understanding this.

This is a discussion on I want to make sure I am understanding this. within the C++ Programming forums, part of the General Programming Boards category; The program I wrote works, but I don't know if I completely understand this subject in C++. Basically I have ...

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    12

    I want to make sure I am understanding this.

    The program I wrote works, but I don't know if I completely understand this subject in C++. Basically I have to enter a number grade and have it return a letter grade. I have to use both a reference parameter and a value parameter. This is what i came up with using the textbook and various tutorials. Letter and Grade in the void statements don't look right to me, not sure why.

    Code:
    #include <iostream>
    using namespace std;
    
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    
    int grade;
    int letter;
    void getScore(int & grade);
    void printGrade (int & letter);
    
    
    int main()
    {
    	void getScore(int & grade);
    	{
    	cout<< "Please enter a number grade: ";
    	cin>> grade;
    	
    }
    void printGrade(int & letter);
    
    
      if ( grade >= 90 )
           cout << "Your final grade is A";
      else if ( grade >= 80 )
           cout << "Your final grade is B";
      else if ( grade >= 70 )
           cout << "Your final grade is C";
      else if ( grade >= 60 )
           cout << "Your final grade is D";
      else
          cout << "Your final grade is F";
    
    
      cin.get();
      cin.get();
    }

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,625
    The results you're getting make sense, if you indent the code and look:
    Code:
    #include <iostream>
    using namespace std;
    
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    
    int grade;
    int letter;
    void getScore(int & grade);
    void printGrade (int & letter);
    
    
    int main()
    {
        void getScore(int & grade);
        {
            cout<< "Please enter a number grade: ";
            cin>> grade;
    
        }
        void printGrade(int & letter);
    
    
        if ( grade >= 90 )
            cout << "Your final grade is A";
        else if ( grade >= 80 )
            cout << "Your final grade is B";
        else if ( grade >= 70 )
            cout << "Your final grade is C";
        else if ( grade >= 60 )
            cout << "Your final grade is D";
        else
            cout << "Your final grade is F";
    
    
        cin.get();
        cin.get();
    }
    All the braces match up conspicuously to make a valid program, and it turns out that the other mistakes you made, while unwanted, are harmless.

    It's obvious to a human what you want though. The parts of a function are actually pretty simple.
    Code:
     void getScore(int & grade);
    This is a function prototype. Apart from the symbols we have lots of words. The blue part is called a return type, which is just the type of the value returned by the return statement. In this case it is void because nothing is returned. Then you have the orange part which is the function's name. Then you have the parameter types indicated in pink, in a list, in parentheses. Each parameter has an optional name, indicated in grey.

    When you define the program function elsewhere, you need braces, indicated in green.
    Code:
    void printGrade(int & grade)
     {
     
      if ( grade >= 90 )
           cout << "Your final grade is A";
      else if ( grade >= 80 )
           cout << "Your final grade is B";
      else if ( grade >= 70 )
           cout << "Your final grade is C";
      else if ( grade >= 60 )
           cout << "Your final grade is D";
      else
          cout << "Your final grade is F";
     
     
      cin.get();
      cin.get();
    }
    Note that the semicolon is left off. This is correct.

    Now when you call a function, you have to consider the usefulness of the return value if there is one. You probably want to store the value in a variable. For the rest of it, you want to type out the name and fill the parameter list with arguments. Arguments are just variables that (typically) come from the calling function.

    So, part of writing good code is not just reading and understanding the errors but considering the code. Hopefully this -- exactly this -- doesn't happen too often!
    Last edited by whiteflags; 07-28-2012 at 03:17 PM.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    Note that functions within functions aren't allowed! Because you somehow confused function definitions with function prototypes, it actually compiled, but keep that in mind!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    12
    Thank you both!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-27-2011, 04:14 PM
  2. Establishing 'make clean' with GNU make
    By Jesdisciple in forum C Programming
    Replies: 9
    Last Post: 04-11-2009, 09:10 AM
  3. How would I make a c++ prgoram make a .exe file?
    By Rune Hunter in forum C++ Programming
    Replies: 9
    Last Post: 12-26-2004, 04:56 PM
  4. Make window in VB but make program in C/C++?
    By Boomba in forum Windows Programming
    Replies: 1
    Last Post: 06-23-2004, 12:29 AM
  5. Replies: 6
    Last Post: 04-20-2002, 06:35 PM

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