好的,所以我在这里遇到了一个小问题。 不要介意他们为我提供的代码中的注释。
我的问题在于功能。我希望它进行测试,以确保再次[0] = y或n。如果它不循环,直到我输入正确的数字。
现在它做了什么:它无休止地循环,无论我投入什么。
我确实错过了一些东西,我确信它确实如此。
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <cstdio>
//functions called
float wages_loop();
//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);
while (another[0] = 'y')
{
//printf("What is your Status: ");
//gets_s(status);
wages = wages_loop();
//printf("\n How much in Other Income. ");
//gets_s(buffer);
//other_income = atof(buffer);
//printf("\n How much in interest. ");
//gets_s(buffer);
//interest = atof(buffer);
//printf("\n How much in Dividends. ");
//gets_s(buffer);
//dividends = atof(buffer);
//printf("\n How many Dependents. ");
//gets_s(buffer);
//dependents = atoi(buffer);
printf("\n\n\t\t Your wage is: %.2f \n", wages);
system("pause");
} //end loop
printf("\n\n\t\t\t Number of Singles filleing: %i \n", single_acc);
return 0;
}//end main
float wages_loop()
{
char again[10];
char buffer[80];
float wages, total_wages = 0;
printf("\n How much in Wages. ");
gets_s(buffer);
wages = atof(buffer);
total_wages = wages + total_wages;
printf("\n Do you have any more wages. (y or n)");
gets_s(again);
if (again[0] != 'y' || 'n')
{
while (again[0] != 'y' || 'n')
{
printf("\n\n INCORRCT ANSWER. \n\n");
printf("\n Do you have any more wages. (y or n)");
gets_s(again);
}
}
while (again[0] = 'y')
{
printf("\n Enter Wages: ");
gets_s(buffer);
wages = atof(buffer);
total_wages = wages + total_wages;
printf("\n Do you have any more wages. ");
gets_s(again);
}
return total_wages;
}
答案 0 :(得分:9)
while (another[0] = 'y')
这是作业,而不是平等。将=
更改为==
while (again[0] = 'y')
此外:
(again[0] != 'y' || 'n')
应该是
(again[0] != 'y' && again[0] != 'n')
因为'n'
本身将始终返回true
(感谢Jonathan Henson)
EDIT2:正如Sam在评论中指出的那样,你没有在循环中设置another[0]
,所以即使你将运算符更改为{{{}也是如此1}},添加一个语句再次获取用户输入。
EDIT3:当log0指出时,您可以通过调高编译器的警告级别来避免此问题。