通过for循环和if语句传递指针

时间:2016-10-30 13:19:14

标签: c pointers if-statement for-loop

我试图通过这个for循环传递指针,但它不起作用。这只是我原始代码的一个块。我不想用200行代码超载你们所有人!如果您需要全部,请询问。

确定!所以我使用了3个printf来查看我的代码在崩溃之前得到了多远。它到达printf(" 2");在它崩溃之前。所以我认为它与if ( *(userInput + i ) == sNumArray[j] )有关。我不确定我做错了什么。据我所知,指针不使用指针[i]循环遍历每个元素,他们使用*( pointer + i )

我4周前才开始使用C编程,如果我没有彻底解释这一点,那就很抱歉。我还在学习术语等等。

for ( i = 0; i < sUserInput_SIZE; i++ ) {

    printf( "1" );

    for ( j = 0; j < sNumArray_SIZE; j++ ) {

        printf( "2" );

        if ( *(userInput + i) == sNumArray[j] ) {

            validInput++;
            printf( "3" );

        }//End if( )

    }//End inner for( )

}//End outer for( )

这是我的源代码 我正在尝试创建一个函数,检查用户输入的是否是一个数字。主代码是一个atm,允许用户输入其引脚,更改引脚并查看引脚输入的次数。

我知道你可以使用isdigit函数进行错误检查,但是我想尝试为了学习目的而创建自己的函数,我已经花了很多时间在这上面而且我很顽固地让它去尝试别的东西

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

#define sUserInput_SIZE 5
#define sNumArray_SIZE 10


char * errorChecking( char *userInput ) {

    //VARIABLE LIST
    //Note: Each variable will have the alphabetical character associated with its data structure at the beginning of its name e.g. integer data structures will have the charater "i" at the beginning of the variable etc
    //Outer for loop variable
    unsigned i;
    //Inner for loop variable
    unsigned j;
    // validInput will be compared with strlen( ) function which is an unsigned int........more?
    unsigned iValidInput = 0;

    //ARRAY LIST

    //This array will be used to check each inputed character to check if it is a number
    char sNumArray[sNumArray_SIZE] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

    //End of Declarations


    //This for loop will cross reference each element the user inputed with the number array
    for ( i = 0; i < sUserInput_SIZE; i++ ) {

        for( j = 0; j < sNumArray_SIZE; j++ ) {

            if ( *(userInput + i ) == sNumArray[j] ) {

                //Every time a number is successfully verified as a number the "validInput" variable will be incremented by 1 
                iValidInput++;

            }//End if( )

        }//End inner for( )

    }//End outer for( )


    //This if statement will check if the inputed value is an integer or not by checking if the number of valid inputs equalls the length of the sUserInput array
    if ( validInput == strlen( userInput ) ){

        printf( "\n\nIs a integer" );
        return( userInput)

    }//End if( )
    else {
        printf( "\n\nIs " );
        printf( "\n\nError: This is not a integer \nTry again: " );
        scanf("%s" , &userInput );


    }//End else



    return( userInput );

}//End main( )

//FIX ME: only add convert it input is verified
//FIX ME: Loop back around if it is not a number

