如何保存()所读的getchar整数数组? [英] How do I save integers read by getchar() to an array?

查看:381
本文介绍了如何保存()所读的getchar整数数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让将使用的getchar()读三个整数并将其存储在一个数组的程序,但事情是错误的,我code。

 的#include<&stdio.h中GT;
诠释的main()
{
    的printf(请输入三位数字100到999:);
    INT numEntered [2];
    numEntered =的getchar();
    的for(int i = 0;我3;;我++){
        的printf(%d个,numEntered [I])
    }
}


解决方案

试试这个:

 的#include<&stdio.h中GT;诠释主(){    的printf(请输入三位数字100到999:);    INT numEntered [3];    的for(int i = 0;我3;;我++){
        scanf函数(%d个,&安培; numEntered [I]);
        的printf(%d个,numEntered [I]);
    }    返回0;
}

你需要阅读里面的一个值,循环!第二件事情,用的getchar(),你所得到的字符的ASCII值,所以..阅读如果你看过1,并与%D打印,您实际打印49!

请参阅ASCII表中的位置: http://www.asciitable.com/index/asciifull。 GIF

I am trying to make a program that will use getchar() to read three integers and store it in an array but something is wrong in my code.

#include <stdio.h>
int main( )
{
    printf("Please enter three digit number 100 to 999: ");
    int numEntered[2];
    numEntered = getchar();
    for (int i = 0; i<3; i++) {
        printf("%d", numEntered[i])
    }
}

解决方案

Try this:

#include <stdio.h>

int main(){

    printf("Please enter three digit number 100 to 999: ");

    int numEntered[3];

    for (int i = 0; i<3; i++){
        scanf("%d", &numEntered[i]);
        printf("%d", numEntered[i]);
    }

    return 0;  
}

you need to read a value inside the for loop! Second thing, by reading with getchar(), you are getting the ascii value of the character, so.. if you read "1" and print with %d, you actually printing 49!

See the ascii table here: http://www.asciitable.com/index/asciifull.gif

这篇关于如何保存()所读的getchar整数数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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