【问题标题】:How do I combine a string and a number together into one scanf function如何将字符串和数字组合成一个 scanf 函数
【发布时间】:2019-10-14 15:59:53
【问题描述】:

当我向用户询问地址时,每次输入数字时,我的程序都会结束,我意识到每次输入字符时它都会继续运行,但是当我同时输入字符串和数字时,它会自动结束

#include <stdio.h>

int main() 
{
    int createAccount;
    char firstName[10];
    char lastName [10];
    char address[20];
    char city[15];
    int zip;
    int existingUser;
    int customerSupport;
    int pendingStatements;
    char userName[10];
    printf("1. Create Account\n");
    printf("2. Login to existing user\n");
    printf("3. Customer support\n");
    printf("4. Check pending statements \n");
    printf("Enter: ");

    scanf("%d, %d, %d, %d", &createAccount, &existingUser, &customerSupport, &pendingStatements);

    if (createAccount == 1)
    {
        printf("Name: ");
        scanf("%s", firstName);

        printf("Last Name: ");
        scanf("%s", lastName);

        printf("Address:  ");
        scanf("%s", address);

        printf("City: ");
        scanf("%s", city);

        printf("Zip: ");
        scanf("%d", &zip);
    }
    else if (existingUser == 2)
    {
        printf("Username: ");
        scanf("%s", userName);
    }

}

【问题讨论】:

  • 创建 scanf() 是为了读取 f 格式化数据(扫描f)。用户输入与格式化相去甚远。首选fgets()(可能后跟sscanf())作为用户输入。

标签: c input scanf


【解决方案1】:

您的第一个 scanf() 正在尝试读取以逗号分隔的 4 个数字。但用户一次只能输入一个菜单选项,而不是 4 个不同的数字。因此,剩余的scanf() 调用均无效。

int choice;
scanf("%d", &choice);
if (choice == 1) {
    ...
} else if (choice == 2) {
    ...
} else if (choice == 3) {
    ...
} else if (choice == 4) {
    ...
} else {
    printf("Invalid choice %d\n", choice);
}

【讨论】:

  • 你好,我做了你所做的,每次我输入第一个 if 语句的地址时仍然有问题,每次我输入一个数字时它总是结束,例如 123 street
  • %s 只处理一个单词,而不是整行。您应该使用fgets() 阅读整行。
  • 如果你在单词之间放置空格,它就不起作用了。 thre 使用 , 代替空格
  • fgets 不起作用,用逗号代替空格是什么意思?
  • 查看stackoverflow.com/questions/5918079/… 了解如何混合fgets()scanf()。我不知道他说的逗号是什么意思。
【解决方案2】:

scanf 中,如果您在输入第一个值时使用多个变量,它将分配给第一个给定变量。在良好的编码scanf 中仅使用一个变量。

前-:

int num1, num2;
printf("Enter numbers = ");
scanf("%d %d", &num1, &num2);

printf("%d\n", num1);
printf("%d\n", num2);

输出

Enter numbers = 1
//It shows blank space it means you have to enter second number
1
2

//You can input two numbers by puting spaces among them. Then it will assign given variables
Enter number = 1 2
1
2

您可以看到第一个输入的值总是分配给第一个变量。因为我们不需要多个变量来完成一项工作。只使用一个变量就可以了

在您的情况下,您必须使用一个变量来检查状态

#include <stdio.h>

int main() 
{

    int choose = 0;
    char firstName[10];
    char lastName [10];
    char address[20];
    char city[15];
    int zip;
    char userName[10];

    printf("1. Create Account\n");
    printf("2. Login to existing user\n");
    printf("3. Customer support\n");
    printf("4. Check pending statements \n");
    printf("Enter: ");

    scanf("%d", &choose);


    if (choose== 1)
    {
        printf("Name: ");
        scanf("%s", firstName);

        printf("Last Name: ");
        scanf("%s", lastName);

        printf("Address:  ");
        scanf("%s", address);

        printf("City: ");
        scanf("%s", city);

        printf("Zip: ");
        scanf("%d", &zip);
    }
    else if (choose== 2)
    {
        printf("Username: ");
        scanf("%s", userName);

    }
    else if (choose== 3)
    {
        printf("..........");
    }
    else if (choose== 4)
    {
        printf("..........");
    }
    else
    {
        printf("Error number\n");
    }

    return 0;

}

【讨论】:

    【解决方案3】:
    #include <stdio.h>
    
    int main() 
    {
    
    //int createAccount;
    char firstName[10];
    char lastName [10];
    //int address;
    char address[20];
    char city[15];
    int zip;
    char password[10];
    int choice;
    
    
    
    // int existingUser;
    // int customerSupport;
    // int pendingStatements;
    char userName[10];
    printf("1. Create Account\n");
    printf("2. Login to existing user\n");
    printf("3. Customer support\n");
    printf("4. Check pending statements \n");
    printf("Enter: ");
    scanf("%d", &choice);
    
    if (choice == 1)
    {
        printf("Name: ");
        scanf("%s", firstName);
    
        printf("Last Name: ");
        scanf("%s", lastName);
    
        printf("Address:  ");
        scanf("%s", address);
    
        printf("City: ");
        scanf("%s", city);
    
        printf("Zip: ");
        scanf("%d", &zip);
    }
    else if (choice == 2)
    {
        printf("Username: ");
        scanf("%s", userName);
        printf("Password: ");
        scanf("%s", password);
    }
    
    
    }
    

    【讨论】:

    • 我解决了这个问题,但我的新问题是当我输入数字和字符串时询问地址时的第一个 if 语句,它会自动停止它(例如:123 street)但是当我刚输入时(例如:街)它继续我现在想弄清楚如何输入一个字符串和一个数字
    • 如果你在单词之间放置空格,它就不起作用。 thre 使用 , 代替空格
    • 你的意思是 put ,而不是?所以我必须做 123,街道?
    • 使用123,street
    • 好的,谢谢你的回答我只是添加了另一个占位符,所以用户可以在上面添加数字
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-30
    • 1970-01-01
    • 2013-12-25
    相关资源
    最近更新 更多