初学C++的代码,请大家指点里面的不足之处,谢谢了 !

racewind 2006-07-03 11:46:02
#include <iostream>
#include <string>


using namespace std;

int i=1;
double d = 11.12;
float f = 123.1;
char c = 'a';
string s = "leelnxxiang";
//long l = 123.2;
//short sh = 123123.123;
bool boo = 1;

void for_dir()
{
for(int i=0;i<100;i++)
{
cout << i;
cout << endl;
}
}

void arrays()
{
int id[100];
for(int i=0;i<100;i++)
{
id[i] = i;
cout << "id[" << id[i] << "]";
cout << endl;
}
}

void arrays_char()
{
cout << "请输入您的名字................................";
cout << endl;
char ch[11];
string str = "" ;

for(int i=0;i<11;i++)
{
cin >> ch[i];
str += ch[i];
}
cout <<"您输入的是: " << str;
}

void while_dir()
{
int x=1;
while(true)
{
cout << endl;
cout << "while ing ............" << x;
x++;
if(x == 33)
{
return;
}
}
}

void do_dir()
{
int x = 1;
do
{
cout << endl;
cout << "do while ing ............" << x;
x++;
if(x == 33)
{
return;
}
}
while(true);
}
void exit_exit()
{
cout << "退出程序请输入 'exit' ...........";
cout << endl;
string s = "";
cin >> s;
if(s == "exit")
{
cout << "保存退出程序成功!";
exit(0);
}
else
{
cout << "输入错误,程序将不保存运行";
}
}


void display()
{
cout << i;cout << endl;
cout << d;cout << endl;
cout << f;cout << endl;
cout << c;cout << endl;
cout << s;cout << endl;
//cout << l;cout << endl;
//cout << sh;cout << endl;
cout << boo;cout << endl;
}


void Cf()
{
for(int i=1;i<10;i++)
{
for(int j=1;i>=j;j++)
{
cout << i << "*" << j << "=" << i*j << " ";
}
cout << endl;
}
}
int main()
{
cout << "hello world";
cout << endl;

cout << "for .....................";
cout <<endl;
for_dir();

cout << endl;
cout << "while ...................";
while_dir();

cout << endl;
cout << "do ..................................";
do_dir();

cout << endl;
printf("显示乘法口诀..................");
cout << endl;
Cf();
printf("显示数据类型..................");
cout << endl;
display();

cout << endl;
arrays();

arrays_char();

cout << endl;
exit_exit();
}
...全文
187 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
Veiz 2006-07-03
  • 打赏
  • 举报
回复
dev c++ 不是编译器,
你可以输出 sizeof(short) 和sizeof(long) ,看看它们的长度
Veiz 2006-07-03
  • 打赏
  • 举报
回复
string s = "leelnxxiang"; 改为 const string s = "leelnxxiang"; 其他类似
能const 就尽量 const
少用全局变量。注意缩小变量作用域。
没有必要的话不要用printf, 用cout
int id[100] = {0}; //要初始化id
char ch[11] = {0}; // 这里也要初始化
一行中不宜出现两个以上的分号, 这是程序维护的需要
多加空格是好的。
endl会清空流缓冲区,没有必要的话用 "\n" 行了
写++i, 尽量不写i++
按照单一职责原则,一个函数应只完成一项功能。
Veiz 2006-07-03
  • 打赏
  • 举报
回复
不要用 float 这种数据类型, 就当它不存在行了。
rollor_phoe 2006-07-03
  • 打赏
  • 举报
