gpt4 book ai didi

c++ - if else 字符逻辑有缺陷

转载 作者:太空宇宙 更新时间:2023-11-04 01:31:30 25 4
gpt4 key购买 nike

好的,我现在尝试使用 check_status() 下面的函数检查代码。

它们在代码中没有错误,但我认为我搞砸了逻辑。当它运行时我想做的是放在 S 中并打印“CORRECT ANSWER.S”

现在,当您输入 FEFWRGV 或不是 S、SH、MJ、MS 的内容时,代码可以正常工作并打印出错误消息。然而!当你输入 SSDFIEWF 或类似的东西时,它会显示“CORRECT ANSWER.S”

我检查是否输入了 S 的方法是如果(状态[0] == 'S')

有没有办法让我写出如下代码:如果(状态[0] == 'S' && 状态[1] == 无效)

因为我知道我正在做的是检查 status[0],然后我想确保 status[1] 没有被使用。

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

//functions called
float wages_loop();
float other_loop();
float interest_loop();
void check_status();


//start main
int main(void)
{
char status[10], another[10];
char buffer[80];

float wages, other_income, interest, dividends, test;
int dependents;
int single_acc = 0, mj_acc = 0, ms_acc = 0, sh_acc = 0;

printf("Would you like to start: ");
gets_s(another);

if (another[0] != 'y' && another[0] != 'n')
{
while (another[0] != 'y' && another[0] != 'n')
{
printf("\n\n INCORRCT ANSWER. \n\n");
printf("\n Would you like to start. (y or n)");
gets_s(another);
}
}
while (another[0] == 'y')
{


check_status();


interest = 0;


printf("\n\n\t\t Your interest is: %.2f \n", interest);
system("pause");

printf("Would you like anoter: ");
gets_s(another);

if (another[0] != 'y' && another[0] != 'n')
{
while (another[0] != 'y' && another[0] != 'n')
{
printf("\n\n INCORRCT ANSWER. \n\n");
printf("\n Would you like anoter. (y or n)");
gets_s(another);
}
}

} //end loop

printf("\n\n\t\t\t Number of Singles filleing: %i \n", single_acc);

return 0;

}//end main



void check_status()
{
char status[10];
int check = 0;

while (check != 1)
{
printf("What is your Status: ");
gets_s(status);

if (status[0] == 'S' && status[1] == 'H')
{
printf("\n\n CORRECT ANSWER. SH \n\n");
check = 1;
}
else if (status[0] == 'S')
{
printf("\n\n CORRECT ANSWER. S \n\n");
check = 1;
}
else if (status[0] == 'M' && status[1] == 'J')
{
printf("\n\n CORRECT ANSWER. MJ \n\n");
check = 1;
}
else if (status[0] == 'M' && status[1] == 'S')
{
printf("\n\n CORRECT ANSWER. MS \n\n");
check = 1;
}
else
{
printf("\n\n INCORRECT ANSWER. noting \n\n");
}
}
}

最佳答案

Is there a way i can write a code like: if (status[0] == 'S' && status1 == void)

您快完成了。 C-“string”-终止符不是 void 而是 '\0'

因此,要测试“字符串”是否在第 1 个st 字符之后结束,即 status[0],请执行以下操作:

if (status[0] == 'S' && status[1] == '\0') 

顺便说一句:要声明一个不带任何参数的函数,请使用 void,如下所示:

check_status(void);

作为Joachim Pileborg已经对您的问题发表了评论,您使用的 gets_s() 不正确。 gets_s() takes two parameters .

编译器应该警告您缺少第二个参数。

认真对待此类警告是个好主意。

关于c++ - if else 字符逻辑有缺陷,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21872951/

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