1. 程式人生 > >演算法提高 3-2字串輸入輸出函式

演算法提高 3-2字串輸入輸出函式

描述

  編寫函式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 ;
}