📄 subject_44571.htm
字号:
<p>
序号:44571 发表者:anhongbo 发表日期:2003-06-21 11:00:07
<br>主题:我刚VC++6,现在正在学SWITCH,发现了一个问题,请帮帮我!:)
<br>内容:# include <stdio.h><BR><BR>void main()<BR>{<BR> char op;<BR> int n1;<BR> int n2;<BR> int result;<BR><BR> printf("enter the number n1:");<BR> scanf("%d",&n1);<BR><BR> printf("enter the number n2:");<BR> scanf("%d",&n2);<BR><BR> printf("enter operator:");<BR> scanf("%c",&op);<BR><BR> switch(op)<BR> {<BR> case'+': result = n1 +n2;<BR> printf("result of the operation is:%d",result);<BR> break;<BR> case'-': result = n1 - n2;<BR> printf("result of the operation is:%d",result);<BR> break;<BR> case'*': result = n1 * n2;<BR> printf("result of the operation is:%d",result);<BR> break;<BR> case'/': result = n1 / n2;<BR> printf("result of the operation is:%d",result);<BR> break;<BR> case'%': result= n1 % n2;<BR> printf("result of the operation is:%d",result);<BR> break;<BR> default:<BR> printf("Wrong Operator!!\n");<BR> }<BR>}<BR>这个本来是选择语句,输入什么符号就进行什么运算,但是我每次运行到printf("enter operator:");<BR> scanf("%c",&op);<BR>这里的时候就直接打印出(Wrong Operator!!)??请问各位高手,到底是哪里出了毛病??谢谢!<BR>
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
回复者:罗兹维尔 回复日期:2003-06-21 11:52:27
<br>内容:scanf()的问题,用iostream的cout与cin<BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:anhongbo 回复日期:2003-06-21 16:27:21
<br>内容:请问楼上的:“iostream的格式是什么?”谢谢!
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:罗兹维尔 回复日期:2003-06-21 19:06:08
<br>内容:#include <iostream><BR><BR>using namespace std;<BR><BR>void main()<BR>{<BR> int i;<BR> char ch;<BR> cout<<"enter the number n1:";<BR> cin>>i;<BR> cout<<"enter operator:";<BR> cin>>ch;<BR>}<BR><BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:e先生 回复日期:2003-06-22 09:29:59
<br>内容:你用的这些是c语言上的,而在c++里输入输出机制有些不同,采取的是io流,也就是iostream,例如cin和cout,要使用 << >>等等,当然如果你用vc++的话会较少涉及到这些问题,因为在vc++里有一些专门的输入输出函数,要比这些好用一些
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
<font color=red>答案被接受</font><br>回复者:Eilsukce 回复日期:2003-06-22 12:28:03
<br>内容:只要把输入op的那个语句提前即可,改后为:<BR># include "stdio.h"<BR>main()<BR>{<BR> char op;<BR> int n1;<BR> int n2;<BR> printf("enter operator:");<BR> op=getchar();<BR> printf("enter the number n1:");<BR> scanf("%d",&n1);<BR> printf("enter the number n2:");<BR> scanf("%d",&n2);<BR> switch(op)<BR> {<BR> case '+':printf("result of the operation is:%d\n",n1+n2);break;<BR> case '-': printf("result of the operation is:%d\n",n1-n2);break;<BR> case '*':printf("result of the operation is:%d\n",n1*n2);break;<BR> case '/':printf("result of the operation is:%d\n",n1/n2);break;<BR> case '%':printf("result of the operation is:%d\n",n1%n2);break;<BR> default:printf("Wrong Operator!!\n");<BR> }<BR>}<BR>原因:op是字符型,使用键盘缓冲。<BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:anhongbo 回复日期:2003-06-22 21:13:30
<br>内容:我已经发现问题了,只要把scanf("%c",&op);的"%c"前加个空格就行了!多谢大家!
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:Eilsukce 回复日期:2003-06-22 22:58:59
<br>内容:不用谢!
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:唐炜 回复日期:2003-06-24 23:08:21
<br>内容:兄弟 <BR><BR>viod main()<BR><BR>错这呢 vc 我从来没有见过 那本书中这样写主函数! 我看的都是 国外权威书籍。<BR><BR>你去找找<BR><BR>我要补充的是 大家都在 批评 void main() 因为这是 错误!
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:Eilsukce 回复日期:2003-06-27 14:39:26
<br>内容:void main()用于.cpp文件,即工程文件。如果要用c的话,就不要在main前加void。
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:天才蛙蛙 回复日期:2003-06-28 21:16:18
<br>内容:BC++都只能接受int main()这样的声明<BR>好象从前的GCC还需要int main(void),哈哈<BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:唐炜 回复日期:2003-06-29 23:17:17
<br>内容:标准的c 规定:<BR><BR>main()<BR>int main()<BR>int main(void)<BR>int main(argv[], argc[])
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:adam&amp;ancy 回复日期:2003-07-01 23:51:54
<br>内容:C++为什么就是void main(),谁知道?
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -