这就是杀我...... !!!!!!!!! [英] This is Killing Me .. !!!!!!!!!

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

问题描述

直到现在我已经安装了我的视觉工作室2012,但仍然有一个愚蠢的错误,我做错了。 。 。 。



Till now I''ve re install my visual studio 2012 but still get one dumb error, what i''m doing wrong with this. . . .

#include "stdafx.h"
#include <string>
#include <iostream>

using namespace std;

int main()
{
	const int size = 30;
	char a[size], b[size];
	cin.get(a, size+1);
	cin.get(b, size+1); // why this is not executing .. ?????????????
	return 0;
}

推荐答案

你还没有正确定义错误是什么,但假设它没有到达第二个cin.get(),你应该注意到每个都会导致输入行的刷新。每个cin.get()调用都会处理一个单独的输入行。



tl; dr:你需要提供至少2个以''结尾的序列\\ n \\ n \\ n \\ n \\ n''''''''''''''''$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
=http://www.cplusplus.com/reference/istream/istream/get/>此链接 [ ^ ]:

[...]

istream& get(char * s,streamsize n);

istream& get(char * s,streamsize n,char delim);



从流中提取字符并将其作为c字符串存储在s中,直到(n- 1)已提取字符或遇到分隔字符:分隔字符是换行符(''\ n'')或delim(如果指定了此参数)。

分隔如果找到,则不从输入序列中提取字符,并将其作为要从流中提取的下一个字符保留(请参阅getline以获取放弃分隔字符的替代字符)。

空字符(如果n大于零,''\'''会自动附加到写入的序列中,即使提取空字符串也是如此。

[...]

---



这会告诉我第一个cin.get()从输入行获取文本,第二个cin.get()得到了''\ n''。



一个reaso nable debugging move将尝试使用3个或更多cin.get()调用输入3行或更多行。

[/ Edit]
You haven''t properly defined what the error is but, assuming that it is not getting to the second cin.get(), you should note that each of these will cause a flush of the input line. Each of the cin.get() calls will handle a separate line of input.

tl;dr: You will need to present at least 2 sequences ending in ''\r\n'' to get both lines to read.


Also, note the following at this link[^]:
[...]
istream& get (char* s, streamsize n);
istream& get (char* s, streamsize n, char delim);

Extracts characters from the stream and stores them in s as a c-string, until either (n-1) characters have been extracted or the delimiting character is encountered: the delimiting character being either the newline character (''\n'') or delim (if this argument is specified).
The delimiting character is not extracted from the input sequence if found, and remains there as the next character to be extracted from the stream (see getline for an alternative that does discard the delimiting character).
A null character (''\0'') is automatically appended to the written sequence if n is greater than zero, even if an empty string is extracted.
[...]
---

This would tell me that the first cin.get() is getting text from a line of your input, and the second cin.get() is getting the ''\n''.

A reasonable debugging move would be to try to input 3 or more lines with 3 or more cin.get() calls.
[/Edit]


你的问题是+1。您正试图获得更多字符,然后缓冲区大小允许您。并不是说这段代码不起作用,而是代码可能导致不可预测的行为。您的 get 语句都不应该有效,但第一个语句可能偶尔会起作用,因为缓冲区首先在您的堆栈上分配。



请参阅: http://www.cplusplus.com/reference/istream/istream/get / [ ^ ]。< br $> b $ b

-SA
Your problem is "+1". You are attempting to get more characters then the buffer size allows you. It''s not that this code is "not working", rather, your code can cause unpredictable behavior. Your both get statements should not work, but the first one may occasionally work simply because the buffer is allocated first on your stack.

Please see: http://www.cplusplus.com/reference/istream/istream/get/[^].

—SA


这让我很伤心 - 有趣的话题......



阅读:两次使用cin.get() [ ^ ]
This is killing me - interesting subject...

Read this: Using cin.get() twice[^]


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

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