4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

空白を含む文字列を受け取る(C++)

Last updated at Posted at 2021-06-20

空白を含む文字列の受け取り方(C++)

getline(cin, 入力を受け取る変数)を使うことで解決する。

# include <iostream>
# include <string>
using namespace std;

int main(){
    string s;
    getline(cin, s);

    cout << s << endl;
    for(int i=0; i<5; i++){
        cout << "s[" << i << "]:" <<s[i] << endl;
    }
    return 0;
}

入力

my name is moufu

出力

my name is moufu
s[0]:m
s[1]:y
s[2]:
s[3]:n
s[4]:a

空白も1文字分になっている。

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?