如何使用cin.get()而不仅仅是cin

How to use cin.get() instead of just cin?

本文关键字:cin 不仅仅是 get 何使用      更新时间:2023-10-16

我试图创建一个凯撒密码,但是当我输入我的消息时,cin不接受空格,只接受第一个单词。我的老师告诉我用cin.get()而不是cin.getline()。在这种情况下我如何使用cin.get() ?

#include <iostream>
#include <string>
#include <stdlib.h>
#include <stdio.h>
#include "unistd.h"
using namespace std;
int main(int argc, char **argv){
  string message = "Empty";
  int opt = 0;
  int rotation = 0;
    extern char *optarg;
    static const char* opt_string = "r:f:h";
    opt = getopt( argc, argv, opt_string);
    while(opt != -1)
    {
      switch (opt)
      {
      case 'r':
        rotation = atoi(optarg);
        cin >> message;
        for(int x = 0; x<message.length(); x++)
          message[x] = message[x] + rotation;
        cout << message << endl;
        break;
    }
    opt = getopt( argc, argv, opt_string);
  }

}

下面是关于cin的解释。得到:http://www.cplusplus.com/reference/istream/istream/get/

您应该将cin >> message替换为cin.get (message,size_of_max_aprox_input)

相关文章: