如何使用 cin.get() 而不仅仅是 cin?
How to use cin.get() instead of just cin?
我正在尝试创建一个 ceasar 密码,但是当我输入消息时,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.get的解释如下:http://www.cplusplus.com/reference/istream/istream/get/
您应该将 cin >> message
替换为 cin.get (message,size_of_max_aprox_input)
我正在尝试创建一个 ceasar 密码,但是当我输入消息时,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.get的解释如下:http://www.cplusplus.com/reference/istream/istream/get/
您应该将 cin >> message
替换为 cin.get (message,size_of_max_aprox_input)