Build this simple progam according to earlier standards

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Break2
    New Member
    • Jul 2007
    • 13

    Build this simple progam according to earlier standards

    I am new to C++ and have learned that when I build a program and I want to learn how, it is best to build each program according to the same standard.

    In my programs I want to have a function protocol before the main section and a function definition after the main section.

    Now I would like to build a program that takes 2 integers and switches them, integer A becomes integer B and the other way around. Simple.

    What I have now is this:

    Code:
    #include <iostream>
    using namespace std;
    
    int ChangeInteger(int, int, int); // protocol
    
    int main()
    {
    int IntegerA;
    int IntegerB;
    int IntegerTemp;
    
    cout << "Type 1st integer: ";
    cin  >> IntegerA;
    
    cout << "Type 2nd integer: ";
    cin  >> IntegerB;
    
    IntegerTemp = 5;
    
    cout << "The value of IntegerA is " << ChangeInteger(IntegerA, IntegerB, IntegerTemp) << endl;
    
    system ("pause");
    
    return 0;
    }
    
    int ChangeInteger (int Integer_A, int Integer_B, int Integer_Temp) // definition
    
    {
    int result = Integer_B; 
    return result;
    }
    
    int ChangeInteger2(int, int, int); // protocol
    
    {
    int IntegerA;
    int IntegerB;
    int IntegerTemp;
    
    cout << "The value of IntegerB is " << ChangeInteger2(IntegerA, IntegerB, IntegerTemp) << endl;
    
    int ChangeInteger2 (int Integer_A, int Integer_B, int Integer_Temp) // definition
    
    int result2 = Integer_A;
    return result2;
    }
    This is not working, it is giving me the following error on the "{" sign that comes below the line with the "int ChangeInteger2 (int Integer_A, int Integer_B, int Integer_Temp)" function definition.

    error C2447: '{' : missing function header (old-style formal list?)

    By the way, is it good practice to have a protocol and a definition in each program?
  • phiefer3
    New Member
    • Jun 2007
    • 67

    #2
    Originally posted by Break2
    I am new to C++ and have learned that when I build a program and I want to learn how, it is best to build each program according to the same standard.

    In my programs I want to have a function protocol before the main section and a function definition after the main section.

    Now I would like to build a program that takes 2 integers and switches them, integer A becomes integer B and the other way around. Simple.

    What I have now is this:

    Code:
    #include <iostream>
    using namespace std;
    
    int ChangeInteger(int, int, int); // protocol
    
    int main()
    {
    int IntegerA;
    int IntegerB;
    int IntegerTemp;
    
    cout << "Type 1st integer: ";
    cin  >> IntegerA;
    
    cout << "Type 2nd integer: ";
    cin  >> IntegerB;
    
    IntegerTemp = 5;
    
    cout << "The value of IntegerA is " << ChangeInteger(IntegerA, IntegerB, IntegerTemp) << endl;
    
    system ("pause");
    
    return 0;
    }
    
    int ChangeInteger (int Integer_A, int Integer_B, int Integer_Temp) // definition
    
    {
    int result = Integer_B; 
    return result;
    }
    
    int ChangeInteger2(int, int, int); // protocol
    
    {
    int IntegerA;
    int IntegerB;
    int IntegerTemp;
    
    cout << "The value of IntegerB is " << ChangeInteger2(IntegerA, IntegerB, IntegerTemp) << endl;
    
    int ChangeInteger2 (int Integer_A, int Integer_B, int Integer_Temp) // definition
    
    int result2 = Integer_A;
    return result2;
    }
    This is not working, it is giving me the following error on the "{" sign that comes below the line with the "int ChangeInteger2 (int Integer_A, int Integer_B, int Integer_Temp)" function definition.

    error C2447: '{' : missing function header (old-style formal list?)

    By the way, is it good practice to have a protocol and a definition in each program?
    I think you mean prototype, not protocol. Anyways, your problem is that your changeinterger2 function has a prototype instead of a heading. remove the semicolon from that line and put the parameter variables in it. And put the function prototype at the top of your code with the other one.

    Also, in both functions you are assigning a variable to a new name and then returning that, this isn't necessary.

    Instead of this:
    [CODE=cpp]int result = Integer_B;
    return result;[/CODE]
    you could just put this:
    [CODE=cpp]return Integer_B;[/CODE]

    And a similar thing with your other function.

    Comment

    • Break2
      New Member
      • Jul 2007
      • 13

      #3
      Thanks for that. I just did as you said (except for the 'result' part, I will do that later). My code now looks like:

      Code:
      #include <iostream>
      using namespace std;
      
      int ChangeInteger(int, int); // prototype
      int ChangeInteger2(int, int); // prototype
      
      int main()
      {
      int IntegerA;
      int IntegerB;
      
      cout << "Type 1st integer: ";
      cin  >> IntegerA;
      
      cout << "Type 2nd integer: ";
      cin  >> IntegerB;
      
      cout << "The value of IntegerA is " << ChangeInteger(IntegerA, IntegerB) << endl;
      
      system ("pause");
      
      return 0;
      }
      
      int ChangeInteger (int Integer_A, int Integer_B) // definitie
      
      {
      int result = Integer_B; 
      return result;
      }
      
      {
      int ChangeInteger2(Integer_A, int Integer_B)
      
      int IntegerA;
      int IntegerB;
      
      cout << "The value of IntegerB is " << ChangeInteger2(IntegerA, IntegerB) << endl;
      
      int ChangeInteger2 (int Integer_A, int Integer_B) // definition
      
      int result2 = Integer_A;
      return result2;
      }
      I am still getting a missing function header error on the line with the bracket above: int ChangeInteger2( Integer_A, int Integer_B, int Integer_Temp)

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        line 33 starts a function definition for

        int ChangeInteger2( Integer_A, int Integer_B)

        except you have no left brace.


        line 40 which is inside (int ChangeInteger2( Integer_A, int Integer_B)

        defines

        int ChangeInteger2 (int Integer_A, int Integer_B) // definition

        again.

        It looks like you wanted to call ChangeInteger() inside ChangeInteger2( ) but didn't code th call correctly.

        Comment

        • Break2
          New Member
          • Jul 2007
          • 13

          #5
          I still dont understand something, you say I do not have a left brace but on line 32 I have the left brace. Or I am just missing something?

          I actually want to have the changeinteger part to be applicable to integer a changing into integer b and I have used the changeinteger2 part to change integer b into integer a.

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Originally posted by Break2
            int ChangeInteger2( Integer_A, int Integer_B)
            <<<<<<<<<<<
            int IntegerA;
            int IntegerB;

            cout << "The value of IntegerB is " << ChangeInteger2( IntegerA, IntegerB) << endl;

            int ChangeInteger2 (int Integer_A, int Integer_B) // definition

            int result2 = Integer_A;
            return result2;
            }
            I don't see a left brace here( <<<<<<).

            Plus what is this line for:

            int ChangeInteger2 (int Integer_A, int Integer_B) // definition

            It looks like:

            int ChangeInteger2( Integer_A, int Integer_B)

            and the compiler will see it as trying to define a function within a function and what won't compile.

            Comment

            Working...