我试图在数组中存储多个带有空格的字符串。

#include<stdio.h>
#define L 30

void main(){

    puts("\t+----------------------+\n\t| Contacts Application |\n\t+----------------------+\n");

    int noc; // number of contacts

    printf(" How many contacts do you want to store: ");
    scanf("%d", &noc);

    char name[noc][L];
        int  number[noc];
        int  a, b; // for counters in loop in switch

    if(noc>0){

        for(int i = 0; i < noc; i++){
            printf("\n\tName %d: ", i+1);
            //scanf("%s", &name[i]);
            fgets(name[i], L, stdin);

            printf("\tNumber: ");
            scanf("%d", &number[i]);
        }

        /** This will clear the screen **/
        #ifdef _WIN64
         system("cls");
        #elif __linux__
         system("clear");
        #endif

        puts("\n\tAll contacts have been saved successfully.\n");
        puts("\t1. Show all the contacts.");
        puts("\t2. Search any contact.\n");

        int choice;
        printf("\tEnter your choice: ");
        scanf("%d", &choice);

        switch(choice){
            case 1:

             for(int a = 0; a < noc; a++){
                printf("\n\t %d: %s - %d", a+1, name[a], number[a]);
             }

             break;

            case 2:
             //searchContact();

            default:
             puts("\n\tInvalid option, please try again.\n");
        }

    } else{
        puts("\nPlease enter more than zero.");
    }
} // main function


而且此代码不起作用,不知道为什么,注释中的@kutt添加了一个示例,该示例有效,但这不起作用,为什么?

当执行直接传递给&number的scanf()时,可以解决该问题。

最佳答案

如果在fgets%d前面添加空格,则不必使用%[^\n]。您还应该测试scanf的返回值以检查操作是否成功。

这是固定代码。

如果此代码,如果输入值不是数字,那么我会重复要求输入数字。

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

void main(){

    puts("\t+----------------------+\n\t| Contacts Application |\n\t+----------------------+\n");
    int noc; // number of contacts
    int cnt;
    do {
        printf(" How many contacts do you want to store: ");
        cnt = scanf("%d", &noc);
    } while (cnt != 1);


    if(noc>0){
        char name[noc][30];
        int  number[noc];
        int  a, b; // for counters in loop in switch

        for(int i = 0; i < noc; i++){
            printf("\n\tName %d: ", i+1);
            scanf(" %[^\n]", &name[i]);

            do {
                printf("\tNumber: ");
                cnt = scanf(" %d", &number[i]);
            } while (cnt != 1);
        }

        /** This will clear the screen **/
        #ifdef _WIN64
         system("cls");
        #elif __linux__
         system("clear");
        #endif

        puts("\n\tAll contacts have been saved successfully.\n");
        puts("\t1. Show all the contacts.");
        puts("\t2. Search any contact.\n");

        int choice;
        printf("\tEnter your choice: ");
        scanf("%d", &choice);

        switch(choice){
            case 1:

             for(int a = 0; a < noc; a++){
                printf("\n\t %d: %s - %d", a+1, name[a], number[a]);
             }

             break;

            case 2:
             //searchContact();

            default:
             puts("\n\tInvalid option, please try again.\n");
        }

    } else{
        puts("\nPlease enter more than zero.");
    }
} // main function

关于c - 如何在c中添加多个带空格的字符串?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59862951/

10-16 14:34