strcmp在c中的while循环中不起作用

时间:2015-04-26 17:11:35

标签: c while-loop scanf fgets strcmp

有人可以帮我解决这个问题吗?我试图用数字填充向量。问题(我认为)是那里的条件。当我输入" f"时,它应该离开while循环。但它没有用,虽然在被问及之前它还没有获得最多的元素。

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

int vecto();
int var;

double* vector; 
char* bv;

int vecto(){

int cont=0,ch;
char v[10];
free(vector);

printf ("¿Number of elements to order?: ");
scanf("%d",&var);
vector = (double*)malloc(var*sizeof(double));
printf("Input numbers press (f) for finish \n");
while((ch = fgetc(stdin)) != EOF && ch != '\n' ){};

do{
    fgets(v,sizeof(v),stdin);
    if((strcmp(v,"f"))!=0){ /*maybe here´s the problem*/
        vector[cont++]=strtod(v,&bv);
    }
} while(!((cont==var) || (strcmp(v,"f")==0)));/*maybe here's the problem*/

printf("\n");

return 0;
}

2 个答案:

答案 0 :(得分:1)

fgets()保留输入的ENTER。试试strcmp(v, "f\n")

答案 1 :(得分:1)

以下代码包含有关更改原因的注释

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

// prototypes
int vecto();

int var;

double* vector; 
char* bv;

int vecto()
{

    int cont=0;
    int ch;
    char v[10];

    // at this point, there is no allocated memory to free
    //free(vector);

    printf ("¿Number of elements to order?: ");

    // need to chec the returned value from scanf
    if( 1 != scanf("%d",&var) )
    { // then scanf failed
        perror( "scanf for number of elements to order failed" );
        exit( 1 );
    }

    // implied else, scanf successful

    // need to check the returned value from malloc
    // in C, do not cast the returned value from malloc
    if( NULL == (vector = malloc(var*sizeof(double)) ) )
    { // then malloc failed
        perror( "malloc for vector array failed" );
        exit( 1 );
    }

    // implied else, malloc successful

    printf("Input numbers press (f) for finish \n");

    // any time inputting, need to chack for terminator
    while( ((ch = fgetc(stdin)) != EOF) && (ch != '\n') && (ch != 'f') ){};

    if( 'f' == ch )
    { // then exiting early
        printf( "terminator entered before reading any lines\n" );
        free( vector ); // cleanup
    }

    else
    {
        do{
            if( NULL == fgets(v,sizeof(v),stdin) )
            { // then fgets failed
                perror( "fgets failed" );
                free(vector); // cleanup
                exit( 1 );
            }

            // implied else, fgets successful

            //if((strcmp(v,"f"))!=0)
            // keep code simple
            if( 'f' != v[0] )
            { // then terminator NOT entered by user

                // add 'v', as double, to end of vector
                // need to check that range of conversion was successful
                double tempDouble = strtod(v,&bv);  
                if( ERANGE == errno )
                { // then conversion failed
                    printf( "conversion of %s to double failed", v );
                }
                else
                {// else, conversion successful
                    vector[cont++]=tempDouble;
                }
            }
            else
            { // else, terminator entered by user
                cont = var;
            }
        // terminator already checked
        } while( cont < var );

        printf("\n");
    } // endif

    return 0;
} // end function: vecto