回复
void arrays_char()
{
cout << "请输入您的名字................................";
cout << endl;
char ch[11];
string str = "" ;

for(int i=0;i<11;i++)
{
cin >> ch[i];
str += ch[i];
}
cout <<"您输入的是: " << str;
--------------------------------------------
这个函数改成:
void arrays_char()
{
cout << "请输入您的名字................................";
cout << endl;

string str = "" ;
getline(cin,str);//这样可以输入空格,它是接改一行数据,以换行符分隔

cout <<"您输入的是: " << str;
---------------------------------------------------
C++中有long和short类型。
racewind 2006-07-03
  • 打赏
  • 举报
回复
对了,C++里有没有long和short的类型?为什么我用++vdev-c++编译器有时候会编译出long和short的错误那?
racewind 2006-07-03
  • 打赏
  • 举报
回复
哦,知道了,语法和java差好多来,呵呵
Veiz 2006-07-03
  • 打赏
  • 举报
回复
cout << "abcdefg" << endl;
cout << "abcdefg\n" ;
可以连在一起写的。
racewind 2006-07-03
  • 打赏
  • 举报
回复
受教,修改如下:#include <iostream>
#include <string>
#include <math.h>

using namespace std;

int i=1;
double d = 11.12;
float f = 123.1;
char c = 'a';
const string s = "leelnxxiang";
//long l = 123.2;
//short sh = 123123.123;
bool boo = 1;

static const string static_string = "javasoft";

void for_dir()
{
for(int i=0;i<100;i++)
{
cout << i;
cout << "n";
}
}

void arrays()
{
int id[100] = {0};
for(int i=0;i<100;++i)
{
id[i] = i;
cout << "id[" << id[i] << "]";
cout << "\n";
}
}

void arrays_char()
{
cout << "请输入您的名字................................";
cout << "\n";
char ch[11];
string str = "" ;

getline(cin,str);
cout <<"您输入的是: " << str;
}

void while_dir()
{
int x=1;
while(true)
{
cout << "\n";
cout << "while ing ............" << x;
x++;
if(x == 33)
{
return;
}
}
}

void do_dir()
{
int x = 1;
do
{
cout << "\n";
cout << "do while ing ............" << x;
x++;
if(x == 33)
{
return;
}
}
while(true);
}
void exit_exit()
{
cout << "退出程序请输入 'exit' ...........";
cout << "\n";
string s = "";
cin >> s;
if(s == "exit")
{
cout << "保存退出程序成功!";
exit(0);
}
else
{
cout << "输入错误,程序将不保存运行";
}
}


void display()
{
cout << i;cout << "\n";
cout << d;cout << "\n";
cout << f;cout << "\n";
cout << c;cout << "\n";
cout << s;cout << "\n";
//cout << l;cout << "\n";
//cout << sh;cout << "\n";
cout << boo;cout << "\n";
}


void Cf()
{
for(int i=1;i<10;++i)
{
for(int j=1;i>=j;j++)
{
cout << i << "*" << j << "=" << i*j << " ";
}
cout << "\n";
}
}
int main()
{
cout << "hello world";
cout << "\n";

cout << "for .....................";
cout <<endl;
for_dir();

cout << "\n";
cout << "while ...................";
while_dir();

cout << "\n";
cout << "do ..................................";
do_dir();

cout << "\n";
printf("显示乘法口诀..................");
cout << "\n";
Cf();
printf("显示数据类型..................");
cout << "\n";
display();

cout << "\n";
arrays();

arrays_char();

cout << "\n";

cout << static_string;

cout << "\n";
exit_exit();
}
racewind 2006-07-03
  • 打赏
  • 举报
回复
谢谢各位大侠!我现在脑子里总是有java的印象而且很深刻,如果不是要开发基于PPC2003的东西我可能就不学c++了,呵呵
Visual C/C++作为功能强大的可视化应用程序开发工具,是计算机界公认的优秀应用开发工具。Microsoft的基本类库MFC使得开发Windows应用程序变得很容易,适合作各种系统软件、应用软件、网络软件、游戏软件等开发平台。 根据Visual C/C++的不同应用对象,将精选的190个实例共分数据库及图形图像分册、网络与通信分册、关键技术精解分册出版。本书为专家指点分册。全书本着实用第一的原则,紧紧围绕主题展开,循序渐进,由浅入深地介绍了使用Visual C/C++进行应用程序开发思想方法与编程技巧。 本书的特色体现如下几点:第一,每一章都是通过一个个的实例来介绍Visual C/C++应用编程方法和技巧,避免枯燥、空洞的理论,并且每一个实例都具有很强的实用性和代表性。第二,所选的每一个实例都是从事Visual C/C++应用编程人员的经验总结,具有很强的实用性,其中很多编程技巧可供借鉴。第三,每一个实例的程序源代码都是经过上机调试通过,给程序开发人员移植源代码带来了方便,从而加快应用编程的步伐。第四,对老版本经典实例进行点评,选取一些老版本开发环境的经典实例加以点评分析,使之能够起到触类旁通的作用。 本书适用于有一定Visual C/C++应用基础的编程人员和应用开发人员,对初学Visual C/C++编程的读者也有一定的参考价值。

64,685

社区成员

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

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