int main( ) {

    //VARIABLE LIST
    //Note: Each variable will have the alphabetical character associated with its data structure at the beginning of its name e.g. integer data structures will have the charater "i" at the beginning of the variable etc

    int iExitLoop = 1;
    unsigned int iCorrectInputs = 0;
    unsigned int iIncorrectInputs = 0;
    char *iNewUserPin = "0";
    char *iUserPin = "1234";
    char *sUserInput = "1";


    //End of Declarations


    while ( iExitLoop == 1 ) {

        //Main menu
        printf( "\n1: Enter pin" );
        printf( "\n2: Change pin" );
        printf( "\n3: Successful and unsuccessful pin logs" );
        printf( "\n4: Exit" );
        printf( "\n\n%s" , sUserInput );

        //Prompting the user to entered in an option
        printf( "\n\nEnter: " );
        scanf( "%s" , &sUserInput );
        printf( "%s" , *sUserInput);

        //Prompting user to enter pin
        if ( strncmp( sUserInput , "1" , 1 ) != 0 ) {

            //This do while loop will prompt the user to enter in their pin and keep running until the correct pin is entered
            do {
                printf( "\nPlease enter your pin: " );
                scanf( "%s" , &sUserInput );
                errorChecking( sUserInput );

                if ( sUserInput == iUserPin ) {
                    iCorrectInputs++;
                }//End if( )
                else {
                    iIncorrectInputs++;
                    printf( "\nTry again!" );
                }//End else
            } while ( sUserInput != iUserPin );//End Do While( )
            //FIX ME - ADD ERROR CHECKING ( FUNCTIONS? )

        }//End if( )


        //Prompting user to change their pin
        if ( sUserInput == "2" ) {

            do {
                printf( "\nPlease enter you current pin: " );
                scanf( "%s" , &sUserInput );
                //FIX ME - ADD ERROR CHECKING ( FUNCTIONS? )

                if ( sUserInput != iUserPin ) {
                    printf( "\nIncorrect pin!" );
                }//End if( )

            } while ( sUserInput != iUserPin );//End do while( )


            while ( iNewUserPin != iUserPin ) {

                printf( "\nEnter new pin: " );
                scanf( "%s" , &iNewUserPin );

                printf( "Re-enter new pin: " );
                scanf( "%s" , &iUserPin );

                if ( iNewUserPin != iUserPin ) {

                    printf( "\nTry again!" );

                }//End if( )

            }//End while( ) 

        }//End if( )


        //This block of code will the display the amount of correct and incorrect inputs
        if ( sUserInput == "3" ) {

            printf( "\nYour pin was correctly entered %d times" , iCorrectInputs );
            printf( "\nYour pin was incorrectly entered %d times" , iIncorrectInputs );

        }//End if( )

        //This block of code will end the program if the user inputs 4
        //FIX ME: possibly use sUserInput for loop execution
        if ( sUserInput == "4" ) {

            iExitLoop = 0;

        }//End if( )

    }//End while( )

    return( 0 );

}//End main( )


//FIX ME: error checking for 4 character input

1 个答案:

答案 0 :(得分:3)

您发布的代码片段没有任何内在错误。确实*(userInput + i)完全等同于userInput[i]。您可能对数组大小存在一致性问题。

请注意,您使用userInputsUserInput_SIZE:名称不一致。您可能有2个不同的数组userInputsUserInput,这些数组可能有不同的大小?

修改

从完整源代码看,sUserInput是指向1个字符串"i"的指针。从偏移量1之外的此指针访问字节会调用未定义的行为。由于您正在处理字符串,而不是迭代直到sUserInut_SIZE,您应该只测试空终止符:

for (i = 0; userInput[i] != '\0'; i++) {
    for (j = 0; j < sNumArray_SIZE; j++) {
        if (userInput[i] == sNumArray[j]) {
            validInput++;
        }
    }
}

您可以先计算userInput的长度,然后使用长度作为循环的上限,并将值作为比较最终validInput的值。

其余代码还有更多问题:

  • 您的错误处理代码不会检查第二个条目的有效性。

  • 更重要的是:在scanf()中阅读sUserInput的{​​{1}}不正确。你应该使用:

    main()
  • 您的比较char sUserInput[20]; if (scanf("%19s", sUserInput) == 1) { /* handle sUserInput */ } else { /* premature end of file? */ } 不正确,您应该检查if (strncmp(sUserInput , "1" , 1) != 0)作为== 0的返回值,strncmp() strcmp()0 )字符串相等。

  • 您无法将字符串与==进行比较:if (sUserInput == "2")应更改为if (strcmp(sUserInput, "2") == 0)

  • 同样,if ( sUserInput == iUserPin )不正确。也可以使用strcmp()