输出问题 [英] Output problem

查看:58
本文介绍了输出问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码一直把输入语句放在同一行,而不是

让我正常输入它们;这里是代码:


int main()

{

cout<< 加密或解密?[1 =加密| 0 =解密]" ;;

bool select;

cin>> select;

if(select == true)

{

string cleartext;

string key;

cout<< 输入明文: << endl;

getline(cin,cleartext,''\ n'');

cout<< 输入密钥: << endl;

getline(cin,key,''\ n'');

string ciphertext(encrypt(cleartext,key));

cout<< 密文: <<密文<< endl;

}

string ciphertext;

string key;

cout<< 输入密文:" ;;

getline(cin,ciphertext,''\ n'');

cout<< 输入密钥:" ;;

getline(cin,key,''\ n'');

字符串解密(decrypt(ciphertext,key)) ;

cout<< 明文: <<解密<<结束;

系统(PAUSE);

返回EXIT_SUCCESS;

}


你能帮助我吗?谢谢。

My code keeps putting the input statements on the same line, instead of
letting me input them normally; here''s the code:

int main()
{
cout << "Encrypt or decrypt?[1=encrypt|0=decrypt] ";
bool select;
cin >> select;
if(select==true)
{
string cleartext;
string key;
cout << "Enter the cleartext: " << endl;
getline(cin,cleartext,''\n'');
cout << "Enter the key: " << endl;
getline(cin,key,''\n'');
string ciphertext(encrypt(cleartext,key));
cout << "Ciphertext: " << ciphertext << endl;
}
string ciphertext;
string key;
cout << "Enter the ciphertext: ";
getline(cin,ciphertext,''\n'');
cout << "Enter the key: ";
getline(cin,key,''\n'');
string decrypted(decrypt(ciphertext,key));
cout << "Cleartext: " << decrypted << endl;
system("PAUSE");
return EXIT_SUCCESS;
}

Can you help me? Thanks.

推荐答案

Protoman写道:
Protoman wrote:
我的代码一直把输入语句放在同一行,而不是让我正常输入它们;这里是代码:

int main()
{
cout<< 加密或解密?[1 =加密| 0 =解密]" ;;
布尔选择;
cin>> select;
if(select == true)
{
string cleartext;
string key;
cout<< 输入明文: << getl;
getline(cin,cleartext,''\ n'');
cout<< 输入密钥: << getl;
getline(cin,key,''\ n'');
string ciphertext(encrypt(cleartext,key));
cout<< 密文: <<密文<< endl;
}
字符串密文;
字符串键;
cout<< 输入密文:" ;;
getline(cin,ciphertext,''\ n'');
cout<< 输入密钥:" ;;
getline(cin,key,''\ n'');
字符串解密(解密(密文,密钥));
cout< < 明文: <<解密<< endl;
系统(暂停);
返回EXIT_SUCCESS;
}

你能帮助我吗?谢谢。
My code keeps putting the input statements on the same line, instead of
letting me input them normally; here''s the code:

int main()
{
cout << "Encrypt or decrypt?[1=encrypt|0=decrypt] ";
bool select;
cin >> select;
if(select==true)
{
string cleartext;
string key;
cout << "Enter the cleartext: " << endl;
getline(cin,cleartext,''\n'');
cout << "Enter the key: " << endl;
getline(cin,key,''\n'');
string ciphertext(encrypt(cleartext,key));
cout << "Ciphertext: " << ciphertext << endl;
}
string ciphertext;
string key;
cout << "Enter the ciphertext: ";
getline(cin,ciphertext,''\n'');
cout << "Enter the key: ";
getline(cin,key,''\n'');
string decrypted(decrypt(ciphertext,key));
cout << "Cleartext: " << decrypted << endl;
system("PAUSE");
return EXIT_SUCCESS;
}

Can you help me? Thanks.




我不完全确定我会关注你,但我认为问题是这个


cin> >选择;


后跟这个


getline(cin,cleartext,''\ n'');


(顺便说一句,你不需要把''\ n'',这是默认的。)


仔细想想这两个语句是做什么的。第一个读取一个

布尔值,但不读取换行符。即使你已经输入了一个换行符(即输入键),它还没有读过

。然后你会看到getline语句,它会读到下一个

换行符。那么下一个换行符就是你输入布尔值后输入的那个字符,这不是你想要的。


答案是告诉C ++在

布尔值之后忽略换行符。你用这个奇怪的代码做到了这一点


cin>>选择;

cin.ignore(INT_MAX,''\ n'');

....

getline(cin,cleartext );


您需要包含< limits.h>定义INT_MAX。


这应该是常见问题,但似乎不是。


john



I''m not completely sure I follow you but I think the problem is this

cin >> select;

followed by this

getline(cin,cleartext,''\n'');

(BTW you don''t need to put ''\n'', it is the default).

Think carefully about what the two statements do. The first reads a
boolean value but does not read the newline character. Even though you
have typed a newline character (i.e. the enter key) it has not been read
yet. Then you get to the getline statement, that reads upto the next
newline. Well the next newline character is the one you typed after
entering the boolean value, which is not what you want.

The answer is to tell C++ to ignore the newline character after the
boolean value. You do that with this strange code

cin >> select;
cin.ignore(INT_MAX, ''\n'');
....
getline(cin,cleartext);

You need to include <limits.h> to get INT_MAX defined.

This should be a FAQ but it doesn''t seem to be.

john


好的,但现在它没有显示字符串decrypted。

OK, but now it doesn''t show the string "decrypted".




John Harrison写道:

John Harrison wrote:
答案是告诉C ++在
布尔值之后忽略换行符。你用这个奇怪的代码做到了这一点

cin>>选择;
cin.ignore(INT_MAX,''\ n'');


你也可以使用

cin.ignore(1);

因为它只是你想要的一个换行符忽略。

...
getline(cin,cleartext);

你需要包含< limits.h>获得INT_MAX定义。
The answer is to tell C++ to ignore the newline character after the
boolean value. You do that with this strange code

cin >> select;
cin.ignore(INT_MAX, ''\n'');
you can also use
cin.ignore(1);
since it is only one newline character which you want to ignore.
...
getline(cin,cleartext);

You need to include <limits.h> to get INT_MAX defined.



首选< climits>。更好的是,包括< limits>并使用

std :: numeric_limits< int> :: max();


Prefer <climits>. Still better, include <limits> and use
std::numeric_limits<int>::max();


这篇关于输出问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