level 8
#include<iostream.h>
#include<string.h>
void showLarger(int n1,int n2);
void showLarger(float f1,float f2);
void showLarger(string s1,string s2);
void main()
{
int num1=50,num2=100;
float numA=1.1,numB=9.9;
string str1="Sunny day !";
string str2="Wonderful tonight !";
showLarger(num1,num2);
showLarger(numA,numB);
showLarger(str1,str2);
}
void showLarger(int n1,int n2)
{
if(n1==n2)
cout<<n1<<"equals"<<n2<<endl;
else if(n1>n2)
cout<<"Between "<<n1<<"and"<<n2<<"the larger one is "<<n1<<endl;
else
cout<<"Between "<<n1<<"and"<<n2<<"the larger one is "<<n2<<endl;
}
void showLarger(float f1,float f2)
{
if(f1==f2)
cout<<n1<<"equals"<<n2<<endl;
else if(n1>n2)
cout<<"Between "<<f1<<"and"<<f2<<"the larger one is "<<f1<<endl;
else
cout<<"Between "<<f1<<"and"<<f2<<"the larger one is "<<f2<<endl;
}
void showLarger(string s1,string s2)
{
if(s1==s2)
cout<<s1<<"equals"<<s2<<endl;
else if(s1>s2)
cout<<"Between "<<"\" "s1<<"\""<<endl;
cout<<" and"<<"\" "<<s2<<"\""<<endl;
cout<<" the larger one is "<<"\" "s1<<"\""<<endl;
else
cout<<"Between "<<"\" "s1<<"\""<<endl;
cout<<" and"<<"\" "<<s2<<"\""<<endl;
cout<<" the larger one is "<<"\" "s2<<"\""<<endl;
}
结果是这样:
2012年04月23日 14点04分
1
level 8
Compiling...
10.cpp
e:\myprojects\第五章\10.cpp(6) : error C2065: 'string' : undeclared identifier
e:\myprojects\第五章\10.cpp(6) : error C2146: syntax error : missing ')' before identifier 's1'
e:\myprojects\第五章\10.cpp(6) : error C2182: 'showLarger' : illegal use of type 'void'
e:\myprojects\第五章\10.cpp(6) : error C2373: 'showLarger' : redefinition; different type modifiers
e:\myprojects\第五章\10.cpp(6) : error C2059: syntax error : ')'
e:\myprojects\第五章\10.cpp(10) : warning C4305: 'initializing' : truncation from 'const double' to 'float'
e:\myprojects\第五章\10.cpp(10) : warning C4305: 'initializing' : truncation from 'const double' to 'float'
e:\myprojects\第五章\10.cpp(11) : error C2146: syntax error : missing ';' before identifier 'str1'
e:\myprojects\第五章\10.cpp(11) : error C2065: 'str1' : undeclared identifier
e:\myprojects\第五章\10.cpp(11) : error C2440: '=' : cannot convert from 'char [12]' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
e:\myprojects\第五章\10.cpp(12) : error C2146: syntax error : missing ';' before identifier 'str2'
e:\myprojects\第五章\10.cpp(12) : error C2065: 'str2' : undeclared identifier
e:\myprojects\第五章\10.cpp(12) : error C2440: '=' : cannot convert from 'char [20]' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
e:\myprojects\第五章\10.cpp(29) : error C2065: 'n1' : undeclared identifier
e:\myprojects\第五章\10.cpp(29) : error C2065: 'n2' : undeclared identifier
e:\myprojects\第五章\10.cpp(35) : error C2146: syntax error : missing ')' before identifier 's1'
e:\myprojects\第五章\10.cpp(35) : error C2182: 'showLarger' : illegal use of type 'void'
e:\myprojects\第五章\10.cpp(35) : error C2373: 'showLarger' : redefinition; different type modifiers
e:\myprojects\第五章\10.cpp(35) : error C2059: syntax error : ')'
e:\myprojects\第五章\10.cpp(36) : error C2143: syntax error : missing ';' before '{'
e:\myprojects\第五章\10.cpp(36) : error C2447: missing function header (old-style formal list?)
执行 cl.exe 时出错. 10.obj - 1 error(s), 0 warning(s)
刚开始学习,什么都不懂!麻烦各位了!
2012年04月23日 14点04分
2
level 5
C风格字符串中S1和S2比较时好像不能直接比较吧,唔,没记错好像是要调用一个strcmy函数
strmpy(s1,s2),只有在C++语言中才可以直接比较吧。
2012年04月23日 14点04分
3
level 8
好像VC++6.0的头文件都必须是.h的,不加的话就出错!我发的是书上的原文来着,不知道为什么错啊!
2012年04月23日 14点04分
4
level 8
编译器不能识别string
你可以把前面改成
#include<string>
using namespace std;
这样应该就可以了,
2012年04月23日 15点04分
5
level 8
你最好把第一个也改了,#include<iostream> 如果你只用了这一个库的话,你就可以写
#incldue<iostream.h> 就不用写后面的 using namespace std; 了
2012年04月23日 15点04分
6
level 4
c++的头文件都是没后缀的
而带.h的都是c的,都是为了兼容c的
而 例如cmath则是为了说明新的c++头文件,但是用以前的c的头文件的内容
2012年04月23日 15点04分
9
level 8
最后把改好的放出来!
#include<iostream>
#include<string>
using namespace std;
void showLarger(int n1,int n2);
void showLarger(float f1,float f2);
void showLarger(string s1,string s2);
void main()
{
int num1=50,num2=100;
float numA=1.1f,numB=9.9f;
string str1="Sunny day !";
string str2="Wonderful tonight !";
showLarger(num1,num2);
showLarger(numA,numB);
showLarger(str1,str2);
}
void showLarger(int n1,int n2)
{
if(n1==n2)
cout<<n1<<"equals"<<n2<<endl;
else if(n1>n2)
cout<<"Between "<<n1<<"and"<<n2<<"the larger one is "<<n1<<endl;
else
cout<<"Between "<<n1<<"and"<<n2<<"the larger one is "<<n2<<endl;
}
void showLarger(float f1,float f2)
{
if(f1==f2)
cout<<f1<<"equals"<<f2<<endl;
else if(f1>f2)
cout<<"Between "<<f1<<"and"<<f2<<"the larger one is "<<f1<<endl;
else
cout<<"Between "<<f1<<"and"<<f2<<"the larger one is "<<f2<<endl;
}
void showLarger(string s1,string s2)
{
if(s1==s2)
cout<<s1<<"equals"<<s2<<endl;
else if(s1>s2)
{
cout<<"Between "<<"\" "<<s1<<"\""<<endl;
cout<<" and"<<"\" "<<s2<<"\""<<endl;
cout<<" the larger one is "<<"\" "<<s1<<"\""<<endl;
}
else
{
cout<<"Between "<<"\" "<<s1<<"\""<<endl;
cout<<" and"<<"\" "<<s2<<"\""<<endl;
cout<<" the larger one is "<<"\" "<<s2<<"\""<<endl;
}
}
2012年04月23日 16点04分
20