Getchar,为什么需要两个getchar? [英] Getchar, Why does it need two getchar ?

查看:380
本文介绍了Getchar,为什么需要两个getchar?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

//控制台应用程序





在这个程序结束时我想要按住屏幕,所以我放了一个getchar功能。拿着屏幕,但是这里有一个getchar无法工作,为什么需要两个getchar工作?



 < span class =code-keyword> #include     stdafx.h 
#include < iostream >
#include < stdio .h >
使用 namespace std;

int main()
{
int arr [ 10 ];
int num = 0 ;
int i = 0 ;
while true
{
cout<< < span class =code-string> 请输入元素<< 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();
}





谢谢,

解决方案

可能,第一个是你之前执行的 cin 代码中的遗留数据。

用户在数字后输入的行尾字符,也许? :笑:

要检查,注释掉其中一个 getchar 调用,并更改 true 中的 false ,而循环。


这是完全错误的题。在回答时,我们必须假设您正在显示自己的代码。 你应该知道你为什么要打两次电话。



如果你不知道,我不得不假设这是不是你的代码。但这意味着你应该作为写作者,而不是我们。这不合逻辑吗?



所以答案是: getchar 被叫两次因为有人写了这么







谢谢你的澄清。请参阅下面的评论。您的逻辑不需要按住屏幕,因为屏幕由 cin>>保持。 num; 你肯定知道在-1之后输入结束申请。



-SA

// 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 ?

#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,

解决方案

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.


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


这篇关于Getchar,为什么需要两个getchar?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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