отсканируйте строку и напечатайте ее в C - PullRequest
0 голосов
/ 14 апреля 2020

Что я могу сделать, чтобы сделать это?

#include <stdio.h>
#include <string.h>

void lee(char s[]);
void escribe(char s[]);

int main()
{
    char str[20];
    char ch;

    printf("1 escribe\n2 lee\n");
    ch=getchar();
    switch(ch) {
        case '1': 
            printf("you are in escribe (write)\n");
            lee(str);
            break;
        case '2': 
            printf("you are in lee (read)\n");
            escribe(str);            
            break;
        default: puts("Error");
    }


    return 0;
}

void lee(char s[]){
    printf("write your sentense\n");
    scanf("%[^\n]", s);
}

void escribe(char s[]){
    printf("Entered string is %s \n", s);
}

1 Ответ

0 голосов
/ 14 апреля 2020

Я не говорю, что это хороший код, но я "думаю", это то, что вы пытаетесь сделать. Может быть, это даст вам идею, чтобы вы могли исправить свой код так, как вы этого хотите.

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>

    void lee(char s[]);
    void escribe(char s[]);

    int main()
    {
        char str[20] = {0};
        char ch = '\0';


        while(1)
        {
            printf("\n1 escribe:\n2 lee:\n");
            ch = getchar();
            fseek(stdin, 0, 0);

            switch(ch) {
                case '1' :
                    printf("\nYou are in escribe (write)\n\n");
                    lee(str);
                    break;

                case '2' :
                    printf("\nYou are in lee (read)\n\n");
                    escribe(str);
                    break;

                default: puts("Error");
                    exit(1);
            }
        }
    return 0;
    }

void lee(char s[]){
    printf("Write your sentence: \n");
    scanf("%19[^\n]", s);
    fseek(stdin, 0,0);
    printf("\n");
}

void escribe(char s[]){
    printf("The entered string is: %s ", s);
    printf("\n");
}
...