0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

C++ 学習備忘録 2 - 文字入力編

Last updated at Posted at 2025-02-12

目次

1.はじめに
2.文字列を入力
3.おわり

1.はじめに

前回(C++ 学習備忘録 1 - 文字出力編)は文字列の出力について記事にしたので、今回は入力について記載。
大きくは出力の時 cout で、入力のときは cin の違いだけですが:confused:

2.文字列を入力

リンゴ、みかん、メロンの個数を入力し、その合計の個数を表示するプログラムを作成しました。

study2_prog1.cpp
#include <iostream>
using namespace std;

int main(){
    int apple, orange, melon;

    cout << "リンゴ の個数を入力してください --->";
    cin >> apple;
    cout << "みかん の個数を入力してください --->";
    cin >> orange;
    cout << "メロン の個数を入力してください --->";
    cin >> melon;

    cout << "リンゴの個数は" << apple << "みかんの個数は" << orange << "メロンの個数は" << melon << "です" << endl;
    cout << "合計の個数は" << apple + orange + melon << "です" << endl;
    
    return 0;

}

実行結果
リンゴ の個数を入力してください --->5
みかん の個数を入力してください --->3
メロン の個数を入力してください --->2
リンゴの個数は5みかんの個数は3メロンの個数は2です
合計の個数は10です
(※太字はキーボードから入力)

文字列の入力の際には cin >> を使用する
この >> で cin に与える文字列を指定しているイメージ。
※namespace を指定しているので std:: はついていない。

改行するの際には endl を付ける。
endl がプログラムで endl がないところは改行がされていない。

3.おわり

文字列の入力について記載しました。
これで入出力は完璧です:wink:

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?