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.
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 << " ";
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.
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?
@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>
usingnamespace 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 << " ";
}
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 << " ";
}
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.
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.
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