描述
编写函数GetReal和GetString,在main函数中分别调用这两个函数。在读入一个实数和一个字符串后,将读入的结果依次用printf输出。
两次输入前要输出的提示信息分别是"please input a number:\n”和"please input a string:\n"
样例输入
9.56
hello
样例输出
please input a number:
please input a string:
9.56
hello
#include <cstring>
#include <stdio.h>
#include <iostream>
#include <iomanip>
using namespace std;
double GetReal()
{
double n ;
scanf("%lf", &n);
return n ;
}
void GetString(string &s)//此处要引用
{
getline(cin, s);
// cout << s;
}
int main()
{
double n ;
string s;
printf("please input a number:\n");
n=GetReal();
printf("please input a string:\n");
getchar();//吸收回车符
GetString(s);
// printf("%.2lf\n%s\n", n, s);
cout << fixed << setprecision(2) << n << endl << s << endl;
return 0 ;
}

本文详细介绍了一个使用C++实现的函数输入输出案例,通过GetReal和GetString两个函数,分别读取并输出用户输入的实数和字符串。文章展示了如何在main函数中调用这两个函数,并在输入前提供相应的提示信息。
1089

被折叠的 条评论
为什么被折叠?



