このforループを介してポインタを渡そうとしていますが、動作しません。これは私の元のコードからちょうどブロックです。私は200行のコードで皆さんを過負荷にしたくありませんでした!それがすべて必要な場合はお尋ねください。forループとifステートメントを介してポインタを渡す
だから私は3つのprintfを使って、コードがクラッシュするまでにどれくらいの距離を得るのか見ていました。それはprintf( "2")に到達します。それがクラッシュする前に。だから私はそれが何であると仮定しているのですif (*(userInput + i) == sNumArray[j])
私は何が間違っているのか分かりません。私の知る限り、ポインターはポインタ[i]を使用して、使用する各要素を循環しません。*(pointer + i)
?
私はCでプログラミングを始めたのは4週間前です。これを完全に説明していないとすみませます。私はまだ専門用語を学んでいます。ここなど
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関数でエラーチェックをすることができますが、私は学習目的のためにそれ自身の関数を作成しようとしていました。私はこれに多くの時間を費やしています。そして、何かを試してみましょうelse。
#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
出力バッファをフラッシュしないため、出力に欠陥があります。つまり、バッファリングされた出力をたくさん持つことができ、クラッシュはどこにあるかとは無関係のどこかで発生します。 'printf'文字列を改行(' "\ n" ')で終わらせ、バッファをフラッシュします。 –
また、デバッガの使い方も学ぶ必要があります。デバッガでプログラムを実行すると、クラッシュが発生したときにプログラムが停止し、その場所が表示されます。クラッシュがコード内にない場合は、コードが完成するまで呼び出すことができます。その後、変数を調べてその値を見ることができます。 –
このようなアクセスについての混乱については、 'pointer [i]'。これは、配列とポインタの両方に適しています。 '[]'添字演算子は、ポインタと配列の両方に使用できます。 – ameyCU