我如何将字符串和数字组合成一个scanf函数

问题描述 投票:0回答:3

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

#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);
    }

}
c input scanf
3个回答
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); }


0
投票
scanf中,如果在输入值时使用多个变量,则会将其分配给第一个给定变量。并且scanf仅用于一个变量。

Ex-:

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

输出

Enter numbers = 1 1 0 Enter number = 1 2 1 0

您可以看到它总是分配给第一个变量。

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

#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; }


0
投票
在scanf中,如果在输入值时使用多个变量,则将其分配给第一个给定变量。并且scanf仅用于一个变量。

Ex-:

int num1,num2;

printf(“输入数字=”);

scanf(“%d%d”,&num1,&num2);

printf(“%d \ n”,num1);

printf(“%d \ n”,num2);

输出

输入数字= 11个0

输入数字= 1 21个0

您可以看到它总是分配给第一个变量。

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

包括

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) {

© www.soinside.com 2019 - 2024. All rights reserved.