getchar和字符数组 [英] getchar and character arrays

查看:84
本文介绍了getchar和字符数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我不能用0开始我的字符数据下标。

在这个程序中我不能这样做:


do

{

na [i] = getchar();

i ++;

na [i] = getchar();


} while(na [i]!=''\ n'');

我的程序:

* - ---------------------

无效输入(字符* na)

{


int i = 0;


printf("输入名称:");

do

{

i ++;


na [i] = getchar();


} while( na [i]!=''\ n'');

}

Why I can not begin my subscript of character arrrays with 0.
In this program I can not do :

do
{
na[i]=getchar();
i++;
na[i]=getchar();

} while (na[i]!=''\n'');
my program :
*-----------------------
void input(char *na)
{

int i=0;

printf ("Enter the name : ");
do
{
i++;

na[i]=getchar();

} while (na[i]!=''\n'');
}

推荐答案

< a href =mailto:eh ********** @ gmail.com> eh ********** @ gmail.com 写道:

>

为什么我不能用0开始我的字符数据下标。

在这个程序中我做不到:


do

{

na [i] = getchar();

i ++;

na [i] = getchar();


} while(na [i]!=''\ n'');


我的程序:

* -----------------------

无效输入(字符* na)

{


int i = 0;
>
Why I can not begin my subscript of character arrrays with 0.
In this program I can not do :

do
{
na[i]=getchar();
i++;
na[i]=getchar();

} while (na[i]!=''\n'');

my program :
*-----------------------
void input(char *na)
{

int i=0;



你的索引工作正常,一点,
如果你用这种方式初始化



int i = -1;

Your indexing would work OK, up to a point,
if you initialized this way:

int i = -1;


printf("输入名称:");

do

{

i ++;


na [i] = getchar();


}而(na [i]!=''\ n'');

}
printf ("Enter the name : ");
do
{
i++;

na[i]=getchar();

} while (na[i]!=''\n'');
}



/ * BEGIN pops_device.c * /

/ *

**如果rc等于0,则输入空行

**并且数组包含垃圾值。

**如果rc等于EOF,则达到文件结尾,

**或存在输入问题。

**如果rc等于1,然后在数组中有一个字符串。

**从文本流的一行读取最多LENGTH字符数

**。

**如果该行超过LENGTH,

**则额外的字符将被丢弃。

* /

#包括< stdio.h>


#define LENGTH 50

#define str(x)#x

#define xstr(x)str(x)


int main(无效)

