注册 登录
编程论坛 C语言论坛

入门选手试了一个求平均值的代码,但是不能求出平均值,有没有兄弟帮忙看看

cherry梨 发布于 2021-09-23 20:15, 2008 次点击
#include <stdio.h>
int main()
{
    int a,b,ave;
    scanf("%d,%d",&a,&b);
    ave=(a+b)/2;
    printf("The average is %d:\n",ave);
    return 0;
}
7 回复
#2
自由而无用2021-09-23 20:24
//online parser: https://www.bccn.net/run/
程序代码:
#include <stdio.h>

#pragma GCC diagnostic ignored "-Wpointer-to-int-cast"

int main(int argc, char *argv[])
{
    int a, b, avr;
   
    a = (char)("cherry" + 3);
    b = (char)("" + 1);
    avr = (a + b) >> 1;
   
    puts("cherry xiaojieje chao ke ai, duck duck!!");
    printf("The average [%d + %d] = %d\n", a, b, avr);
   
    return 0;
}


output sample:
cherry xiaojieje chao ke ai, duck duck!!
The average [91 + 96] = 93
#3
cherry梨2021-09-23 20:26
回复 2楼 自由而无用
我是想知道我的代码哪里出错了
#4
cherry梨2021-09-23 20:29
回复 2楼 自由而无用
谢谢你的夸奖哦
#5
自由而无用2021-09-23 20:40
回复 4楼 cherry梨
you deserve it
your code works well, only if you ought to pay attention to the scanf, there is a comma between 2%d, so input form must be:
3,2
not
3 2
good luck!
#6
cherry梨2021-09-23 20:47
回复 5楼 自由而无用
But when I type in like this,I get the previous number.
#7
自由而无用2021-09-23 20:53
回复 6楼 cherry梨
are u sure, how could that be
#8
自由而无用2021-09-23 20:58
are u sure you really rebuild the project after you modified your source code, it sounds very strange...

[此贴子已经被作者于2021-9-23 21:44编辑过]

#9
cherry梨2021-09-23 21:36
回复 8楼 自由而无用
You can try running my code.It's true.
#10
自由而无用2021-09-23 21:44
I did test your code using online parser (https://www.bccn.net/run/), no problem
its bedtime now, I will test it again tomorrow, g-night sweetheart
#11
cherry梨2021-09-23 22:01
回复 10楼 自由而无用
Thank you!Good night.
#12
自由而无用2021-09-24 08:49
I tried to catch the bug you mentioned to, but still cant make it reappear, change your c-compiler and
try another laptop to run your code once more, or using online parser(https://www.bccn.net/run/), thats not too bad
the following issue looks close to what u have met, take a look at the Answer
https://
-------------------------------------------------------------------------------------------------------------------
Answer

Its a bug in CLion's console - CPP-2580 There are two possible workarounds:

1. Turn off PTY: by disabling run.processes.with.pty option in the Registry (Help -> Find Action -> Registry...)
2. Use Cygwin instead of MinGW
-------------------------------------------------------------------------------------------------------------------
#13
cherry梨2021-09-24 10:11
回复 12楼 自由而无用
Thanks!
#14
lautition2021-10-01 02:46
ave定义为整型,只有双偶数或双基数相加的平均之才是整型
1