小编Bel*_*sem的帖子

为什么在第二个循环中不进行for循环打印?

我做了一个for循环,将名称存储在用户选择其大小的数组中,但是当for循环运行时,它会跳过第二条printf语句。

int NumToDelete;
printf("How much employees do you want to remove?\n");
scanf(" %d", &NumToDelete);
char Name[NumToDelete][25];
for(int i = 0; i < NumToDelete; i++)
{
    fgetc(stdin);      //To stop the program from doing
    printf("Name: ");  //something like this: Name:Name:
    fgets(Name[i], 25, stdin);
}
Run Code Online (Sandbox Code Playgroud)

提示和用户输入应如下所示(如果NumToDelete为3):

Name: Ahmed
Name: John
Name: Bob
Run Code Online (Sandbox Code Playgroud)

但是,相反,在输入名称“ Ahmed”之后,必须输入第二个名称“ John”,然后代码再次显示“名称:”提示。因此,控制台中的文本最终看起来像这样:

Name: Ahmed
John
Name: Bob
Run Code Online (Sandbox Code Playgroud)

名称是用户输入。先感谢您。

c arrays string stdin for-loop

3
推荐指数
1
解决办法
88
查看次数

为什么我的整数数组显示随机值?

我试图制作一个存储有关员工信息的程序,但是当我尝试将薪水存储在那里时,输出是一个随机整数。

在将原始文件Salary不是数组)转为之前,代码可以完美工作Salary[i],我能够计算出总工资(totSlry

int NumofEmpo;
printf("How much Employees do you want to add?\n");
scanf(" %d", &NumofEmpo); 

char Name[25], Service[20];                //All the variables that the user
int DOB, Salary[NumofEmpo], totSlry = 0;   //info will get stored in

    for(int i = 0; i != NumofEmpo; i++)
    {
        fgetc(stdin);

        printf("Name: ");
        fgets(Name, 25, stdin);
        printf("Service: ");
        fgets(Service, 20, stdin);
        printf("Date of Birth: ");
        scanf("%d", &DOB);
        printf("Salary: ");
        scanf("%d", &Salary[i]);
        printf("\n");
        totSlry = totSlry + Salary[i];

        FILE …
Run Code Online (Sandbox Code Playgroud)

c arrays pointers

1
推荐指数
1
解决办法
74
查看次数

如果有陈述,有什么方法可以缩短此时间吗?

我有非常大的if语句,看起来不那么令人愉悦,而且效率也不高。

我正在制作一个程序(取决于用户输入)运行某个代码块,例如,如果用户输入'a'该程序运行一个向文件中添加某些内容的代码,等等。声明(我不知道这是否相关)。

else if(ansr != 'a' && ansr != 'A' && ansr != 'r' && ansr != 'R' && ansr != 's' && ansr != 'S' && ansr != 'e' && ansr != 'E')
{
    printf("Invalid input!\n");
}
Run Code Online (Sandbox Code Playgroud)

如您所见,这是一个很长的if语句,我希望它要短一些。谢谢

c if-statement char logical-operators

1
推荐指数
1
解决办法
141
查看次数

Why doesnt the OR logical operator( || ) work?

I tried to use the OR logical operator in a do-while statement but for some reason it wouldn't work.

它不使用OR逻辑运算符(仅使用一条语句),否则不起作用。

int main()
{
    char ansr;

    do
    {
        printf("What do you want to do?\n");
        printf("A = Add Employee\nR = Remove Employee\nE = Exit\n");
        scanf(" %c", &ansr);
    }while(ansr != 'E'||ansr != 'e');

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

每当我写“ E”或“ e”时,我都希望程序退出while循环,但由于某种原因,它将继续执行do-while语句。

c logical-operators

0
推荐指数
1
解决办法
82
查看次数

标签 统计

c ×4

arrays ×2

logical-operators ×2

char ×1

for-loop ×1

if-statement ×1

pointers ×1

stdin ×1

string ×1