自己编写的一个程序,不知道错在哪里了

pp25210 2011-03-20 10:58:06
#include<stdio.h>
void input();
void add();
void subtract();
void multiply();
void divide();
int main()
{
char ch;
printf("Enter the operation of your choice:");
printf("a.add s.subtract\n");
printf("b.multiply d.divide \nq.quit\n");
ch=getchar();
do
{
switch(getchar())
{
case 'a':add();
break;
case 's':subtract();
break;
case 'b':multiply();
break;
case 'd':divide();
default:
printf("input error,please input 'a' 's' 'b' 'd' 'q '\n");
}
}
while(ch=='q');
printf("bye.\n");
return 1;
}


void input()
{
float a[2];
int m,n;
printf("Enter first number:");
do
{
m=scanf("%f",&a[0]);
if(m==0)
{
printf("input errror!please enter an number such as 2.5,-2 or -1.788e2\n");
continue;
}
else
{
printf("Enter second number:");
n=scanf("%f",&a[1]);
if(n==0)
{
printf("input errror!please enter an number such as 2.5,-2 or -1.788e2\n");
continue;
}
}
while(m*n!=0);
}

void add()
{
printf("%f + %f =%f \n",a[0],a[1],a[0]+a[1]);
}


void subtract()
{
printf("%f - %f =%f \n",a[0],a[1],a[0]-a[1]);
}



void multiply()
{
printf("%f * %f =%f \n",a[0],a[1],a[1]*a[0]);
}


void divide()
{
while(a[1]==0)
{
printf("enter a number other than 0:");
scanf("%f",&a[1]);
}
printf("%f / %f =%f \n",a[0],a[1],a[1]/a[0]);
}
根据用户输入进行加,减,乘,除运算。
...全文
196 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
pp25210 2011-03-21
  • 打赏
  • 举报
回复
我调试了,可是看不懂错误提示
AnYidan 2011-03-21
  • 打赏
  • 举报
回复
楼主可以单步跟踪一下,看看问题出在哪里
sfd1234 2011-03-21
  • 打赏
  • 举报
回复
楼主,你不能自己一步一步调试?调试是必须的,再NB的NB也不敢说不用调试的!
ineedpower 2011-03-21
  • 打赏
  • 举报
回复

int a;
ineedpower 2011-03-21
  • 打赏
  • 举报
回复
[code = C/C++]
int a;
[/code]
ineedpower 2011-03-21
  • 打赏
  • 举报
回复
[code = c/c++]
int a
[/code]
thelover 2011-03-21
  • 打赏
  • 举报
回复
天那,你完全就是在靠自己的臆想在编程。仔细的看一本书吧!
qwangwei1231 2011-03-21
  • 打赏
  • 举报
回复
楼主自己对照下吧!
测试可以通过!你试下


// TEST11.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<stdio.h>
void input();
void add();
void subtract();
void multiply();
void divide();

float a[2]; //必须定义为全局变量

int main()
{
char ch;

while(1)
{
printf("Enter the operation of your choice:\n");
printf("a.add s.subtract\n");
printf("b.multiply d.divide \nq.quit\n");
fflush(stdin);
ch=getchar();
if(ch == 'q')
break;
input();
switch(ch)
{
case 'a':add();
break;
case 's':subtract();
break;
case 'b':multiply();
break;
case 'd':divide();
break; //这少了个Break
default:
printf("input error,please input 'a' 's' 'b' 'd' 'q '\n");
}
}
printf("bye.\n");
return 1;
}


void input()
{

int m,n;
printf("Enter first number:");
do
{
m=scanf("%f",&a[0]);
if(m==0)
{
printf("input errror!please enter an number such as 2.5,-2 or -1.788e2\n");
continue;
}
else
{
printf("Enter second number:");
n=scanf("%f",&a[1]);
if(n==0)
{
printf("input errror!please enter an number such as 2.5,-2 or -1.788e2\n");
continue;
}
}

}while(m*n==0);
}
void add()
{
printf("%f + %f =%f \n",a[0],a[1],a[0]+a[1]);
}


void subtract()
{
printf("%f - %f =%f \n",a[0],a[1],a[0]-a[1]);
}



void multiply()
{
printf("%f * %f =%f \n",a[0],a[1],a[1]*a[0]);
}


void divide()
{
while(a[1]==0)
{
printf("enter a number other than 0:");
scanf("%f",&a[1]);
}
printf("%f / %f =%f \n",a[0],a[1],a[1]/a[0]);
}
heartgoon2010 2011-03-21
  • 打赏
  • 举报
回复
input()函数都没调用,参与运算的数是怎么输入的?仔细琢磨琢磨吧
無_1024 2011-03-20
  • 打赏
  • 举报
回复
ls的也是其中之一
还有就是一个编程习惯
LinuxBirdMan 2011-03-20
  • 打赏
  • 举报
回复
应该是:
while(ch!='q');

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