{

int rc;

char数组[LENGTH + 1];


puts(LENGTH宏是 xstr(LENGTH));

fputs("输入带空格的行:",stdout);

fflush(stdout);

rc = fscanf(stdin,"%" xstr(LENGTH)" [^ \ n]%* [^ \ n]",array);

if(!feof (stdin)){

getc(stdin);

}

而(rc == 1){

printf(你的字符串是:%s \ n \\ nn>

"点击Enter键结束,\ nor输入"

" ;另一行继续:,数组;

fflush(stdout);

rc = fscanf(stdin,"%" xstr(LENGTH)" [^ \ n]%* [^ \ n]",array);

if(!feof(stdin)){

getc(stdin);

}

if(rc == 0){

* array =''\ 0'';

}

}

返回0;

}


/ * END pops_device.c * /


-

pete

/* BEGIN pops_device.c */
/*
** If rc equals 0, then an empty line was entered
** and the array contains garbage values.
** If rc equals EOF, then the end of file was reached,
** or there is some input problem.
** If rc equals 1, then there is a string in array.
** Up to LENGTH number of characters are read
** from a line of a text stream.
** If the line is longer than LENGTH,
** then the extra characters are discarded.
*/
#include <stdio.h>

#define LENGTH 50
#define str(x) # x
#define xstr(x) str(x)

int main(void)
{
int rc;
char array[LENGTH + 1];

puts("The LENGTH macro is " xstr(LENGTH));
fputs("Enter a line with spaces:", stdout);
fflush(stdout);
rc = fscanf(stdin, "%" xstr(LENGTH) "[^\n]%*[^\n]", array);
if (!feof(stdin)) {
getc(stdin);
}
while (rc == 1) {
printf("Your string is:%s\n\n"
"Hit the Enter key to end,\nor enter "
"another line to continue:", array);
fflush(stdout);
rc = fscanf(stdin, "%" xstr(LENGTH) "[^\n]%*[^\n]", array);
if (!feof(stdin)) {
getc(stdin);
}
if (rc == 0) {
*array = ''\0'';
}
}
return 0;
}

/* END pops_device.c */

--
pete


pete写道:
pete wrote:

/ * BEGIN pop s_device.c * /

/ *

**如果rc等于0,则输入一个空行

**并且数组包含垃圾值。

**如果rc等于EOF,则达到文件结尾,

**或存在输入问题。

**如果rc等于1,则数组中有一个字符串。

**最多读取LENGTH字符数

**来自文本行流。

**如果该行超过LENGTH,

**则会丢弃额外的字符。

* /

#include< stdio.h>


#define LENGTH 50

#define str(x)#x

#define xstr(x)str(x)


int main(无效)

{

int rc;

char数组[LENGTH + 1];


puts(LENGTH宏是 xstr(LENGTH));

fputs("输入带空格的行:",stdout);

fflush(stdout);

rc = fscanf(stdin,"%" xstr(LENGTH)" [^ \ n]%* [^ \ n]",array);

if(!feof (stdin)){

getc(stdin);

}

而(rc == 1){

printf(你的字符串是:%s \ n \\ nn>

"点击Enter键结束,\ nor输入"

" ;另一行继续:,数组;

fflush(stdout);

rc = fscanf(stdin,"%" xstr(LENGTH)" [^ \ n]%* [^ \ n]",array);

if(!feof(stdin)){

getc(stdin);

}
/* BEGIN pops_device.c */
/*
** If rc equals 0, then an empty line was entered
** and the array contains garbage values.
** If rc equals EOF, then the end of file was reached,
** or there is some input problem.
** If rc equals 1, then there is a string in array.
** Up to LENGTH number of characters are read
** from a line of a text stream.
** If the line is longer than LENGTH,
** then the extra characters are discarded.
*/
#include <stdio.h>

#define LENGTH 50
#define str(x) # x
#define xstr(x) str(x)

int main(void)
{
int rc;
char array[LENGTH + 1];

puts("The LENGTH macro is " xstr(LENGTH));
fputs("Enter a line with spaces:", stdout);
fflush(stdout);
rc = fscanf(stdin, "%" xstr(LENGTH) "[^\n]%*[^\n]", array);
if (!feof(stdin)) {
getc(stdin);
}
while (rc == 1) {
printf("Your string is:%s\n\n"
"Hit the Enter key to end,\nor enter "
"another line to continue:", array);
fflush(stdout);
rc = fscanf(stdin, "%" xstr(LENGTH) "[^\n]%*[^\n]", array);
if (!feof(stdin)) {
getc(stdin);
}



我想这个getc是抓住''\ n''仍然停留在stdin中。为什么不


rc = fscanf(stdin,"%" xstr(LENGTH)" [^ \ n]%* [^ \ n]%* c",数组);


%* c应匹配一个字符而不存储它,对吧?然后你

不需要if(!feof(stdin)){getc(stdin); }

I suppose this getc is to grab the ''\n'' still stuck in stdin. Why not

rc = fscanf(stdin, "%" xstr(LENGTH) "[^\n]%*[^\n]%*c", array);

The %*c should match one character and not store it, right? Then you
don''t need the if(!feof(stdin)) { getc(stdin); }


if(rc == 0){

* array =''\ 0'';

}

}

返回0;

}


/ * END pops_device.c * /
if (rc == 0) {
*array = ''\0'';
}
}
return 0;
}

/* END pops_device.c */



-

Simon。

--
Simon.


Simon Biber写道:
Simon Biber wrote:

为什么不


rc = fscanf(stdin,"%" xstr(LENGTH)" [^ \ n]%* [^ \ n]%* c",array);
Why not

rc = fscanf(stdin, "%" xstr(LENGTH) "[^\n]%*[^\n]%*c", array);



谢谢。

我不知道怎么让这条线路循环工作。

随后调用fscanf,返回0.


/ * BEGIN pops_device.c * /

/ *

**如果rc等于0,则输入空行

**并且数组包含垃圾值。

**如果rc等于EOF,则达到文件结尾,

**或者有一些输入问题。

**如果rc等于1,那么数组中有一个字符串。

* *从文本流的一行读取最多LENGTH个字符数

**

**如果该行长于LENGTH,

**然后额外的字符被丢弃。

* /

#include< stdio.h>


#define OLD_WAY 1


#define LENGTH 50

#define str(x)#x

#define xstr(x) str(x)


int main(无效)

{

int rc;

char数组[LENGTH + 1];


puts(OLD_WAY is xstr(OLD_WAY));

puts(LENGTH宏是xstr(LENGTH));

fputs("输入带空格的行:" ,stdout);

fflush(stdout);


#if OLD_WAY!= 0

rc = fscanf(stdin,& ;%" xstr(LENGTH)" [^ \ n]%* [^ \ n]",array);

if(!feof(stdin)){

getc(stdin);

}

#else

rc = fscanf(stdin,"%" xstr( LENGTH)" [^ \ n]%* [^ \ n]%* c",array);

#endif

if(1 rc){

* array =''\''';

}

printf(" rc是%d。你的字符串是:% s\\\
\ n,rc,array);

while(rc == 1){

fputs("点击Enter键结束,\\ n \\ n输入

另一行继续:",stdout);

fflush(stdout);


#if OLD_WAY!= 0

rc = fscanf(stdin,"%" xstr(LENGTH)" [^ \ n]%* [^ \ n]",数组);
if(!feof(stdin)){

getc(stdin);

}

#else

rc = fscanf(stdin,"%" xstr(LENGTH)" [^ \ n]%* [^ \ n]%* c",array);

#endif


if(1 rc){

* array =''\ 0'';

}

printf(" rc is%d你的字符串是:%s \ n \ n",rc,array);

}

返回0;

}


/ * END pops_device.c * /

-

pete

Thank you.
I don''t know how to make that line work in a loop.
A subsequent call to fscanf, returns 0.

/* BEGIN pops_device.c */
/*
** If rc equals 0, then an empty line was entered
** and the array contains garbage values.
** If rc equals EOF, then the end of file was reached,
** or there is some input problem.
** If rc equals 1, then there is a string in array.
** Up to LENGTH number of characters are read
** from a line of a text stream.
** If the line is longer than LENGTH,
** then the extra characters are discarded.
*/
#include <stdio.h>

#define OLD_WAY 1

#define LENGTH 50
#define str(x) # x
#define xstr(x) str(x)

int main(void)
{
int rc;
char array[LENGTH + 1];

puts("OLD_WAY is " xstr(OLD_WAY));
puts("The LENGTH macro is " xstr(LENGTH));
fputs("Enter a line with spaces:", stdout);
fflush(stdout);

#if OLD_WAY != 0
rc = fscanf(stdin, "%" xstr(LENGTH) "[^\n]%*[^\n]", array);
if (!feof(stdin)) {
getc(stdin);
}
#else
rc = fscanf(stdin, "%" xstr(LENGTH) "[^\n]%*[^\n]%*c", array);
#endif
if (1 rc) {
*array = ''\0'';
}
printf("rc is %d. Your string is:%s\n\n", rc, array);
while (rc == 1) {
fputs("Hit the Enter key to end,\nor enter "
"another line to continue:", stdout);
fflush(stdout);

#if OLD_WAY != 0
rc = fscanf(stdin, "%" xstr(LENGTH) "[^\n]%*[^\n]", array);
if (!feof(stdin)) {
getc(stdin);
}
#else
rc = fscanf(stdin, "%" xstr(LENGTH) "[^\n]%*[^\n]%*c", array);
#endif

if (1 rc) {
*array = ''\0'';
}
printf("rc is %d. Your string is:%s\n\n", rc, array);
}
return 0;
}

/* END pops_device.c */
--
pete


这篇关于getchar和字符数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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