Visual Studio Console Issues

Asked 2 years ago, Updated 2 years ago, 34 views

int oddCheck(int *param) {
    for (int i = 0; i < 10; i++) {
        if (param[i] % 2 == 1) {
            printf("%d ", param[i]);
        }
    }
    printf("\n");
}

int evenCheck(int *param) {
    for (int i = 0; i < 10; i++) {
        if (param[i] % 2 == 0) {
            printf("%d ", param[i]);
        }
    }
    printf("\n");
}



int main(void) 
{   
    int *numArray[10];
    int num;

    for (int i = 0; i < 10; i++) {
        printf("intput : ");
        scanf_s("%d\n", &num);
        numArray[i] = num;
    }
    oddCheck(numArray);
    evenCheck(numArray);
}

I organized the code like this, but there is no problem with the code itself, so from the debug console

printf("intput : ");

The above part is not properly printed in the second run. There's also a problem where the input comes out once again in the last part. I don't know why, but the last part should not come out, but I can also chew the integer I entered 11 because it came out. Thanks to you, the program is working fine, but it's very unpleasant to watch ㅠ<

c visual-studio-2017

2022-09-21 11:45

1 Answers

Wasn't there an error? Will it be compiled and move?

int *numArray[10];

is recognized in two dimensions. To be exact, I think it's a collection of pointers for ten entities.

int numArray[10] = {0};

You can do it with this. = It doesn't matter much if there's no {0}, but it's better if it's a regional variable.

On the other hand, the odCheck and evenCheck functions are int return, so you have to specify the return value or use it as a void type.

In addition, the scanf function does not automatically enter \n when receiving "%d\n", but when receiving a number, the input that you have to enter up to \n ends. That's why the move went off in the middle. If you want to implement moving on to the next line,

#include <stdio.h>
#include <stdlib.h>

using namespace std;
void oddCheck(int *param) {
    for (int i = 0; i < 10; i++) {
        if (param[i] % 2 == 1) {
            printf("%d ", param[i]);
        }
    }
    printf("\n");
}

void evenCheck(int *param) {
    for (int i = 0; i < 10; i++) {
        if (param[i] % 2 == 0) {
            printf("%d ", param[i]);
        }
    }
    printf("\n");
}



int main(void)
{
    int numArray[10] = {0};
    int num;

    for (int i = 0; i < 10; i++) {
        printf("intput : ");
        scanf_s("%d", &num);
        numArray[i] = num;
    }
    oddCheck(numArray);
    evenCheck(numArray);
    return 0;
}

You can do it with.


2022-09-21 11:45

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.