C语言家庭收支软件
大家好,请大家帮帮忙!就是这个代码生成之后输入4退出的时候输入y和n都提示输入不正确,小白在此请教各位大神{:5_92:}
麻烦帮忙解答,谢谢!!!
#include<stdio.h>
#include<string.h>
struct MyFamilyAccount{
int flag;
char details;
double balance;
};
char key = ' ';
int loop=1;
char note="";
char temp="";
double money=0.0;
double choice=' ';
void pay(struct MyFamilyAccount*myFamilyAccount){
printf("本次支出金额:");
scanf("%lf",&money);
getchar();
if(money>(*myFamilyAccount).balance){
printf("\n余额不足:");
return;
}
printf("本次支出说明:");
scanf("%s",¬e);
getchar();
(*myFamilyAccount).balance -= money;
sprintf(temp,"\n\n支出\t%.2f\t\t%.2f\t\t\t%s",money, (*myFamilyAccount).balance,note);
strcat((*myFamilyAccount).details,temp);
(*myFamilyAccount).flag=1;
}
void income(struct MyFamilyAccount*myFamilyAccount){
printf("本次收入金额:");
scanf("%lf",&money);
getchar();
(*myFamilyAccount).balance += money;
printf("本次收入说明:");
scanf("%s",note);
getchar();
sprintf(temp,"\n\n收入\t%.2f\t\t%.2f\t\t\t%s",money,(*myFamilyAccount).balance,note);
strcat((*myFamilyAccount).details,temp);
(*myFamilyAccount).flag=1;
}
void showDetails(struct MyFamilyAccount*myFamilyAccount){
if((*myFamilyAccount).flag){
printf("%s",(*myFamilyAccount).details);
}else{
printf("没有任何收支明细,来一笔吧!");
}
}
void myExit() {
printf("\n你确定要退出吗? y/n");
do {
scanf("%c", &choice);
getchar();
if(choice == 'y'||choice == 'n') {
break;
}
printf("\n 你的输入有误,请重新输入 y/n");
}while(1);
if ( choice == 'y') {
loop = 0;}
}
void mainMenu(struct MyFamilyAccount*myFamilyAccount){
do{
printf("\n-----------------家庭收支记账软件-----------------");
printf("\n 1 收支明细");
printf("\n 2 登记收入");
printf("\n 3 登记收入");
printf("\n 4 退 出");
printf("\n请选择1--4:");
scanf("%c",&key);
getchar();
switch(key){
case'1':
showDetails(myFamilyAccount);
break;
case'2':
income(myFamilyAccount);
break;
case'3':
pay(myFamilyAccount);
break;
case'4':
myExit();
}
}while(loop);
printf("你退出了家庭收支软件");
getchar();
}
void main(){
struct MyFamilyAccount myFamilyAccount;
myFamilyAccount.flag = 0;
myFamilyAccount.balance = 1000.0;
memset(myFamilyAccount.details,3000,0);
strcpy(myFamilyAccount.details,"-----------------当前收支明细-----------------\n收支\t收支金额\t账户金额\t说明");
mainMenu(&myFamilyAccount);
getchar();
}
{:10_254:} 因为你的变量choice的类型定义错了,你定义的是double类型,而后面使用的时候一直当做char使用的。
应该定义成char
页:
[1]