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

标签 c pointers if-statement for-loop

<分区>

我试图通过这个 for 循环传递一个指针,但它不起作用。这只是我原始代码的一个 block 。我不想用 200 行代码让大家负担过重!如果您需要全部,请询问。

好的!所以我使用了 3 个 printf 来查看我的代码在崩溃之前能走多远。它到达 printf( "2");在它崩溃之前。所以我假设它与 if ( *(userInput + i ) == sNumArray[j] ) 有关 我不确定我做错了什么。据我所知,指针不使用 pointer[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

最佳答案

您发布的代码片段本身没有任何问题。实际上 *(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 进行比较的值。

其余代码还有更多问题:

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

  • 更重要的是:main() 中读取sUserInputscanf() 是不正确的。你应该使用:

    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()

关于c - 通过 for 循环和 if 语句传递指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40329548/

相关文章:

c - 在缓冲区内取消引用

带有 Promise 和 if 语句的 Angular Auth Guard

c - C 中的按位运算

python - 变量如何在 Python 中工作?

c++ - 指向对象指针数组的指针的深拷贝

无法访问数组指针 C 的索引

java - 如何将用户字符串输入与整个数组中的字符串进行比较?

c - 我怎样才能只计算列表中的某些项目?

c - 使用函数查找字母和单词

c - 串口监视器中的Arduino换行符(帮助)