gpt4 book ai didi

c - C 中的嵌套 if 语句 - 为什么它不评估最后一个 else if?

转载 作者:行者123 更新时间:2023-12-04 16:36:53 25 4
gpt4 key购买 nike

当您分配给 choice3 时,以下代码不会执行最后一个 else if 语句。

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

int main() {
puts("Specify with a number what is that you want to do.");
puts("1. Restore wallet from seed.");
puts("2. Generate a view only wallet.");
puts("3. Get guidance on the usage from within monero-wallet-cli.");

unsigned char choice;
choice = getchar();

if ( choice == '1' ) {
system("nice -19 ~/monero-x86_64-linux-gnu-v0.17.2.0/monero-wallet-cli --testnet --restore-deterministic-wallet");
exit(0);
}
else if ( choice == '2' ) {
system("nice -19 ~/monero-x86_64-linux-gnu-v0.17.2.0/monero-wallet-cli --testnet --generate-from-view-key wallet-view-only");
exit(0);
}
else if ( choice == '3' ) {
puts("Specify with a number what is that you want to do.");
puts("1. Get guidance in my addresses and UTXOs");
puts("2. Pay");
puts("3. Get guidance on mining.");

unsigned char choicetwo = getchar();
if ( choicetwo == '1' ) {
printf("Use \033address all\033 to get all your addresses that have any balance, or that you have generated at this session.");
printf("Use \033balance\033 to get your balance");
printf("Use \033show_transfers\033 to get ");
printf("Use \033show_transfers\033 out to get ");
printf("Use \033show_transfers in\033 to get your balance");
}
}

return 0;
}

当我输入 3 时,我得到以下输出:

Specify with a number what is that you want to do.
1. Restore wallet from seed.
2. Generate a view only wallet.
3. Get guidance on the usage from within monero-wallet-cli.
3
Specify with a number what is that you want to do.
1. Get guidance in my addresses and UTXOs
2. Pay
3. Get guidance on mining.

我真的被阻止了,缺少了一些东西,我不知道为什么它不继续第二次从用户那里获取输入。

最佳答案

当您第一次输入“3”时,您实际上输入了两个字符:字符'3' 和一个换行符。第一个 getchar 函数从输入流中读取“3”,第二个函数读取换行符。

接受第一个输入后,您需要循环调用 getchar,直到读取换行符以清除输入缓冲区。

choice = getchar();
while (getchar() != '\n');

关于c - C 中的嵌套 if 语句 - 为什么它不评估最后一个 else if?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68866548/

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