cout与printf

liuzq133 2007-02-11 09:56:45
下面这个程序不能正常运行:
#include <string.h>
#include <stdio.h>
#include <iostream.h>

void main()
{
const int MAX = 20 ;

char str_a[MAX] ;
char str_b[MAX] ;

cout << "please input the first string:" ;
gets(str_a) ;

cout << "please input the second string:" ;
gets(str_b) ;

char *pa ;
char *pb ;

for (pa = str_a , pb = str_b ; pa < str_a + strlen(str_a) && pb < str_b + strlen(str_b) ;
pa++ , pb++)
{
if (*pa == *pb)
{
cout << *pa ;
}
}

cout << endl ;
}
如果把上面的cout << "please input the first string:" ;cout << "please input the second string:" ;输出换成printf就可以正常运行,那位知道是什么原因啊,可能涉及到cout与printf的区别,
...全文
654 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
FourQueue 2007-10-28
  • 打赏
  • 举报
回复
using namespace std;

名字空间
hpyyang 2007-10-28
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;
void main()
{
const int MAX=20;
char str_a[MAX];
char str_b[MAX];
cout<<"please input the first strting:";
gets(str_a);
cout<<"please input the second strting:";
gets(str_b);
char *pa;
char *pb;
int kg=0;
for (pa=str_a,pb=str_b;pa<str_a+strlen(str_a)&&pb<str_b+strlen(str_b);pa++,pb++)
{
if (*pa==*pb)
{
cout<<*pa;
kg=1;
}



}
if (kg==0)

cout<<"bu tong"<<endl;

}
或者是这样写
hpyyang 2007-10-28
  • 打赏
  • 举报
回复
#include "string.h"
#include "stdio.h"
#include "iostream.h"
void main()
{
const int MAX=20;
char str_a[MAX];
char str_b[MAX];
cout<<"please input the first strting:";
//后面加一个<<endl结束换行就可以了
gets(str_a);
cout<<"please input the second strting:"<<endl;
//后面加一个<<endl结束换行就可以了
gets(str_b);
char *pa;
char *pb;
int kg=0;
for (pa=str_a,pb=str_b;pa<str_a+strlen(str_a)&&pb<str_b+strlen(str_b);pa++,pb++)
{
if (*pa==*pb)
{
cout<<*pa;
kg=1;
}



}
if (kg==0)

cout<<"bu tong"<<endl;

}
Your_FOREST 2007-02-12
  • 打赏
  • 举报
回复


#include <iostream.h> 换成

#include <iostream>
using namespace std;

因为要打开 iostream 的名称空间
liuzq133 2007-02-12
  • 打赏
  • 举报
回复
这个程序的目的是找到输入两个字符串的对应位置相同的字符并输出
liuzq133 2007-02-12
  • 打赏
  • 举报
回复
那个程序是在VC 6.0下运行的,
#include <string.h> #include <cstring>
#include <stdio.h> ==》 #include <cstdio>
#include <iostream.h> #include <iostream>
不行啊,编译都不能通过的,显示cin 和 cout没有声明,另外我在上面给出的那个程序编译可以通过 , 就是运行起来不太正常,各位试一下就知道了



tygmy 2007-02-12
  • 打赏
  • 举报
回复
xyjchinese(scorpion)
是正解
jixingzhong 2007-02-12
  • 打赏
  • 举报
回复
基本上程序是可行的 ~

不过,
iostream.h 这样的头文件是为了兼容C的,
推荐使用 C++ 方式包含头文件:
#include <string.h>
#include <stdio.h>
#include <iostream.h>
==》
#include <cstring>
#include <cstdio>
#include <iostream>
using namespace std;
discory 2007-02-12
  • 打赏
  • 举报
回复
不符合c++标准
xyjchinese 2007-02-12
  • 打赏
  • 举报
回复
头文件改成如下:

#include<iostream>
#include<cstdio>
using namespace std;
toddzst 2007-02-12
  • 打赏
  • 举报
回复
刚在VC6.0中试了下,正常运行,只不过结果有问题。
lidongri 2007-02-12
  • 打赏
  • 举报
回复
在VC中没有任何问题
楼主的问题,俺搞不明白
估计是编译器的问题
toddzst 2007-02-12
  • 打赏
  • 举报
回复
#include <iostream.h> 修改为 #include <iostream>试下,C++标准库好像改了
longlianxi 2007-02-11
  • 打赏
  • 举报
回复
修改:
#include <string>
#include <iostream>

using namespace std;

void main()
{
string str_a;
string str_b;

cout<< "please input the first string:";
cin>>str_a;

cout<< "please input the second string:"<<str_b;
cin>>str_b;

if(str_a==str_b)
{
cout << "str_a is equal to str_b!\n";
cout <<"str_a is "<< str_a ;
}
else
{
cout << "str_a is not equal to str_b!";
}

cout << endl ;
}
用纯c++编写,或用纯c编写.
peterxuxu 2007-02-11
  • 打赏
  • 举报
回复
你加个<<endl;
看看
jixingzhong 2007-02-11
  • 打赏
  • 举报
回复
没有 IDE,
错误提示是什么那?
jixingzhong 2007-02-11
  • 打赏
  • 举报
回复
不会这么 BT 吧?

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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