gpt4 book ai didi

c - 跳过我输入用户名的输出

转载 作者:行者123 更新时间:2023-11-30 14:48:32 26 4
gpt4 key购买 nike

我需要制作一本日记/计划表,我可以在其中使用用户名和密码登录,并设置登录名和帐户(如果您还没有的话)。我需要稍后能够使用它登录并访问我的日记。我可以在那里输入条目并保存它。这样当我退出程序并再次启动程序时,一旦登录,我就可以再次访问计划器。

所以我的问题是,为了设置我的帐户,我需要允许用户输入用户名和密码,但是当我启动程序时,它会跳过我可以输入用户名的部分并直接进入密码。代码:

#include <stdio.h>
#include <stdlib.h>
#define SIZE 1000

void signUp (char username [SIZE], char password [SIZE], char name [SIZE], int age) {
printf("Sign Up Procedure: \n");
printf("\nPlease enter your username: ");
gets(username);
printf("\nPlease enter a password: ");
gets(password);
printf("\nEnter your full name: ");
gets(name);
printf("\nEnter your age: ");
scanf("%d", &age);

printf("Username: %s, Password: %s, Full Name: %s, Age: %d", username, password, name, age);
}

void logIn(char username [SIZE], char password [SIZE]) {

}

int main() {
printf("1. Log In\n"); // Enter 1 for logging in
printf("2. Sign Up\n"); // Enter 2 for signing up
int choice;
scanf("%d", &choice);

char username [SIZE];
char password [SIZE];
char name [SIZE];
int age;

int keepGoing = 0;

while (keepGoing == 0){
switch (choice) {
case (1):
logIn (username, password);
keepGoing = 1;
break;
case (2):
signUp (username, password, name, age);
keepGoing = 1;
break;
default:
printf("Please enter a choice from the given options");
keepGoing = 0;
break;
}
}

FILE * pfile;

pfile = fopen("Planner.txt", "w");

fputs("Planner", pfile);

fclose(pfile);

return 0;
}

最佳答案

你的第一个scanf()

scanf("%d", &choice);

正在读取一个整数,因此它将换行符留在输入缓冲区中。

然后当您尝试获取用户名时:

gets(username);

它选取该换行符并将其分配给用户名。我建议阅读 C 输入以及为什么 scanf() 使用起来可能很危险且棘手,特别是在混合整数和字符串输入以及与其他 一起使用时>C 输入函数。

关于c - 跳过我输入用户名的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50342033/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com