- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要为我的第一学期 c 作业编写一个函数。如果有人可以帮助我,我已经写好了所有作业,我只需要验证用户输入。这是老师给我的...如果这还不够,我可以发布更多内容
double get_double(void)
必须使用函数来验证每项费用的输入和收入。它将不断提示用户输入数值,直到满足要求状况。值必须是数字且大于或等于0。如果输入任何字符在数字之前或之后,输入将被视为无效。
#include <stdio.h>
#include <conio.h>
double get_expenses(double* pSchool, double* pLiving, double* pTransp, double* pOther); /* function prototype */
double get_income(double* pEmploy, double* pOther); /* function prototype */
double display_report(double School, double Living, double Transp, double OtherExp, double Employ, double OtherInc); /* function prototype */
char get_char(char* pRecalculate); /* function prototype */
void clear_buffer(void); /* function prototype */
double get_double(void); /* function prototype */
main() {
double school, living, transp, otherExp;
double employ, otherInc;
char recalculate;
int counter = 1;
while (counter > 0){
printf("Student Budget Planner\n");
printf("~~~~~~~~~~~~~~~~~~~\n");
printf("Input Set: %d\n\n", counter);
get_expenses(&school, &living, &transp, &otherExp);
get_income(&employ, &otherInc);
display_report(school, living, transp, otherExp, employ, otherInc);
clear_buffer();
get_char(&recalculate);
if (recalculate == 'Y' || recalculate == 'y'){
counter = counter + 1;
} else {
counter = 0;
}
}
return 0;
}
double get_expenses(double* pSchool, double* pLiving, double* pTransp, double* pOther) {
*pSchool = *pLiving = *pTransp = *pOther = 0;
double tuition, textbooks, supplies, rent, utilities, phone, groceries;
double entertainment, transportation, car, insurance, gas, other;
printf("School Expenses:\n");
printf("~~~~~~~~~~~~~~\n");
printf("Tuition (per semester): ");
scanf("%lf", &tuition);
printf("\nTextbooks(per semester): ");
scanf("%lf", &textbooks);
printf("\nSupplies: ");
scanf("%lf", &supplies);
*pSchool = (tuition / 4) + (textbooks / 4) + supplies;
printf("\n\nLiving Expenses:\n");
printf("~~~~~~~~~~~~~\n");
printf("Residence/Rent/Mortgage: ");
scanf("%lf", &rent);
printf("\nUtilities: ");
scanf("%lf", &utilities);
printf("\nPhone/Internet: ");
scanf("%lf", &phone);
printf("\nGroceries/Eating out: ");
scanf("%lf", &groceries);
printf("\nEntertainment: ");
scanf("%lf", &entertainment);
*pLiving = rent + utilities + phone + groceries + entertainment;
printf("\n\nTransportation: \n");
printf("~~~~~~~~~~~~~\n");
printf("Public Transportation: ");
scanf("%lf", &transportation);
printf("\nCar: ");
scanf("%lf", &car);
printf("\nAuto Insurance: ");
scanf("%lf", &insurance);
printf("\nGas/Maintenance: ");
scanf("%lf", &gas);
*pTransp = transportation + car + insurance + gas;
printf("\n\nOther: \n");
printf("~~~~~\n");
printf("Any other expenses: ");
scanf("%lf", &other);
*pOther = other;
return *pSchool, *pLiving, *pTransp, *pOther;
}
double get_income(double* pEmploy, double* pOther){
*pEmploy = *pOther = 0;
double wages, family, scholarship, other;
printf("\n\nEmployment income:\n");
printf("~~~~~~~~~~~~~~~~~\n");
printf("Expected Wages/Tips: ");
scanf("%lf", &wages);
*pEmploy = wages;
printf("\n\nOther income:\n");
printf("~~~~~~~~~~~\n");
printf("Family Support: ");
scanf("%lf", &family);
printf("\nScholarship/Bursaries (per semester): ");
scanf("%lf", &scholarship);
printf("\nOther: ");
scanf("%lf", &other);
*pOther = family + scholarship + other;
return *pEmploy, *pOther;
}
double display_report(double School, double Living, double Transp, double OtherExp, double Employ, double OtherInc) {
printf("\n\nStudent Name Budget Report\n");
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("Expenses:\n");
printf("~~~~~~~~\n");
printf(" MONTHLY ANNUALY\n");
printf("School Expenses: $%.2lf $%.2lf\n", School, School * 12);
printf("Living Expenses: $%.2lf $%.2lf\n", Living, Living * 12);
printf("Transportation Expenses: $%.2lf $%.2lf\n", Transp, Transp * 12);
printf("Other Expenses: $%.2lf $%.2lf\n", OtherExp, OtherExp * 12);
printf("total Expenses: $%.2lf $%.2lf\n", School + Living + Transp + OtherExp, School * 12 + Living * 12 + Transp * 12 + OtherExp * 12);
printf("Income:\n");
printf("~~~~~~~\n");
printf("Employment Income: $%.2lf $%.2lf\n", Employ, Employ * 12);
printf("Other Income: $%.2lf $%.2lf\n", OtherInc, OtherInc * 12);
printf("Total Income: $%.2lf $%.2lf\n", Employ + OtherInc, Employ * 12 + OtherInc * 12);
printf("Total Savings Available for your Goals: $%.2lf $%.2lf\n", (Employ + OtherInc) - (School + Living + Transp + OtherExp), (Employ * 12 + OtherInc * 12)- (School * 12 + Living * 12 + Transp * 12 + OtherExp * 12));
return 0;
}
char get_char(char* pRecalculate){
int valid_input;
valid_input = 0;
while (valid_input == 0){
printf("\nWould you like to recalculate your budget? (Y/N): ");
scanf("%c", &*pRecalculate);
*pRecalculate = toupper(*pRecalculate);
if (*pRecalculate == 'Y' || *pRecalculate == 'y' || *pRecalculate == 'N' || *pRecalculate == 'n') valid_input = 1;
else printf("Error Invalid choice\n");
printf("\n");
}
return *pRecalculate;
}
void clear_buffer(void) {
while ( getchar() != '\n' );
}
double get_double()
{
// What to put here?
}
最佳答案
考虑流程序列,并考虑您希望程序或函数遵循的流程图/决策树。
在输入有效的输入之前,您不想停止询问用户输入。您保证至少询问用户一次,但也可能询问多次。输入有效或无效。
现在我们开始起草一些伪代码。我会这样做:
double get_double(void)
{
double d;
int ask_again = 1; /* continue if 1 (true), exit if 0 (false) */
do {
/* display message to user to let them know to type a number */
/* read user input */
if (/* user input didn't input a valid double */)
{
/* display appropriate error message */
}
else if (/* user entered a double, but it wasn't greater than or equal to zero */)
{
/* display appropriate error message */
}
else /* user entered valid input, so ... */
{
/* ... do something to make sure the loop exists */
}
} while (/* we need to ask again */);
}
这对我来说是 do-while,所以我把它写在 do-while 循环中。我提供了一个 int Ask_again
变量,但我将它留给您,它可能有用(如果有的话)。
您应该考虑所有注释所在的地方应该包含哪些真正的代码。
检查你的类(class)笔记,看看你的老师希望你知道哪些技巧可以在这里应用。
在所有计算机语言中,读取和清理用户输入都是一个臭名昭著的问题,尤其是 C。StackOverflow 可以让您了解如何执行特定操作。
关于c - 如何在 C 中创建一个验证用户输入的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11495816/
在为 Web 应用程序用例图建模时,为用户可以拥有的每个角色创建一个角色是否更好?或拥有一个角色、用户和一个具有特权的矩阵? guest < 用户 < 版主 < 管理员 1: guest 、用户、版主
我无法使用 Elixir 连接到 Postgres: ** (Mix) The database for PhoenixChat.Repo couldn't be created: FATAL 28P
这个问题已经有答案了: Group by field name in Java (7 个回答) 已关闭 7 年前。 我必须编写一个需要 List 的方法并返回 Map> . User包含 Person
感谢您的帮助,首先我将显示代码: $dotaz = "Select * from customers JOIN contracts where customers.user_id ='".$_SESS
我只想向所有用户中的一个用户显示一个按钮。我尝试了 orderByKey() 但没有成功! 用户模型有 id 成员,我尝试使用 orderByChild("id") 但结果相同! 我什至尝试了以下技巧
我们在工作中从 MongoDB 切换到 Postgres,我正在建立一个 BDR 组。 在这一步,我正在考虑安全性并尽可能锁定。因此,我希望设置一个 replication 用户(角色)并让 BDR
export class UserListComponent implements OnInit{ users; constructor(private userService: UserS
我可以使用 Sonata User Bundle 将 FOS 包集成到 sonata Admin 包中。我的登录功能正常。现在我想添加 FOSUserBundle 中的更改密码等功能到 sonata
在 LinkedIn 中创建新应用程序时,我得到 4 个单独的代码: API key 秘钥 OAuth 用户 token OAuth 用户密码 我在 OAuth 流程中使用前两个。 的目的是什么?最后
所以..我几乎解决了所有问题。但现在我要处理另一个问题。我使用了这个连接字符串: SqlConnection con = new SqlConnection(@"Data Source=.\SQLEX
我有一组“用户”和一组“订单”。我想列出每个 user_id 的所有 order_id。 var users = { 0: { user_id: 111, us
我已经为我的Django应用创建了一个用户模型 class User(Model): """ The Authentication model. This contains the u
我被这个问题困住了,找不到解决方案。寻找一些方向。我正在用 laravel 开发一个新的项目,目前正致力于用户认证。我正在使用 Laravels 5.8 身份验证模块。 对密码恢复 View 做了一些
安装后我正在使用ansible配置几台计算机。 为此,我在机器上本地运行 ansible。安装中的“主要”用户通常具有不同的名称。我想将该用户用于诸如 become_user 之类的变量. “主要”用
我正在尝试制作一个运行 syncdb 的批处理文件来创建一个数据库文件,然后使用用户名“admin”和密码“admin”创建一个 super 用户。 到目前为止我的代码: python manage.
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 6 年前。 Improv
我已在 Azure 数据库服务器上设置异地复制。 服务器上运行的数据库之一具有我通过 SSMS 创建的登录名和用户: https://learn.microsoft.com/en-us/azure/s
我有一个 ionic 2 应用程序,正在使用 native FB Login 来检索名称/图片并将其保存到 NativeStorage。流程是我打开WelcomePage、登录并保存数据。从那里,na
这是我的用户身份验证方法: def user_login(request): if request.method == 'POST': username = request.P
我试图获取来自特定用户的所有推文,但是当我迭代在模板中抛出推文时,我得到“User”对象不可迭代 观看次数 tweets = User.objects.get(username__iexact='us
我是一名优秀的程序员,十分优秀!