1. 程式人生 > >c/c++ 以回車符為結束符的字符串輸入 樣例(getline, gets, getchar) by slj

c/c++ 以回車符為結束符的字符串輸入 樣例(getline, gets, getchar) by slj

tchar i++ 註意 span str nbsp code getch mes

#include<iostream>
#include<cstdio>
#include<string>
using namespace std;
int main()
{
/*
//輸入一個以回車符為結束符的字符串:方法1:需要#include<string>
    string a;
    getline(cin,a);
    cout<<a;
*/

/*
//輸入一個以回車符為結束符的字符串:方法2:需要#include<iostream> 或    #include<cstdio>
    char s[20];
    gets(s);
    cout<<s;
*/ /* //輸入一個以回車符為結束符的字符串:方法3:需要#include<iostream> 或 #include<cstdio> char ch,s[20]; int i=0; while((ch=getchar())!=‘\n‘) //註意, (ch=getchar())一定要有括號 { s[i++]=ch; //cout<<i; } s[i]=‘\0‘; cout<<s; */ return 0; }

c/c++ 以回車符為結束符的字符串輸入 樣例(getline, gets, getchar) by slj