帮忙给看看哪个地方出错了/:~

shore1111 2008-03-07 05:05:35
#include <iostream>
using namespace std;
void printBinary(const unsigned char );
int main()
{
printBinary(cin.get());

void printBinary(const unsigned char val)
{
for(int i=7;i>=0;i--)
if(val&(1<<i))
cout<<"1";
else cout<<"0";
}
return 0;
}
编译器提示函数定义非法(illegal),可是看了好久不知道哪里出错了,还望赐教!!多谢~
...全文
81 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
shore1111 2008-03-07
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 ttkk_2007 的回复:]
C/C++ code#include<iostream>usingnamespacestd;voidprintBinary(constunsignedchar);intmain()
{
printBinary(cin.get()-'0');//处理数字的return0;
}voidprintBinary(constunsignedcharval)
{for(inti=7; i>=0; i--)if(val&(1<<i))
cout<<"1";elsecout<<"0";
}
[/Quote]
可以处理数字,不过好象只能处理个位数,当然稍微修改一下就好了
#include <iostream>
using namespace std;
void printBinary(const unsigned int);

int main()
{ int a;
cin>>a;
printBinary(a);
cout<<endl;
return 0;
}
void printBinary(const unsigned int val)
{
for(int i=7;i>=0;i--)
if(val&(1<<i))
cout<<"1";
else cout<<"0";
}
ttkk_2007 2008-03-07
  • 打赏
  • 举报
回复

#include <iostream>
using namespace std;

void printBinary(const unsigned char );

int main()
{
printBinary(cin.get() - '0'); //处理数字的
return 0;
}

void printBinary(const unsigned char val)
{
for (int i = 7; i >= 0; i--)
if (val & (1 << i))
cout << "1";
else
cout << "0";
}

独孤过儿 2008-03-07
  • 打赏
  • 举报
回复

#include <iostream>
using namespace std;

void printBinary(const unsigned char );

int main()
{
printBinary(cin.get());
return 0;
}

void printBinary(const unsigned char val)
{
for (int i = 7; i >= 0; i--)
if (val & (1 << i))
cout << "1";
else
cout << "0";
}
这个好像是《C++编程思想》中的例子吧...
楼主记得,你处理的是一个char,而不是一个数字,所以你输入1、2的时候,处理的是字符1、2,而不是数字1、2!
tangyangyu 2008-03-07
  • 打赏
  • 举报
回复
一般函数不能嵌套定义的.除了递归(自己调用自己的).
#include <iostream>
using namespace std;
void printBinary(const unsigned char );
int main()
{
printBinary(cin.get());
return 0;
}
void printBinary(const unsigned char val)
{
for(int i=7;i> =0;i--)
if(val&(1 < <i))
cout < <"1";
else cout < <"0";
}
这样就不会错了.
stecdeng 2008-03-07
  • 打赏
  • 举报
回复
main外定义函数
void printBinary(const unsigned char val)
{
for(int i=7;i>=0;i--)
if(val&(1<<i))
cout <<"1";
else cout <<"0";
}
shore1111 2008-03-07
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 baihacker 的回复:]
C/C++ code//不要嵌套定义函数#include<iostream>usingnamespacestd;voidprintBinary(constunsignedchar);intmain()
{
printBinary(cin.get());return0;
}voidprintBinary(constunsignedcharval)
{for(inti=7;i>=0;i--)if(val&(1<<i))
cout<<"1";elsecout<<"0";
}
[/Quote]

多谢!!
不过还有一个问题,我打算输出一个数的二进制,可是函数好象不能达到目的,还望能给指正~
shore1111 2008-03-07
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 baihacker 的回复:]
C/C++ code//不要嵌套定义函数#include<iostream>usingnamespacestd;voidprintBinary(constunsignedchar);intmain()
{
printBinary(cin.get());return0;
}voidprintBinary(constunsignedcharval)
{for(inti=7;i>=0;i--)if(val&(1<<i))
cout<<"1";elsecout<<"0";
}
[/Quote]

多谢!!
不过还有一个问题,我打算输出一个数的二进制,可是函数好象不能达到目的,还望能给指正~
Treazy 2008-03-07
  • 打赏
  • 举报
回复
是帖错了?
Cplusplus2005 2008-03-07
  • 打赏
  • 举报
回复
main函数少了一个'}'
baihacker 2008-03-07
  • 打赏
  • 举报
回复

//不要嵌套定义函数
#include <iostream>
using namespace std;
void printBinary(const unsigned char );
int main()
{
printBinary(cin.get());
return 0;
}
void printBinary(const unsigned char val)
{
for(int i=7;i>=0;i--)
if(val&(1<<i))
cout <<"1";
else cout <<"0";
}

64,681

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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