2015-08-26 43 views
0

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

#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); 
    } 


} 
+0

'焦炭nextChar更换cin >> message; if(std :: cin.get(nextChar)){/ * process character * /}' –

回答