如果三个if语句中的任何一个为真,则尝试退出程序,但仅在处理完所有三个语句之后才尝试退出程序。我已经通过多种方式对其进行了格式化,但我只能在每个语句后退出,而不是在所有三个语句之后退出。所以基本上它将继续使用其余的函数,即使其中两个if语句都是真的。
int getValues(float* loan, float* rate, int* years)
{
//Prompt the user for the loan amount, the yearly interest rate, and the loan length in years, then make sure each value is large enough
printf("Enter a loan amount (dollars and cents): ");
scanf("%f", loan);
printf("Enter a yearly interest rate (ex. 2.5 for 2.5%): ");
scanf("%f", rate);
printf("Enter the loan length in years (integer number): ");
scanf("%d", years);
if (*loan < 1)
printf("\nStarting amount must be at least one dollar.\n");
if (*rate < .1)
printf("Interest rate must be at least .1%.\n");
if (*years < 1)
{ printf("Number of years must be at least 1.\n");
exit (100);
}
return(0);
}
这只是最近的一次尝试。我对编程非常陌生,并且在这个小程序上花费了无数个小时。
答案 0 :(得分:1)
您需要添加一个标志变量,您将在每个system.file("sql/myquery.sql",package = "mypackage")
的正文中设置该变量。然后你会检查标志和if
是否已设置。
exit
答案 1 :(得分:1)
使用标志,这是一种简单的方法,可以在您不想立即采取行动时跟踪特定条件。
int flag = 0;
if (*loan < 1)
{
printf("\nStarting amount must be at least one dollar.\n");
flag = 1;
}
if (*rate < .1)
{
printf("Interest rate must be at least .1%.\n");
flag = 1;
}
if (*years < 1)
{
printf("Number of years must be at least 1.\n");
flag = 1;
}
if(flag == 1)
{
exit(100)
}
答案 2 :(得分:1)
我有一个初始化为false的布尔变量,然后当一个条件被命中时,设置为true。然后在最后进行简单的布尔检查。
int getValues(float* loan, float* rate, int* years)
{
int shouldExit = 0;
//Prompt the user for the loan amount, the yearly interest rate, and the loan length in years, then make sure each value is large enough
printf("Enter a loan amount (dollars and cents): ");
scanf("%f", loan);
printf("Enter a yearly interest rate (ex. 2.5 for 2.5%): ");
scanf("%f", rate);
printf("Enter the loan length in years (integer number): ");
scanf("%d", years);
if (*loan < 1){
shouldExit = 1;
printf("\nStarting amount must be at least one dollar.\n");
}
if (*rate < .1){
shouldExit = 1;
printf("Interest rate must be at least .1%.\n");
}
if (*years < 1)
{ printf("Number of years must be at least 1.\n");
shouldExit = 1;
}
if (shouldExit == 1){
exit (100);
}
return(0);
}
答案 3 :(得分:0)
int errors = 0;
if (*loan < 1) {
printf("\nStarting amount must be at least one dollar.\n");
errors = 1;
}
..
像上面设置的变量错误或任何变为1.后来检查并退出。
..