1. 程式人生 > >c語言網-1477-字串輸入輸出函式

c語言網-1477-字串輸入輸出函式

題目描述

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

很顯然,這是一條水題,但注意的是要用printf輸出字串,string是不能直接用printf輸出的,我們可以用字元陣列輸出,也可以用string,不過要用scanf("%s",c.c_str()); printf("%s\n",c.c_str());格式輸出。

# include <iostream>
# include<stdio.h>
# include<cstring>
  using namespace std;
  	char a[100];
  	char b[100];
  void GetReal()
  {
  
  	scanf("%s",&a);
  
  }
  void GetString()
  {
    scanf("%s",&b);
  }
  int main ()
  {
    printf("please input a number:\n");
    printf("please input a string:\n");
    GetReal();
    GetString();
    printf("%s\n",a);
    printf("%s\n",b);
 
    
    
   }