Click here to Skip to main content
15,904,877 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Till now I've re install my visual studio 2012 but still get one dumb error, what i'm doing wrong with this. . . .

C++
#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;
}
Posted
Comments
H.Brydon 26-Mar-13 16:59pm    
What is the error?
Usman Hunjra 26-Mar-13 17:01pm    
2nd cin statement is not executing .. ???
Sergey Alexandrovich Kryukov 26-Mar-13 17:08pm    
There is no such thing as "not executing". If should be some exception; so you need to provide comprehensive exception information. Use the debugger to see what's going on.
—SA
Irina Pykhova 26-Mar-13 17:05pm    
I haven't touch c++ for years, but can all this stuff be optimized by compiler? It's obvious that there is no work but returning 0.
Sergey Alexandrovich Kryukov 26-Mar-13 17:09pm    
Yes, of course, but it doesn't really matter. The bug is apparent.
—SA

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.

[Edit]
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]
 
Share this answer
 
v2
Comments
nv3 26-Mar-13 17:47pm    
That's a plausible story to me; except that the newline is not even getting eaten by the second cin.get(); instead the second get return an empty string and the newline is still in the buffer -- but that's a detail. My 5!
Sergey Alexandrovich Kryukov 26-Mar-13 19:02pm    
I see the point, my 5.
—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
 
Share this answer
 
Comments
nv3 26-Mar-13 17:45pm    
The wrong buffer size specification is one of the bugs in that code, but actually not the one that is causing the headaches, I guess. What's probably happening is:
He types a couple character + ENTER and the characters arrive correctly in array a. The ENTER key (newline) is not deposited in a, as stated by the documentation of "get". Now the second "get" executes and it sees the newline, which is again not deposited; instead b receives an empty string and the newline is still sitting in the buffer. And OP is wondering why b is empty and falsely assumed that the second get did not execute. That's what I think could have happened here. H.Brydon hit the nail, I guess.
Sergey Alexandrovich Kryukov 26-Mar-13 19:02pm    
Oh, thank you for the note. Funny enough.
—SA
Usman Hunjra 29-Mar-13 3:19am    
Thank You So Much Guys ..
Sergey Alexandrovich Kryukov 29-Mar-13 10:14am    
You are welcome.
—SA
This is killing me - interesting subject...

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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900