C++中字符数组的输入
PHP中文网
PHP中文网 2017-04-17 14:45:24
0
4
496

如上图,红线框起来的部分,为什么字符数组的输入放到里面出来的结果是不正确的,而按照下面的方法,放到循环的外面去输入运行结果是正确的?

正确放法图
下面放完整代码:

#include<iostream>
#include<stdio.h>
using namespace std;
int main(){
        char n[100] = { 0 };
        int sum = 0;
        cin>>n;
        int i, number, mask = 1;
        for (i = 0; n[i] != 0; i++) {
            sum += (int)n[i] - 48;
        }
        int a = sum;
        while (a>9){
            a /= 10;
            mask *= 10;
        }

        do{
            number = sum / mask;
            sum %= mask;
            mask /= 10;
            switch (number){
            case 0:printf("ling"); break;
            case 1:printf("yi"); break;
            case 2:printf("er"); break;
            case 3:printf("san"); break;
            case 4:printf("si"); break;
            case 5:printf("wu"); break;
            case 6:printf("liu"); break;
            case 7:printf("qi"); break;
            case 8:printf("ba"); break;
            case 9:printf("jiu"); break;
            }
            if (mask>0){
                printf(" ");
            }
        } while (mask>0);

    system("pause");
    return 0;
}

这个题的题目
![图片描述][3]

我的错误的代码:

#include<iostream>
#include<stdio.h>
using namespace std;
int main(){
    char n[100] = { 0 };
    int sum = 0, number, mask = 1;
    for (int i = 0; i != 0; i++){
        cin >> n[i];
        sum = sum + (int)n[i] - 48;
    }
    int a = sum;
    while (a > 9){
        a = a / 10;
        mask = mask * 10;
    }
    do{
        number = sum / mask;
        sum = sum % mask;
        mask = mask / 10;
        switch (number){
        case 0:printf("ling");
            break;
        case 1:printf("yi");
            break;
        case 2:printf("er");
            break;
        case 3:printf("san");
            break;
        case 4:printf("si");
            break;
        case 5:printf("wu");
            break;
        case 6:printf("liu");
            break;
        case 7:printf("qi");
            break;
        case 8:printf("ba");
            break;
        case 9:printf("jiu");
            break;
        default:break;
            if (mask > 0){
                printf(" ");
            }
        }

    } while (mask > 0);
    system("pause");
    return 0;
}

为什么字符数组的输入不放到循环里面呢?而且放到里面的运行结果是错误的。

PHP中文网
PHP中文网

认证0级讲师

reply all(4)
洪涛

As mentioned above, this is an infinite loop!

Just cin>>n and enter the characters. Is this the problem? Is it because the system opens a buffer in the memory during input to temporarily store the data of the input and output streams, and then takes out the data from the buffer and assigns it to n? Is n the first address of the array? So it is equivalent to assigning to Array n[100]; For your question, look at the input and output streams of C++.

Peter_Zhu

In fact, cin can be placed inside or outside the loop.
Needless to say anything outside the loop, you’ve got it all done.
There is actually a logic error in the loop. The code in the loop has never been executed.
In the first line, you have initialized n[100] to '0'*100,
so the result of the subsequent loop condition is always False, because n[i] is always equal to 0.
The program will always ignore the loop and jump to line 11 to continue execution.

巴扎黑

Because this is an IO operation, the system call speed is slow, so generally more data is read at one time and then buffer is used for caching. The cin here should be line buffered, which reads one line of data at a time. If you want to use a loop, you need to use the getchar function, but this is inefficient and not recommended.

伊谢尔伦

for (int i = 0; i != 0; i++){What the hell

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!