-
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
-
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;
}
-
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......