简体   繁体   中英

need help creating function in C that gives out specific amount of \n

This is how my code looks right now:

#include <stdio.h>

void print_lines(int n); 

int main() {
printf("%i", print_lines(7));
return 0;
}

void print_lines(int n) {
    int i;
    scanf("%i", &n);
    for (i = 1; n != 0; --1) 
        printf("\n");
}

The aim is that the function prints out as many new lines as the user puts in with the scan f function. what am I doing wrong?

Here is what you wish:

#include <stdio.h>

void print_lines(int n); 

int main() {
print_lines(7);
return 0;
}

void print_lines(int n) {
    int i;
    for (i = n; i >= 1; --i) 
        printf("\n");
}
  • If the return type of print_lines is void , you cannot use it in an expression as if you were using its value.
  • If you are passing a value ( 7 ) to the function, then you need not read it again using scanf .
  • If you are already printing in print_lines , then you need not use printf in main . Just the function call is enough.
  • for (i = 1; n != 0; --1) won't get you anywhere. This line alone has too many errors. You are initializing i , testing for n and incrementing 1 (which is not possible in C).

Try reading some basics for better understanding.

Another "trick" might be:

printf("%.*s\n", 7, "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");

This would print the first 7 characters from the string of 20 characters given as third argument.

I think a better implementation of what you're trying to get at is this:

#include <stdio.h>

void print_lines(int n); 

int main() {
    /* take input here (how many lines etc) */
    print_lines(7));
    return 0;
}

void print_lines(int n) {
    int i;
    for (i = 0; i < n; ++i) 
        printf("\n");
}

The variant where you want to use inside of printf would be the following:

#include <stdio.h>

void print_lines(int n); 

int main() {
    /* take input here (how many lines etc) */
    char* lines = print_lines(7);
    printf("%s", lines);
    free(lines) // <-- important
    return 0;
}

char* print_lines(int n) {
    int i;
    char* res = malloc(sizeof(char) * (n + 1)); // <-- important, for \0 termination
    for (i = 0; i < n; ++i) 
        res[i] = '\n';
    res[n] = '\0';
    return res;
}

However I'd rather use a more generic approach, where you can get N of any character supplied to the function as a secondary parameter. I'll leave that part to you.

EDIT: Here's a version with a user-created buffer:

#include <stdio.h>

void print_lines(int n); 

int main() {
    /* take input here (how many lines etc) */
    char buf[8]; // if you use GCC you can use char buf[N], these are variable length arrays, if not, use a similar malloc method above
    print_lines(7, buf);
    printf("%s", buf);
    return 0;
}

void print_lines(int n, char buf[]) {
    int i;
    for(i = 0; i < n; ++i)
        buf[i] = '\n';
    buf[n] = '\0';
}

And finally, the fantasy solution StoryTeller suggested:

#include <stdio.h>

void most_generic_printN(int n, char c, FILE* f) {
    int i;
    for(i = 0; i < n; ++i)
        fprintf(f, "%c", c);
}

int main() {
    most_generic_printN(10, 'a', stdout);
    return 0;
}

In the above solution, stdout is the standard output stream, which is what you see as the console. You can redirect this to be a file and such. Play around with it!

"While N goes to 0, print a newline."

void print_lines(int n)
{
    while (n --> 0) printf("\n");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM