学习C - Scanf或Getch,或Getchar在第一次循环后无法正常工作。 [英] Learning C - Scanf or Getch, or Getchar not working correctly after first loop.

查看:61
本文介绍了学习C - Scanf或Getch,或Getchar在第一次循环后无法正常工作。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨大师,


我在Ivor Horton的帮助下编写了以下代码

起点C:


//结构,结构数组。


#include" stdafx.h"

#include" stdio.h"

#define MY_ARRAY 15

#define BASIS 360

struct swap

{

char counterParty [10];

浮动名义;

浮动intRate;

int busDays;

// int basis = 360;

浮动流量;

};


void main()

{


/ *结构阵列* /


交换myArraySwap [MY_ARRAY];

int hcount = 0;

int i = 0;

// =''\''';


/ / for(hcount = 0; hcount< 5; hcount ++)

do

{

char test = NULL;

printf(\\\
你想输入掉期(Y或N)吗?:");


test = getc har();


if(test!=''Y'')// || test!=''y'')

休息;


printf(" \\\
Enter CounterParty:");

scanf("%s",& myArraySwap [hcount] .counterParty);

printf(" \\\
Enter Notional:");

scanf ("%f",& myArraySwap [hcount] .notional);

printf(" \ nEnter输入利率:");

scanf(" ;%f",& myArraySwap [hcount] .intRate);

printf(" \\\
Enter Number of days:");

scanf(" %d",& myArraySwap [hcount] .busDays);

myArraySwap [hcount] .flows =(myArraySwap [hcount] .notional *

(myArraySwap [hcount] .intRate / 100.0f)*

((浮动)myArraySwap [hcount] .busDays / 360.0f));


hcount ++;

}

while(hcount< 5);


for(i = 0; i< hcount; i ++)

{

printf(" \ nCounter Party:%s",myArraySwap [hcount] .counterParty);

printf(" \\ nFlow%f",myArraySwap [i] .flows);

printf(" \ nDone");

}

}

问题:


第一次围绕var test =''''我可以输入一个值,即Y


这里是dos输出:


你想输入掉期(Y或N)吗?:Y


输入CounterParty:测试


输入名义:1000


输入利率:5.25


输入天数:35


你想输入掉期(Y或N)吗?:


第二次在test = getchar点附近();测试设置

自动为'0''我无法输入Y或N.


当应用到达时:test = getchar( );它自动将变量从$ 89设置为:0'''


这个编译器与我有vs.net 2003相关吗?


i也试过scanf并得到相同的结果:(


谢谢。


tesh

Hi Gurus,

I have written the following code with the help of Ivor Horton''s
Beginning C :

// Structures, Arrays of Structures.

#include "stdafx.h"
#include "stdio.h"
#define MY_ARRAY 15
#define BASIS 360

struct swap
{
char counterParty[10];
float notional;
float intRate;
int busDays;
//int basis = 360;
float flows;
};

void main()
{

/*Arrays of Structures*/

swap myArraySwap[MY_ARRAY];
int hcount = 0;
int i = 0;
//=''\0'';

// for (hcount = 0; hcount < 5 ; hcount++ )
do
{
char test = NULL;
printf("\nWould you like to enter Swaps (Y or N)?:");

test = getchar();

if (test != ''Y'') //|| test !=''y'')
break;

printf("\nEnter CounterParty:");
scanf("%s", &myArraySwap[hcount].counterParty );
printf("\nEnter Notional :" );
scanf("%f", &myArraySwap[hcount].notional );
printf("\nEnter Interest Rate:" );
scanf("%f", &myArraySwap[hcount].intRate );
printf("\nEnter Number of days:" );
scanf("%d", &myArraySwap[hcount].busDays );
myArraySwap[hcount].flows = (myArraySwap[hcount].notional *
(myArraySwap[hcount].intRate/100.0f) *
((float)myArraySwap[hcount].busDays/360.0f));

hcount++;
}
while (hcount < 5);

for (i=0 ; i < hcount ; i++)
{
printf("\nCounter Party: %s",myArraySwap[hcount].counterParty);
printf("\nFlows %f", myArraySwap[i].flows);
printf("\nDone");
}
}
Problem:

First time around the var test = '''' and I can enter a value i.e. Y

here is the dos output:

Would you like to enter Swaps (Y or N)?:Y

Enter CounterParty:Test

Enter Notional :1000

Enter Interest Rate:5.25

Enter Number of days:35

Would you like to enter Swaps (Y or N)?:

second time around the at the point of test = getchar(); test is set
to ''0 '' automatically and I am not able to enter Y or N.

when the app reaches:test = getchar(); it automatically sets the var
from Y 89 to : 0 ''''

is this compiler related i have vs.net 2003 ?

i have also tried scanf and get the same :(

thank you.

tesh

推荐答案

2月15日09:06,tesh ... @ googlemail.com< tesh ... @ googlemail.com> ;

写道:

主题:第一次循环后学习C - Scanf或Getch,或Getchar无法工作




注意C是一种类型敏感的语言,因此Scanf等应该写成扫描信息。


您是否阅读过常见问题解答,可在以下网址找到: -
http:// c-faq。 com / stdio / index.html


尝试:


12.18a我正在读一个带有scanf的数字和%d,然后一个字符串与

得到()

但编译器似乎是skippi调用gets()"


12.18b"我正在使用scanf%c来读取Y / N响应,但稍后输入

被跳过。

12.20为什么每个人都说不使用scanf?我该怎么用

呢?"


因为这些可能会回答你的问题
On 15 Feb, 09:06, "tesh...@googlemail.com" <tesh...@googlemail.com>
wrote:
subject: Learning C - Scanf or Getch, or Getchar not working
correctly after first loop.

note C is a type sensitive language so Scanf etc. should
be written scanf.

Have you read the FAQ, which may be found at:-
http://c-faq.com/stdio/index.html

Try:

12.18a "I''m reading a number with scanf and %d, and then a string with
gets()
but the compiler seems to be skipping the call to gets()"

12.18b "I''m using scanf %c to read a Y/N response, but later input
gets skipped."
12.20 "Why does everyone say not to use scanf? What should I use
instead?"

as these may answer your questions

我在Ivor Horton的帮助下编写了以下代码

起始C:
I have written the following code with the help of Ivor Horton''s
Beginning C :



我会找到一本更好的书。你有没有试过

" The C Programming Language"作者:Kernighan& Richie?

I''d find a better book. Have you tried
"The C Programming Language" by Kernighan & Richie?


//结构,结构数组。


#include" stdafx.h"
// Structures, Arrays of Structures.

#include "stdafx.h"



非标准标题

non-standard header


#include" stdio.h"
#include "stdio.h"



标准标题应使用此语法


#include< stdio.h>

standard headers should use this syntax

#include <stdio.h>


#define MY_ARRAY 15

#define BASIS 360

struct swap

{

char counterParty [10];

浮动名义;
#define MY_ARRAY 15
#define BASIS 360

struct swap
{
char counterParty[10];
float notional;



更喜欢double to float,除非你*真的*需要节省空间。

prefer double to float unless you *really* need to save space.


float intRate;

int busDays;

// int basis = 360;

浮动流量;

};


void main()
float intRate;
int busDays;
//int basis = 360;
float flows;
};

void main()



int main(void)

int main(void)


{


/ *结构数组* /
{

/*Arrays of Structures*/



不是一个非常有用的评论。好的,当你开始时。

not a very useful comment. Ok when you are starting out.


swap myArraySwap [MY_ARRAY];


int hcount = 0;

int i = 0;

// =''\''';


// for(hcount = 0; hcount< ; 5; hcount ++)

do

{

char test = NULL;
swap myArraySwap[MY_ARRAY];

int hcount = 0;
int i = 0;
//=''\0'';

// for (hcount = 0; hcount < 5 ; hcount++ )
do
{
char test = NULL;



不,NULL通常是指针常量。使用''\0''代表

a nul charcter。

no, NULL is usually a pointer constant. Use ''\0'' to represent
a nul charcter.


printf(&\\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n你想要输入掉期( Y或N)?:);
printf("\nWould you like to enter Swaps (Y or N)?:");



fflush(stdout);

fflush(stdout);


>

test = getchar ();


if(test!=''Y'')// || test!=''y'')

休息;


printf(" \\\
Enter CounterParty:");

scanf("%s",& myArraySwap [hcount] .counterParty);

printf(" \\\
Enter Notional:");

scanf ("%f",& myArraySwap [hcount] .notional);

printf(" \ nEnter输入利率:");

scanf(" ;%f",& myArraySwap [hcount] .intRate);

printf(" \\\
Enter Number of days:");

scanf(" %d",& myArraySwap [hcount] .busDays);

myArraySwap [hcount] .flows =(myArraySwap [hcount] .notional *

(myArraySwap [hcount] .intRate / 100.0f)*

((浮动)myArraySwap [hcount] .busDays / 360.0f));


hcount ++;

}

while(hcount< 5);


for(i = 0; i< hcount; i ++)

{

printf(" \\\
Counter Party:%s",myArraySwap [hcount] .counterParty);

printf(" \ nFlow%f",myArraySwap [i] .flows);

printf(" \ nDone");

}


}


问题:


第一次围绕var test =''''我可以输入一个值,即Y


这里是dos输出:


你想输入掉期(Y或N)吗?:Y


输入CounterParty:测试

输入名义价值:1000


输入利率:5.25


输入天数:35


你想输入掉期(Y或N)吗?:


第二次在test = getchar();测试设置

自动为'0''我无法输入Y或N.


当应用到达时:test = getchar( );它自动将变量从$ 89设置为:0'''


这个编译器与我有vs.net 2003相关吗?


i也尝试过scanf并得到相同的结果:(
>
test = getchar();

if (test != ''Y'') //|| test !=''y'')
break;

printf("\nEnter CounterParty:");
scanf("%s", &myArraySwap[hcount].counterParty );
printf("\nEnter Notional :" );
scanf("%f", &myArraySwap[hcount].notional );
printf("\nEnter Interest Rate:" );
scanf("%f", &myArraySwap[hcount].intRate );
printf("\nEnter Number of days:" );
scanf("%d", &myArraySwap[hcount].busDays );
myArraySwap[hcount].flows = (myArraySwap[hcount].notional *
(myArraySwap[hcount].intRate/100.0f) *
((float)myArraySwap[hcount].busDays/360.0f));

hcount++;
}
while (hcount < 5);

for (i=0 ; i < hcount ; i++)
{
printf("\nCounter Party: %s",myArraySwap[hcount].counterParty);
printf("\nFlows %f", myArraySwap[i].flows);
printf("\nDone");
}

}

Problem:

First time around the var test = '''' and I can enter a value i.e. Y

here is the dos output:

Would you like to enter Swaps (Y or N)?:Y

Enter CounterParty:Test

Enter Notional :1000

Enter Interest Rate:5.25

Enter Number of days:35

Would you like to enter Swaps (Y or N)?:

second time around the at the point of test = getchar(); test is set
to ''0 '' automatically and I am not able to enter Y or N.

when the app reaches:test = getchar(); it automatically sets the var
from Y 89 to : 0 ''''

is this compiler related i have vs.net 2003 ?

i have also tried scanf and get the same :(



-

Nick Keighley


--
Nick Keighley


te*****@googlemail.com 说:
te*****@googlemail.com said:

嗨大师,


我在Ivor Horton的帮助下编写了以下代码

起始价C:
Hi Gurus,

I have written the following code with the help of Ivor Horton''s
Beginning C :



我建议你买一本更好的书。从你的代码看,Horton先生

需要在尝试写一本关于它的书之前学习C.

I suggest you get a better book. From the look of your code, Mr Horton
needs to learn C before he tries writing a book about it.


>

//结构,结构数组。


#include" stdafx.h"
>
// Structures, Arrays of Structures.

#include "stdafx.h"



C语言没有定义这样的标题。

No such header is defined by the C language.


#include" st dio.h
#include "stdio.h"



既然您想要拿起标准标题,那么使用它可能会更好。

使用:


#include< stdio.h>

Since you want to pick up the standard header, it''s probably better to
use:

#include <stdio.h>


#define MY_ARRAY 15

#define BASIS 360 < br $>
struct swap

{

char counterParty [10];

浮动名义;

float intRate;

int busDays;

// int basis = 360;

浮动流量;

};


void main()
#define MY_ARRAY 15
#define BASIS 360

struct swap
{
char counterParty[10];
float notional;
float intRate;
int busDays;
//int basis = 360;
float flows;
};

void main()



int main(void)

int main(void)


{


/ *结构阵列* /


交换myArraySwap [MY_ARRAY];


int hcount = 0;

int i = 0;

// =''\''';


// for(hcount = 0; hcount< 5; hcount ++)

do

{

char test = NULL;
{

/*Arrays of Structures*/

swap myArraySwap[MY_ARRAY];
int hcount = 0;
int i = 0;
//=''\0'';

// for (hcount = 0; hcount < 5 ; hcount++ )
do
{
char test = NULL;



NULL用作指针值,而不是char值。如果你不希望测试的价值不确定,请使用0作为初始值。

此外,你将把它用于getchar, - 尽管它的名字是

- 返回int,所以make test是一个int,而不是char。 (有一个很好的理由

这个在现阶段会有些过分。)

NULL is intended for use as a pointer value, not a char value. Use 0 for
an initialiser if you don''t want test''s value to be indeterminate.
Furthermore, you''ll be using this for getchar, which - despite its name
- returns int, so make test an int, not a char. (There is a good reason
for this which would be overkill to go into at this stage.)


printf(" \你想输入掉期(Y或N)吗?:");


test = getchar();


if(test !=''Y'')// ||测试!=''y'')

休息;
printf("\nWould you like to enter Swaps (Y or N)?:");

test = getchar();

if (test != ''Y'') //|| test !=''y'')
break;



嗯,是的,这是*摆脱循环的一种方式,但不是这样的方式

我已经选择了。


现在,当你运行你的程序时,你按'Y'键,然后你

按输入键,是吗? getchar()调用将从标准输入流的

中检索''Y'并将其值赋给''test'',但是

仍会留下流中的换行符(因为你按了

''ENTER''),还有待阅读。接下来的输入函数调用

然后将检索它。但你不希望/你的scanf调用

检索它。你只想扔掉它。你可以这样做:


getchar(); / *读取并丢弃换行符* /


这不是很强大(如果用户键入是而不是Y怎么办?),

但是现在它会让你重回正轨。

Well, yes, that''s *one* way to get out of a loop, but it isn''t the way
I''d have chosen.

Now, when you run your program, you press the ''Y'' key, and then you
press the ENTER key, yes? The getchar() call will retrieve the ''Y'' from
the standard input stream and assign its value to ''test'', but that
still leaves a newline character in the stream (because you pressed
''ENTER''), and that has yet to be read. The next input function call,
then, will retrieve it. But you don''t /want/ your scanf call to
retrieve it. You just want to throw it away. You can do so like this:

getchar(); /* read and discard newline character */

This isn''t very robust (what if the user typed ''Yes'' rather than ''Y''?),
but it''ll get you back on track for now.


>

printf(" \ n输入CounterParty:);

scanf("%s",& myArraySwap [hcount] .counterParty);
>
printf("\nEnter CounterParty:");
scanf("%s", &myArraySwap[hcount].counterParty );



这里有几个问题。首先,永远不要将%s与scanf一起使用。如果你
必须使用scanf来读取字符串,请指定缓冲区可以支持的字符串

的最大长度。在这种情况下,你的struct的counterParty成员你的struct有10个元素,所以它可以支持长度最多为9

个字符的字符串,所以使用%9s而不是%秒。其次,你是
,希望提供指向缓冲区中第一个字符的指针,

不是指向数组的指针。所以使用带有前导&符号的
& myArraySwap [hcount] .counterParty [0]


但指定<的第一个字符br />
数组作为它的操作数,OR,更简单和等价的含义:


myArraySwap [hcount] .counterParty


(没有主要的&符号)。


另外,scanf可能会失败,所以一定要检查返回值。


那个'''我将继续这样做。


< snip>


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上述域名, - www。

Several problems here. Firstly, never ever use %s with scanf. If you
must use scanf to read a string, specify the maximum length of string
that your buffer can support. In this case, the counterParty member of
your struct has ten elements, so it can support a string up to 9
characters in length, so use %9s rather than %s. Secondly, you are
expected to provide a pointer to the first character in your buffer,
not a pointer to the array. So use EITHER

&myArraySwap[hcount].counterParty[0]

with the leading ampersand but specifying the first character in the
array as its operand, OR, more simply and equivalent in meaning:

myArraySwap[hcount].counterParty

(without the leading ampersand).

Also, scanf can fail, so be sure you check the return value.

That''ll do to be going on with.

<snip>

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.


Richard Heathfield< rj*@see.sig.invalidwrites :
Richard Heathfield <rj*@see.sig.invalidwrites:
te*****@googlemail.com 说:

>嗨大师,

我在Ivor Horton的帮助下编写了以下代码
开头C:
>Hi Gurus,

I have written the following code with the help of Ivor Horton''s
Beginning C :



我建议你买一本更好的书。从代码的外观来看,Horton先生

在尝试写一本关于它的书之前需要学习C.


I suggest you get a better book. From the look of your code, Mr Horton
needs to learn C before he tries writing a book about it.



你的傲慢无所不知吗?您是否从未看过写过K& R的人写的坏代码


Does your arrogance know no bounds? Did you never see bad code written
by people who read K&R?


这篇关于学习C - Scanf或Getch,或Getchar在第一次循环后无法正常工作。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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