开发者

Program not doing what it should - C

开发者 https://www.devze.com 2023-02-26 03:53 出处:网络
I wrote a program that receive from user a series of numbers (<=20) while the last \'0\' means end of series (not included in series storing). 2 arrays (x,y) size 20 (0-19 + 1 for the \'0\') must b

I wrote a program that receive from user a series of numbers (<=20) while the last '0' means end of series (not included in series storing). 2 arrays (x,y) size 20 (0-19 + 1 for the '0') must be zeros, and m means number of organs in Y array.

The user must enter numbers ascending (it is ok 4ex. 1,2,2,3,7,8,...,0) and end with a '0' of course, if not, appropriate error message will appear, and program will shut off.

We can be sure the user will keep the <=20 numbers of input.

Y array will be (if everything went ok with X array) a sorted array of X but without duplicates. 'm' will be number of organs in Y exclude '0' of course.

Function SIFT must only organize the Y array for being printed from main().

Example:

If user will store in X: 1,1,2,3,5,5,5,6

On screen will be: m = 5 Y = 1,2,3,5,6

My code:

#include <stdio.h>
#include <string.h>

void SIFT(int x_a开发者_StackOverflow中文版rr[ ], int y_arr[]);

int main ()
{
    int x[20] = {0} , y[20] = {0};
    int m=0,temp=0,curr=0,i=0,j=0;

    printf("Please enter your numbers now:\n\n");

    /*enter numbers one by one. if x[i+1] value < x[i] value, err msg.
      when user want to end the series he must enter '0' which means end of string (it wont       included in x[]) */
    while ( ( temp = getchar() ) != '0' )
    {
        if (temp >= curr)
        {
            x[i] = temp;
            curr = temp;
            i++;
        }
        else
        {
            printf("The numbers are not at the right order !\n\nProgram will now terminate...\n\n");
        }
    }

    SIFT(x,y);

    for (i=0 ; y[i]=='0' ; i++) /*strlen(y) without ('0')'s includes*/
        m++;

    /*Prints  m , y's organs*/
    printf("\n\nm = %d",m);
    printf("Y = ");
    while (y[j]!='0')
    {
        printf ("%d ,",y[j]);
        j++;
    }

return 0;
}

void SIFT(int x_arr[ ], int y_arr[])
{
    int i=0,j=0;

    while (x_arr[i] != '0')
    {
        if (x_arr[i] == x_arr[i+1]) /*if current val. equals next val. -> jump dbl at x_arr*/
        {
            y_arr[j] = x_arr[i];
            i+=2;
            j++;
        }
        else
        {
            y_arr[j]=x_arr[i];
            i++;
            j++;
        }
    }    

}

For some unknown reason, for any kind of legal input I'm getting a "The numbers are not at the right order..." error message...

I will be more than happy if someone can fix it so it should work properly, because until now I can see everything is seems to be OK.....

Thanks :( ...


getchar() does not do what you think it does. Read its description ( http://pubs.opengroup.org/onlinepubs/9699919799/functions/getchar.html ).

You can try scanf() instead ( http://pubs.opengroup.org/onlinepubs/9699919799/functions/scanf.html ), or better, fgets() and sscanf().


Fixing the input parsing (with scanf for instance), your code still has some problems:

1) You assume input is ordered (but not strictly). Therefore you don't need to store everything in x, you could at input time only compare the previous number and the current number. if current number is GREATER than previous (strictly), you store it, and increase m. If it is LESSER than the previous, you must exit. If they are equal, do nothing:

while ( scanf("%d",&temp) == 1 && temp != 0 && m < 20) {
    if (temp > curr) {
        y[m++] = temp;
    }
    else if (temp < curr) {
        printf("The numbers are not at the right order !\n\nProgram will now terminate...\n\n");
        exit(0);
    }
    curr = temp;
}

(therefore you don't need SIFT and the array x anymore).

2) You don't exit the program under the termination condition, you just print a message! (fixed above).

3) I put a m < 20 condition at the loop, just to make sure no segfault would occur at misbehaved inputs.

That should put you on the track. Observe that now that you're using scanf, the termination condition is not a char anymore ('0') but a number (0). And remember to #include <stdlib.h> at the top (for exit())


Most likely you're using getchar() instead of something like fgets(). Are you pressing newline after input numbers?


First, it would greatly help if you included in your Error Print statement the values of temp and curr.

Since others have said it, you do not want to read a char, you want to read a number. The links pmg has provided in his answer should get you to a solution. Also, debugging using breakpoints could have likely helped you identify this problem quick, I urge you to investigate some basic debugging skills.. Print Errors can only get you so far :)


You don't use getchar() to read a number, it's purpose it's to read a single character from the input buffer!

You need to call scanf() or fgets(), for example.


Also,

If I understood correct, I think you mean

for (i=0 ; y[i] != '0' ; i++) /* strlen */
    m++;
0

精彩评论

暂无评论...
验证码 换一张
取 消