1.
#include <iostream>
int main()
{
using namespace std;
char first[20], last[20];
cout << "Enter your first name:\n";
cin >> first;
cout << "Enter your last name:" << endl;
cin >> last;
cout << "Hi, " << first << ' ' << last << endl;
return 0;
}
Enter your first name:
Man
Enter your last name:
Wu
Hi, Man Wu
#include <iostream>
int main()
{
using namespace std;
cout << "Enter a distance in long.\n";
float distance;
cin >> distance;
cout << distance << " long equals to " << distance * 220 << " yard.\n";
return 0;
}
可以看到float只有6位有效位(和小数点没关系!)
Enter a distance in long.
2.111144141
2.11114 long equals to 464.452 yard.
#include <iostream>
void mice();
void run();
int main()
{
mice();
mice();
run();
return 0;
}
void mice()
{
std::cout << "Three blind mice\n";
}
void run()
{
std::cout << "See how they run\nSee how they run\n";
}
记住函数原型,然后这次main不需要using指令,子函数里我觉得只用cout不需要名称空间
Three blind mice
Three blind mice
See how they run
See how they run
#include <iostream>
int main()
{
using namespace std;
cout << "Enter your age:\n";
int age;
cin >> age;
cout << "Your age contains " << age * 12 << " months.\n";
}
Enter your age:
23
Your age contains 276 months.
#include <iostream>
float tempTransform(float celsius);
int main()
{
std::cout << "Enter a temperature in Celsius:\n";
float celsius, fahrenheit;
std::cin >> celsius;
fahrenheit = tempTransform(celsius);
std::cout << celsius << " degrees Celsius is " << fahrenheit << " degrees Fahrenheit.\n";
return 0;
}
float tempTransform(float celsius)
{
float f;
f = 9 * celsius / 5 + 32;
return f;
}
即使是float,也没有默认加小数点哦
Enter a temperature in Celsius:
20
20 degrees Celsius is 68 degrees Fahrenheit.
#include <iostream>
double LightToAstro(double light);
int main()
{
using namespace std;
cout << "Enter the number of light years:\n";
double light, astro;
cin >> light;
astro = LightToAstro(light);
cout << light << " light years = " << astro << " astronomical units.\n";
return 0;
}
double LightToAstro(double light)
{
return light * 63240;
}
Enter the number of light years:
4.2
4.2 light years = 265608 astronomical units.
#include <iostream>
void showTime(int hours, int minutes);
int main()
{
std::cout << "Enter the number of hours:\n";
int hours, minutes;
std::cin >> hours;
std::cout << "Enter the number of minutes:\n";
std::cin >> minutes;
showTime(hours, minutes);
return 0;
}
void showTime(int hours, int minutes)
{
std::cout << "Time: " << hours << ":" << minutes << std::endl;//注意endl也要有前缀啊
}
Enter the number of hours:
9
Enter the number of minutes:
28
Time: 9:28