이 글은 제가 처음 작성한 프로그램 중 하나입니다.이 포럼이 정확한지 잘 모르겠습니다. 이 질문을하지만 누군가가 내 문제를 해결할 수 있는지 궁금 해서요.간단한 계산기가 예상대로 작동하지 않습니다. - 초보자 용 도움말 (C)
계산기에서 questionnaire 옵션을 선택하면 첫 번째 질문이 올바르게 작동하고 프로그램이 끝납니다. 중첩 된 switch 문은 실행되지 않습니다.
int selectOperation,firstValue,secondValue,questInput,userScore = 0,nextQuestion = 0;
이가 무슨 의미가 있습니다
int main() {
/** Declaring variables (multiple variables can be declared together providing they are the same data type). **/
int selectOperation,firstValue,secondValue,questInput,userScore,nextQuestion;
printf("\n***Welcome back, sir!***\n");
do {
printf("\n***Please select your desired operation by inputting the associated value***\n\n");
printf(" [1]: Addition \n [2]: Subtraction \n [3]: Division \n [4]: Multiplication \n [5]: Questionnaire \n");
/** Asks the user to input an integer and stores the value in the selectOperation variable. **/
printf("\nEnter value: ");
scanf("%d", &selectOperation);
/** While the selectOperation variable is not equal to 1, 2, 3, 4 or 5 continue to run this loop. **/
/** The loop will continue to run until the user selects an operation. **/
} while((selectOperation != 1) && (selectOperation != 2) && (selectOperation != 3) && (selectOperation != 4) && (selectOperation != 5));
if ((selectOperation == 1) || (selectOperation == 2) || (selectOperation == 3) || (selectOperation == 4)) {
/** Input first value **/
printf("\nPlease enter the first value you would like to process:\n");
printf("\nEnter value: ");
scanf("%d", &firstValue);
/** Input second value **/
printf("\nPlease enter the second value you would like to process:\n");
printf("\nEnter value: ");
scanf("%d", &secondValue);
}
/** If the selectOperation variable is equal to the case value the code for that case will be ran, for
example if selectOperation's current value is 1 the text "You have selected addition" will print." **/
switch(selectOperation) {
case 1: /** Operation: Addition **/
printf("\nYou have selected addition! Your answer can be found below:\n\n");
/** Adds the stored values of the variables firstValue and secondValue and outputs the workings/result. **/
printf("%d + %d = %d\n",firstValue,secondValue,firstValue+secondValue);
break;
case 2: /** Operation: Subtraction **/
printf("\nYou have selected subtraction! Your answer can be found below:\n\n");
/** Subtracts the value of secondValue from firstValue and outputs the workings/result. **/
printf("%d - %d = %d\n",firstValue,secondValue,firstValue-secondValue);
break;
case 3: /** Operation: Division **/
printf("\nYou have selected division! Your answer can be found below:\n\n");
/** Divides firstValue by secondValue and outputs the workings/result. **/
printf("%d/%d = %d\n",firstValue,secondValue,firstValue/secondValue);
break;
case 4: /** Operation: Multiplication **/
printf("\nYou have selected multiplication! Your answer can be found below:\n\n");
/** Multiplies firstValue by secondValue and outputs the workings/result. **/
printf("%d * %d = %d\n",firstValue,secondValue,firstValue*secondValue);
break;
case 5: /** Questionnaire **/
printf("\nQuestion 1: What is 48 * 3?\n");
printf("\nEnter value: ");
scanf("%d", &questInput);
if (questInput == 144) {
printf("That's correct!\n");
userScore = userScore + 1;
nextQuestion = nextQuestion + 1;
}
else {
printf("That's incorrect!\n");
nextQuestion = nextQuestion + 1;
}
while(nextQuestion > 0 && nextQuestion < 5) {
switch(nextQuestion) {
case 1:
printf("\nQuestion 2: What is 36/6?\n");
printf("\nEnter value: ");
scanf("%d", &questInput);
if (questInput == 6) {
printf("That's correct!\n");
userScore = userScore + 1;
nextQuestion = nextQuestion + 1;
}
else {
printf("That's incorrect!\n");
nextQuestion = nextQuestion + 1;
}
break;
case 2:
printf("\nQuestion 3: What is 1526 + 49?\n");
printf("\nEnter value: ");
scanf("%d", &questInput);
if (questInput == 1575) {
printf("That's correct!\n");
userScore = userScore + 1;
nextQuestion = nextQuestion + 1;
}
else {
printf("That's incorrect!\n");
nextQuestion = nextQuestion + 1;
}
break;
case 3:
printf("\nQuestion 4: What is 5 * (5 + 15)?\n");
printf("\nEnter value: ");
scanf("%d", &questInput);
if (questInput == 100) {
printf("That's correct!\n");
userScore = userScore + 1;
nextQuestion = nextQuestion + 1;
}
else {
printf("That's incorrect!\n");
nextQuestion = nextQuestion + 1;
}
break;
case 4:
printf("\nQuestion 5: What is 30 + 21?\n");
printf("\nEnter value: ");
scanf("%d", &questInput);
if (questInput == 51) {
printf("That's correct!\n");
userScore = userScore + 1;
nextQuestion = nextQuestion + 1;
}
else {
printf("That's incorrect!\n");
nextQuestion = nextQuestion + 1;
}
break;
}
}
}
}
코드 서식을 다시 지정하십시오. – Hugal31
Welcome to Stack Overflow! 지금까지 연구/디버깅 노력을 보여주십시오. 먼저 [Ask] 페이지를 읽으십시오. –
초기 입력을 제외하고 * 루프가 * 없기 때문입니다. 이것은 a) 적절하게 들여 쓰기 된 코드와 b) 더 작은 함수로 나누어 진 코드로보기가 더 쉬울 것입니다. BTW는 inintialised 변수 인'userScore'와'nextQuestion'을 사용하고 있습니다. –