print_out

Thanks for the help.
Last edited on

Am I getting somewhere? If I am... I can't figure out the last bit, anything helps..
Last edited on
Anyone wanna give it a shot? haha
@espionage1

1
2
3
4
for (int i = 1; i <= n; i++)
	void print_out(int n); // ??? 3 mistakes ??
     //print_out(i); // Should be..
	return 0;


You already know how to call the function, so why do you have the word void, before the call? Remove it, and your program should run like you want. Also, remove the word, int, before the variable, n. AND, you are supposed to send the variable i, to the function, otherwise, only the one number, the inputted n, will be printed out.

Is this what you mean @whitenite1 ? compiles incorrectly. 2 print_out's not understanding also sorry my english isnt the best..
Last edited on
@espionage1

No, that's not what I meant. You only need 1 call to the function. I was showing you first, that what you had written, had three errors in it. The line below, was what the line should look like. So, all that is needed, was print_out(i);

One other problem I noticed. Your declaration of the function, is wrong.
void function(int);

This should have the actual name of the function to be called.
Change it to void print_out(int);

One last thing. If you don't want your numbers to be printed as one long digit, I suggest you add a space after the cout << n;, so it looks like cout << n << " ";
Last edited on
print_out(i);

is the same thing as

cout << i << " ";

??

Also even with only print_out(i); added, the code will still not compile.

Test the function by placing it in a program that passes a number n to print_out, where this number is entered from the keyboard. The print_out function should have type void; it does not return a value.

How do I test the function?
@whitenite1

I have been struggling on this for a while, thank you.

now I have to test the function, any tips?
Last edited on
@espionage1

Just place a call to the function, with the inputted variable, n, after the input.

Like..
print_out(n);

You should probably add a newline afterwards, just to separate the inputted number, with the string of numbers that's going to be printed.
What you have works, but I don't think it's not what your prof wants. I think you want to pass the n from line nine in your program above, then put your loop from line 11 inside your print_out function. Make sense?
Last edited on
@mgoetschius

yes it works but it's not what my proff wants.. I'm not sure how to do what you are saying, if I move everything it will not compile..

@whitenite1
I don't know what you mean by add another print_out(n);.
Last edited on
@espionage1
this seems to be exactly what your professor is asking for.
Write a function named print_out that prints all the whole number from 1 to N. Test the function by placing it in a program that passes a number n to print_out, 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:



You are sending the test number to the function, then prints out from 1 to the user entered number.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;

void print_out(int);
int main() {
	int n;
	
	cout << "Enter your number: ";
	cin >> n;
	print_out(n); // Print out what user entered

	for (int i = 1; i <= n; i++)
	print_out(i); // This sends the values of the 'i' loop, to the function 
	return 0;
	
}
void print_out(int n) {
	cout << n << " ";
}
So this is all he wants? I've seen codes that go longer after the last bracket..
@espionage1

I can only guess that this is all he wants, based solely on the description you gave in the original post, on what the program was to accomplish. Based on that, this program does exactly that.
I agree that the program accomplishes what was asked, but I understood the directions differently. For clarification, here's what I was thinking for the print_out function:
1
2
3
4
5
void print_out(int n)
{
     for(int i = 1; i <=n; i++)
          std::cout << i << " ";
}
Last edited on
@mgoetschius

Test the function by placing it in a program that passes a number n to print_out, where this number is entered from the keyboard.


Your version doesn't really work for this statement, though. It has to be able to print just the number entered on the keyboard. Your way, prints only a the numbers 1 to N. So, test, fails.
Symanatics arguments, yeah...
Write a function named print_out that prints all the whole number from 1 to N.

Your version doesn't really print the numbers from 1 to n, it only prints a single number.

That being said, I'll say no more, it's not productive. I just wanted to offer espionage1 my thoughts. If op thinks it necessary, he or she can ask the instructor for clarification.

@espionage1
Like I said, what you have accomplishes the assigned task; print the numbers 1 to n.
@espionage1

You wrote:
why does your code ask for a number and later prints that number first?


Reason:
Test the function by placing it in a program that passes a number n to print_out, where this number is entered from the keyboard.


You said, in the original post, you needed to test the function. So, after the initial input, that number gets sent to the function, and gets printed. Then, the loop of 1 to inputted number, gets sent to the function, one digit at a time, to be printed. If you don't really want the test to be done, leave off the print_out(n); // Print out what user entered
Topic archived. No new replies allowed.