Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
// console application

Hi,
At the end of this program i want to hold the screen, so i put a getchar func. to hold the screen, but here one getchar won't work, why does it need to be two getchar to work ?

C++
#include "stdafx.h"
#include <iostream>
#include <stdio.h>
using namespace std;

int main()
{
	int arr[10];
	int num=0;
	int i=0;
	while ( true )
	{
		cout<<"please enter element "<<i<<" ::: ";
		cin>>num;
		if ( num < 0 ) break;
		arr[i]=num;
		for(int j=0; j<=i; j++)
			cout<< arr[j];
		i++;
		cout<<endl;
	}
	getchar();getchar();
}


Thanks,
Posted

Probably, the first one is "left over" data from the cin code you executed before.
The end of line character he user types after the number, perhaps? :laugh:
To check, comment out one of the getchar calls, and change true to false in your while loop.
 
Share this answer
 
Comments
m.r.m.40 26-Jul-14 7:26am    
I commented one getchar, and changed the true to false, it didn't work.
OriginalGriff 26-Jul-14 7:34am    
What did happen?
m.r.m.40 26-Jul-14 7:38am    
well, While loop doesn't start.
i replaced getchar right after the cin :

while ( true )
{
cout<<"please enter element "<<i<<" ::: ";
cin>>num;getchar();
if ( num < 0 ) break;
arr[i]=num;
for(int j=0; j<=i; j++)
cout<< arr[j];
i++;
cout<<endl;
}
getchar();

and it worked.
OriginalGriff 26-Jul-14 7:46am    
Ok...when I said "To check..." what I meant was "to check if this is causing the problem, do this temporarily to disable the cin calls and see if a single getchar was sufficient" - only I was too lazy to type all that...

Yes, changing the tree to false disables the loop - that was the idea. Now we know that it's a left over from the cin, and not some weird feature!

I''m not surprised: cin and getch are different systems, and mixing them together is going to cause fun-and-games.
Why not try this instead of getchar:
string userInput;
...
cout << "Press ENTER to continue: ";
cin >> userInput;
m.r.m.40 26-Jul-14 7:49am    
Yes,
It's definitely a better idea, Thank you for helping me.
This is a totally incorrect question. When answering, we have to assume you are showing you own code. And you should know why did you put some call twice.

If you don't know that, I would have to assume this is not your code. But it would mean that you should as the person who wrote it, not us. Isn't that logical?

So, the answer is: getchar is called twice because someone wrote so.

[EDIT]

Thank you for clarification. Please see my comment below. Your logic does not require holding the screen as the screen is held by cin >> num; you know for sure that Enter after "-1" ends the application.

—SA
 
Share this answer
 
v2
Comments
m.r.m.40 27-Jul-14 1:02am    
Well that's my own code ( what a great code! ),
and the question was that when i put one getchar to hold the screen it doesn't work, but by putting the second one it works, and we figured that out why does it work like it, then we fixed it, you can see the solution 1 and it's comments.

Thanks anyway,
Sergey Alexandrovich Kryukov 27-Jul-14 1:23am    
Very good. Yes, I can see your point. I just put some irony in the answer. Now, your first char could be "absorbed", because one character from the previous input was read as a char. You will be able to see it if you read both getchar into a variable and put the values in the debug output. It could be something like line feed or carriage return. Just check it up.

Besides, don't use it to hold the stream. Instead, read the key. Don't forget to output something like "Press any key" to the user...
—SA
m.r.m.40 27-Jul-14 1:27am    
I'll do that,
Thank you.

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