Jump to content

beginner error

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
Gondoma

Gondoma

    Newbie

  • Members
  • Pip
  • 6 posts
i am very new to programming having trouble with this exercise in my book any help would be great!!

Excercise:
Write a function named print_out that prints all the whole numbers from 1 to n. Test the function by placing it in a program that passes a number n to it, where this number is entered from the keyboard. The print_out function should have type void; it does not return a value. The function can be called with a simple statement.
-----
The chapter is about functions and i believe the purpose is just to use void.

i am using visual c++

i am getting these errors.
error C2065: 'n' : undeclared identifier
error C2448: 'print_out' : function-style initializer appears to be a function definition

here is what i have so far

p.s. please forgive the sloppy post i mostly just read the forums and dont post much
#include <stdafx.h>

#include <iostream>

using namespace std;




void print_out(int n);


int main() {

    int n;


    cout << "Enter a number and press ENTER: ";

    cin >> n;


    print_out(n);


    return 0;

}


 


int print_out(n) {

    int i;


    for (i = 1; i <= n; i++)    

        cout << i << " ";        

    return sum;

}


some of this code was copied from the book but i believe it is correct syntax

#2
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
[COLOR="Red"]void[/COLOR] print_out([COLOR="Red"]int[/COLOR] n) {
// ...
[COLOR="Red"]//  return sum;[/COLOR]
}


#3
bobdark

bobdark

    Programmer

  • Members
  • PipPipPipPip
  • 164 posts
notice that return type of print_out is void in declaration and int in definition.
Also in the definition you have to specify type of each parameter.