print_out

This is a discussion on print_out within the C++ Programming forums, part of the General Programming Boards category; Hello All! I`m still trying to wade through C++ and am now using VC++. I tried to run a program ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    23

    print_out

    Hello All!

    I`m still trying to wade through C++ and am now using VC++. I tried to run
    a program and keep getting a message that says .....


    "function1102506.cpp(23) : error C2448: 'print_out' : function-style initializer appears to be a function definition
    Build log was saved at "file://i:\My Documents\Visual Studio 2005\Projects\function1102506\function1102506\Debu g\BuildLog.htm" "



    Code:
    #include <iostream>
    #include "stdafx.h"
    using namespace std;
    
    // Function must be declared before being used.
    
    void print_out(int n);
      int n;
      int main() {
          int n;
    
             cout << "Enter a number and press ENTER: ";
             cin >> n;
    
          print_out(n);
    
        return 0;
    }
    
    // Print-out function.
    // Prints numbers from 1 to n. 
    
    int print_out(n) {
        int i;
    
          for (i = 1; i <= n; i++)     // For i = 1 to n,
              cout << i << " ";        //   print i
          return sum;
    }
    I have tried to make sure this is indented properly and all my programming "grammar" is correct. However since I am only starting I hope you will forgive any missteps.

    I can`t find anything on the web that might clue me in and MSVC++ "Help" is no help. Thank You All for any advice. Plain

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    change:

    int print_out(n)

    to

    int print_out( int n)

    its prototype is declared as void, change that, and sum isn't initialised or even declared in the print_out function. it can't be returned because of that.

    edit:

    this should do it:
    Code:
    #include <iostream>
    #include "stdafx.h"
    
    using namespace std;
    
    
    void print_out(int n);
    
    
    int main( void )
    {
        int n;
    
        cout<< "Enter a number and press ENTER: ";
        cin >> n;
    
        print_out(n);
    
        return 0;
    }
    
    
    int print_out( int n )
    {
        int i=0, sum=0;
    
        for (i = 1; i <= n; i++) 
    // This is normally to be i=0; i<whatever; i++. I didn't change it in case you had a use for it starting at one
        {
            sum += i;
            cout << i << " ";        //   print i
        }
        
        return sum;
    }
    Last edited by twomers; 10-28-2006 at 02:28 PM.

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    23
    Hi Twomers:

    Well, I reworded my web search and found this exact example in a previous post here on C Programming. It seems the book is wrong and the function declarator should have been "void print_out (int n)" and the "return sum" command shouldn`t even be in the example. Boy this is tough enough without wrong instructions getting in the way. Now I know why a couple of the examples of this book haven`t acted right. Thanks for helping me out. Plain......

Popular pages Recent additions subscribe to a feed

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