叫做函数。
#include<iostream>
在最终编译之前,将iostream里的文件内容替换该编译指令。
using namespace std;
使程序使用std名称空间的定义。
cout<<"Hello,world\n"; //或者cout<<"Hello,world"<<endl;
int cheeses;
cheeses=32;
cin>>cheeses;
cout<<"We have "<<cheeses<<"varieties of cheeses"<<endl;
int froop(double t);
void rattle(int n);
int prune(void);
int froop(double)
指出函数在调用时需要输入一个double类型的参数,函数返回一个int类型值。void rattle(int n)
指出函数在调用的时候需要输入一个int类型参数,且该函数无返回值。int prune(void)
指出函数不接收任何输入参数,函数返回一个int值。当函数的返回值类型为void的时,不用在函数中使用return。
可能原因:
没有#include<iostream>
,或未使用using namespace std
。
解决方法:
1.添加#include<iostream>
,在main()函数外使用using namespace std
;
2.添加include<iostream>
,使用using std::cout<<"Please enter your PIN:"
;
3.添加include<iostream>
,使用std::cout << "Please enter your PIN:"
;
#include<iostream>
using namespace std;
int main()
{
cout<<"My name is iDoit,my address is BLABLA.."<<endl;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int longNum=0;
cout<<"Please enter distance(long): ";
cin>>longNum;
cout<<"You input "<<longNum<<" long"<<endl;
cout<<longNum<<"long = "<<longNum*220<<" 码"<<endl;
return 0;
}
Three blind mice
Three blind mice
See how they run
See how they run
#include<iostream>
using namespace std;
void A_function()
{ cout<<"Three blind mice"<<endl; }
void B_function()
{ cout<<"See how they run"<<endl; }
int main()
{
A_function();
A_function();
B_function();
B_function();
return 0;
}
Enter your age:29
#include<iostream>
using namespace std;
int main()
{
int age;
cout<<"Enter your age:";
cin>>age;
cout<<"your age include "<<age*12<<" months."<<endl;
return 0;
}
Please enter a Celsius value:20
20 degrees Celsius is 69 degrees Fahrenheit.
#include<iostream>
using namespace std;
double CtoF(double C)
{
double F=1.8*C+32.0;
return F;
}
int main()
{
double celsiusValue,fahrenheitValue;
cout<<"Please enter a Celsius value: ";
cin>>celsiusValue;
fahrenheitValue=CtoF(celsiusValue);
cout<<celsiusValue<<" degrees Celsius is "<<fahrenheitValue<<" degress Fahrenheit"<<endl;
}
Enter the number of light years: 4.2
4.2 light years =265608 astronomical units.
#include<iostream>
using namespace std;
double LYtoAU(double lightYears)
{
double astroUnits=lightYears*63240;
return astroUnits;
}
int main()
{
double lightYears;
cout<<"Enter the number of light years: ";
cin>> lightYears;
double astroYears=LYtoAU(lightYears);
cout<<lightYears<<" light Years ="<<astroYears<<" astronomical units"<<endl;
return 0;
}
Enter the number of hours: 9
Enter the number of minutes: 28
Time: 9:28
#include<iostream>
using namespace std;
void Time(int hours,int minutes)
{
cout<<"Time: "<<hours<<":"<<minutes<<endl;
}
int main()
{
int hours,minutes;
cout<<"Enter the number of hours: ";
cin>>hours;
cout<<"Enter the number of minutes: ";
cin>>minutes;
Time(hours,minutes);
return 0;
}