gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 06:59:29 24 4
gpt4 key购买 nike

<分区>

我试图通过这个 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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com