2017-07-23 31 views
0

這是我數學測驗計劃的一部分。我嘗試爲我的任務編寫數學測驗,並且想爲我的數學測驗做一個最終報告,如(int report())所示。當我運行該程序時,它表示title()(在modeadd()中)調用的參數太少。當我更改modeadd()中的title()與int title()相同時,我無法返回到菜單並直接轉到report.How我能解決這個問題嗎?我可以將函數的結果帶到另一個函數嗎?

void main() 
{ 
    int add, minus, times, divide; 

    menu(&add, &minus, &times, &divide); 
    report(add, minus, times, divide); 

    printf("\n"); 
    system("pause"); 
} 

int menu(add, minus, times, divide) 
{ 
    int choice; 

     printf("\t\t\t\t\t WELCOME TO THE MATH QUIZ.\n\a"); 
     printf("\t\t\t IF YOU ARE READY, ENTER '1' FOR YES. IF NOT, ENTER '2' FOR NO.\n"); 
     printf("\t\t\t\t PLEASE ENTER YOUR CHOICE : "); 
     scanf("%d", &choice); 
     rewind(stdin); 


     while (choice >= 3 || choice <= 0) 
      { 
       printf("INVALID NUMBER, PLEASE TRY AGAIN.\n"); 
       printf("PLEASE RE-ENTER AGAIN : "); 
       scanf("%d", &choice); 
       rewind(stdin); 
       printf("\n----------------------------------PAGE BREAK------------------------------------\n"); 
      } 

     switch (choice) 
     { 
     case 1: 
      title(add, minus, times, divide); 
      break; 

     default: 
      printf("\t\t\t THANK YOU. WELCOME YOU TO TRY WHEN YOU ARE READY."); 
     } 
     return choice; 
    } 

    int title(int*add, int*minus, int*times, int*divide) 
    { 
     int choice; 

    printf("PLEASE CHOOSE THE TITLE YOU WANT CHALLENGE.\n"); 
    printf("1. ADDITION\n2. SUBTRACTION\n3. MULTIPLICATION\n4. DIVISION\n"); 
    printf("PLEASE ENTER YOUR CHOICE : "); 
    scanf("%d", &choice); 
    rewind(stdin); 

    while (choice >= 5 || choice <= 0) 
    { 
     printf("INVALID NUMBER, PLEASE TRY AGAIN.\n"); 
     printf("PLEASE RE-ENTER AGAIN : "); 
     scanf("%d", &choice); 
     rewind(stdin); 
     printf("\n----------------------------------PAGE BREAK------------------------------------\n"); 
} 

     switch (choice) 
     { 
     case 1: 
      printf("YOU CHOICE IS NUMBER 1, ADDITION\n"); 
      printf("\n----------------------------------PAGE BREAK------------------------------------\n\n"); 
      (*add)++; 
      modeadd(); 
      break; 

     case 2: 
      printf("YOU CHOICE IS NUMBER 2, SUBTRACTION\n"); 
      printf("\n----------------------------------PAGE BREAK------------------------------------\n\n"); 
      (*minus)++; 
      modeminus(); 
      break; 

     case 3: 
      printf("YOU CHOICE IS NUMBER 3, MULTIPLICATION\n"); 
      printf("\n----------------------------------PAGE BREAK------------------------------------\n\n"); 
      (*times)++; 
      modetimes(); 
      break; 

     default: 
      printf("YOU CHOICE IS NUMBER 4, DIVISION\n"); 
      printf("\n----------------------------------PAGE BREAK------------------------------------\n\n"); 
      (*divide)++; 
      modedivide(); 
      break; 
     } 

    return choice; 
} 

int modeadd() 
{ 
    int choice; 

    printf("PLEASE CHOOSE THE DIFFICULT YOU WANT CHALLENGE FOR ADDITION.\n"); 
    printf("1. BEGINNER\n2. INTERMEDIATE\n3. ADVANCED\n4. QUIT TO MENU\n"); 
    printf("PLEASE ENTER YOUR CHOICE : "); 
    scanf("%d", &choice); 
    rewind(stdin); 

    while (choice <= 0 || choice >= 5) 
    { 
     printf("INVALID NUMBER, PLEASE TRY AGAIN.\n"); 
     printf("PLEASE RE-ENTER AGAIN : "); 
     scanf("%d", &choice); 
     rewind(stdin); 
     printf("\n----------------------------------PAGE BREAK------------------------------------\n"); 
    } 

    if (choice == 1) 
    { 
     printf("YOUR CHOICE IS NUMBER 1, BEGINNER.\n"); 
     printf("\n----------------------------------PAGE BREAK------------------------------------\n\n"); 
     ABeg(); 
    } 

    else if (choice == 2) 
    { 
     printf("YOUR CHOICE IS NUMBER 2, INTERMEDIATE.\n"); 
     printf("\n----------------------------------PAGE BREAK------------------------------------\n\n"); 
     AInter(); 
    } 

    else if (choice == 3) 
    { 
     printf("YOUR CHOICE IS NUMBER 3, ADVANCED.\n"); 
     printf("\n----------------------------------PAGE BREAK------------------------------------\n\n"); 
     AAdv(); 
    } 

    else 
    { 
     printf("\n----------------------------------PAGE BREAK------------------------------------\n\n"); 
     printf("YOU HAVE CHOOSE TO BACK TO MENU. "); 
     title(); 
    } 

    return 0; 
} 

//Addition beginner question 
int ABeg() 
{ 
    int response, i, corAns, wrongAns, num2, num1; 
    double answer, correct; 

    printf("PLEASE ENTER THE NUMBER OF QUESTION YOU WANT TO TRY : "); 
    scanf("%d", &response); 
    rewind(stdin); 

    while (response < 0) 
    { 
     printf("PLEASE ENTER A VALID NUMBER : "); 
     scanf("%d", &response); 
     rewind(stdin); 
    } 

    printf("PLEASE ENTER YOUR ANSWER.\n"); 

    if (response == 0) 
    { 
     printf("\nTHANKS FOR PLAYING!!!\n"); 
     return 0; 
    } 

    else 
    { 
     corAns = 0; 
     wrongAns = 0; 
     for (i = 1; i <= response; i++) 
     { 
      srand(time(NULL)); 
      num1 = rand() % 21; 
      num2 = rand() % 21; 
      correct = num1 + num2; 

      printf("\n%d. %d + %d = ", i, num1, num2); 
      scanf("%lf", &answer); 
      rewind(stdin); 

      if (answer == correct) 
      { 
       printf("I KNOW YOU WILL ANSWERED IT CORRECTLY.\n"); 
       corAns++; 
      } 

      else 
      { 
       printf("SORRY BUT GOOD TRY. THE CORRECT ANSWER IS %.0f.\n", correct); 
       wrongAns++; 
      } 
     } 

     printf("\nYOUR RESULT\n"); 
     printf("===========\n"); 
     printf("CORRECT ANSWER = %d QUESTION\n", corAns); 
     printf("INCORRECT ANSWER = %d QUESTION\n", wrongAns); 

     if (corAns == response) 
     { 
      printf("YOU ARE SO HIGHLY INTELLIGENT :). YOU GET THEM ALL RIGHT!!!\n"); 
      nextadd(); 
     } 
     else if (corAns == wrongAns) 
     { 
      printf("GOOD TRY BUT YOU ARE NEARLY FAIL. KEEP WORKING HARDER AND YOU WILL DO BETTER NEXT TIME.\n"); 
      failadd(); 
     } 
     else if (corAns > wrongAns) 
     { 
      printf("YOUR CORRECT ANSWER IS ABOVE HALF. GREAT JOB! XD. PLAY THE GAME AGAIN SOON.\n"); 
      nextadd(); 
     } 
     else 
     { 
      printf("YOU HAVE FAIL IN THIS MATH QUIZ :(. PLEASE DO THE REVISION.\n"); 
      failadd(); 
     } 
    } 
    return 0; 
}  

//Asking after addition question 
int nextadd() 
{ 
    int choice; 

    printf("\n----------------------------------PAGE BREAK------------------------------------\n\n"); 
    printf("DID YOU WANT TO CHALLENGE HARDER QUESTION? OR DIFFERENT ARITHMETIC? OR ELSE.\n"); 
    printf("1. CHALLENGE SAME OR DIFFERENT LEVEL WITH SAME ARITHMETIC.\n"); 
    printf("2. DIFFERENT ARITHMETIC.\n"); 
    printf("3. NO, I WANT TO QUIT THE QUIZ.\n"); 
    printf("PLEASE ENTER YOUR ANSWER : "); 
    scanf("%d", &choice); 
    rewind(stdin); 

    while (choice >= 4 || choice <= 0) 
    { 
     printf("INVALID NUMBER, PLEASE TRY AGAIN.\n"); 
     printf("PLEASE RE-ENTER AGAIN : "); 
     scanf("%d", &choice); 
     rewind(stdin); 
     printf("\n----------------------------------PAGE BREAK------------------------------------\n"); 
} 

    switch (choice) 
    { 
    case 1: 
     printf("\n----------------------------------PAGE BREAK------------------------------------\n"); 
     modeadd(); 
     break; 
    case 2: 
     printf("\n----------------------------------PAGE BREAK------------------------------------\n"); 
     title(); 
     break; 
    default: 
     printf("THANK YOU FOR YOUR PLAYING."); 
    } 

    return 0; 
} 

int report(int add, int minus, int times, int divide) 
{ 
    printf("\n============\n"); 
    printf("FINAL REPORT\n"); 
    printf("============\n"); 
    printf("YOU HAVE PLAY ADDITION FOR %d TIMES\n", add); 
    printf("YOU HAVE PLAY SUBTRACTION FOR %d TIMES\n", minus); 
    printf("YOU HAVE PLAY MULTIPLICATION FOR %d TIMES\n", times); 
    printf("YOU HAVE PLAY DIVISION FOR %d TIMES\n", divide); 

    return 0; 
} 

回答

0

首先歡迎來到StackOverflow。

當問一個關於錯誤的問題時,如果你說錯誤是什麼,它會很有幫助,而不是僅僅聲明有錯誤。然而,就你而言,我認爲你的「錯誤」是,無論你玩多少次,你的報告都會顯示你在每個類別中都玩過0次。

這是因爲範圍。你在title()和report()中聲明變量的加,減,乘和除。因此,這些變量僅侷限於這些例程,並在控制從它們傳遞時實際上丟失。您的報告只顯示0,因爲這是這些變量包含的報告()中唯一的值。

要解決這個問題,你需要做兩件事之一;或者將你的代碼包含在一個類中,並將變量聲明爲該類的字段並從title()和report()中移除聲明,或者將它們聲明爲main()中的變量,並通過引用menu()轉向title())和report()。您需要通過引用傳遞,否則被調用的例程只會獲取該值的副本,這意味着從被調用例程退出時,對該變量的任何更改都將丟失。

編輯

只是有一點空閒時間,所以我可以修改你的代碼。這是我的:

#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 

void main() 
{ 
    int add, minus, times, divide; 
    add = minus = times = divide = 0; 
    int choice = 1; 
    while (choice == 1) 
    { 
     choice = menu(&add, &minus, &times, &divide); 
    } 
    report(add, minus, times, divide); 

    printf("\n"); 
    system("pause"); 
} 

int menu(int * add, int * minus, int * times, int * divide) 
{ 
    int choice; 

    printf("\t\t\t\t\t WELCOME TO THE MATH QUIZ.\n\a"); 
    printf("\t\t\t IF YOU ARE READY, ENTER '1' FOR YES. IF NOT, ENTER '2' FOR NO.\n"); 
    printf("\t\t\t\t PLEASE ENTER YOUR CHOICE : "); 
    scanf_s("%d", &choice); 
    rewind(stdin); 


    while (choice >= 3 || choice <= 0) 
    { 
     printf("INVALID NUMBER, PLEASE TRY AGAIN.\n"); 
     printf("PLEASE RE-ENTER AGAIN : "); 
     scanf_s("%d", &choice); 
     rewind(stdin); 
     printf("\n----------------------------------PAGE BREAK------------------------------------\n"); 
    } 

    switch (choice) 
    { 
    case 1: 
     title(add, minus, times, divide); 
     break; 

    default: 
     printf("\t\t\t THANK YOU. WELCOME YOU TO TRY WHEN YOU ARE READY."); 
    } 
    return choice; 
} 

void failadd(int * add, int * minus, int * times, int * divide) 
{ 
    return; 
} 

int modeadd(int * add, int * minus, int * times, int * divide) 
{ 
    int choice; 

    printf("PLEASE CHOOSE THE DIFFICULT YOU WANT CHALLENGE FOR ADDITION.\n"); 
    printf("1. BEGINNER\n2. INTERMEDIATE\n3. ADVANCED\n4. QUIT TO MENU\n"); 
    printf("PLEASE ENTER YOUR CHOICE : "); 
    scanf_s("%d", &choice); 
    rewind(stdin); 

    while (choice <= 0 || choice >= 5) 
    { 
     printf("INVALID NUMBER, PLEASE TRY AGAIN.\n"); 
     printf("PLEASE RE-ENTER AGAIN : "); 
     scanf_s("%d", &choice); 
     rewind(stdin); 
     printf("\n----------------------------------PAGE BREAK------------------------------------\n"); 
    } 

    if (choice == 1) 
    { 
     printf("YOUR CHOICE IS NUMBER 1, BEGINNER.\n"); 
     printf("\n----------------------------------PAGE BREAK------------------------------------\n\n"); 
     ABeg(add, minus, times, divide); 
    } 

    else if (choice == 2) 
    { 
     printf("YOUR CHOICE IS NUMBER 2, INTERMEDIATE.\n"); 
     printf("\n----------------------------------PAGE BREAK------------------------------------\n\n"); 
     AInter(add, minus, times, divide); 
    } 

    else if (choice == 3) 
    { 
     printf("YOUR CHOICE IS NUMBER 3, ADVANCED.\n"); 
     printf("\n----------------------------------PAGE BREAK------------------------------------\n\n"); 
     AAdv(add, minus, times, divide); 
    } 

    else 
    { 
     printf("\n----------------------------------PAGE BREAK------------------------------------\n\n"); 
     printf("YOU HAVE CHOOSE TO BACK TO MENU. "); 
     title(add, minus, times, divide); 
    } 

    return 0; 
} 

//Addition beginner question 
int ABeg(int * add, int * minus, int * times, int * divide) 
{ 
    int response, i, corAns, wrongAns, num2, num1; 
    double answer, correct; 

    printf("PLEASE ENTER THE NUMBER OF QUESTION YOU WANT TO TRY : "); 
    scanf_s("%d", &response); 
    rewind(stdin); 

    while (response < 0) 
    { 
     printf("PLEASE ENTER A VALID NUMBER : "); 
     scanf_s("%d", &response); 
     rewind(stdin); 
    } 

    printf("PLEASE ENTER YOUR ANSWER.\n"); 

    if (response == 0) 
    { 
     printf("\nTHANKS FOR PLAYING!!!\n"); 
     return 0; 
    } 

    else 
    { 
     (*add)++; 
     corAns = 0; 
     wrongAns = 0; 
     for (i = 1; i <= response; i++) 
     { 
      srand(time(NULL)); 
      num1 = rand() % 21; 
      num2 = rand() % 21; 
      correct = num1 + num2; 

      printf("\n%d. %d + %d = ", i, num1, num2); 
      scanf_s("%lf", &answer); 
      rewind(stdin); 

      if (answer == correct) 
      { 
       printf("I KNOW YOU WILL ANSWERED IT CORRECTLY.\n"); 
       corAns++; 
      } 

      else 
      { 
       printf("SORRY BUT GOOD TRY. THE CORRECT ANSWER IS %.0f.\n", correct); 
       wrongAns++; 
      } 
     } 

     printf("\nYOUR RESULT\n"); 
     printf("===========\n"); 
     printf("CORRECT ANSWER = %d QUESTION\n", corAns); 
     printf("INCORRECT ANSWER = %d QUESTION\n", wrongAns); 

     if (corAns == response) 
     { 
      printf("YOU ARE SO HIGHLY INTELLIGENT :). YOU GET THEM ALL RIGHT!!!\n"); 
      nextadd(add, minus, times, divide); 
     } 
     else if (corAns == wrongAns) 
     { 
      printf("GOOD TRY BUT YOU ARE NEARLY FAIL. KEEP WORKING HARDER AND YOU WILL DO BETTER NEXT TIME.\n"); 
      failadd(add, minus, times, divide); 
     } 
     else if (corAns > wrongAns) 
     { 
      printf("YOUR CORRECT ANSWER IS ABOVE HALF. GREAT JOB! XD. PLAY THE GAME AGAIN SOON.\n"); 
      nextadd(add, minus, times, divide); 
     } 
     else 
     { 
      printf("YOU HAVE FAIL IN THIS MATH QUIZ :(. PLEASE DO THE REVISION.\n"); 
      failadd(add, minus, times, divide); 
     } 
    } 
    return 0; 
} 

int AInter(int * add, int * minus, int * times, int * divide) 
{ 
    return 0; 
} 

int AAdv(int * add, int * minus, int * times, int * divide) 
{ 
    return 0; 
} 
//Asking after addition question 

int nextadd(int * add, int * minus, int * times, int * divide) 
{ 
    int choice; 

    printf("\n----------------------------------PAGE BREAK------------------------------------\n\n"); 
    printf("DID YOU WANT TO CHALLENGE HARDER QUESTION? OR DIFFERENT ARITHMETIC? OR ELSE.\n"); 
    printf("1. CHALLENGE SAME OR DIFFERENT LEVEL WITH SAME ARITHMETIC.\n"); 
    printf("2. DIFFERENT ARITHMETIC.\n"); 
    printf("3. NO, I WANT TO QUIT THE QUIZ.\n"); 
    printf("PLEASE ENTER YOUR ANSWER : "); 
    scanf_s("%d", &choice); 
    rewind(stdin); 

    while (choice >= 4 || choice <= 0) 
    { 
     printf("INVALID NUMBER, PLEASE TRY AGAIN.\n"); 
     printf("PLEASE RE-ENTER AGAIN : "); 
     scanf_s("%d", &choice); 
     rewind(stdin); 
     printf("\n----------------------------------PAGE BREAK------------------------------------\n"); 
    } 

    switch (choice) 
    { 
    case 1: 
     printf("\n----------------------------------PAGE BREAK------------------------------------\n"); 
     modeadd(add, minus, times, divide); 
     break; 
    case 2: 
     printf("\n----------------------------------PAGE BREAK------------------------------------\n"); 
     title(add, minus, times, divide); 
     break; 
    default: 
     printf("THANK YOU FOR YOUR PLAYING."); 
    } 

    return 0; 
} 

void modeminus(int * add, int * minus, int * times, int * divide) 
{ 
    return; 
} 

void modetimes(int * add, int * minus, int * times, int * divide) 
{ 
    return; 
} 

void modedivide(int * add, int * minus, int * times, int * divide) 
{ 
    return; 
} 

int title(int * add, int * minus, int * times, int * divide) 
{ 
    int choice; 

    printf("PLEASE CHOOSE THE TITLE YOU WANT CHALLENGE.\n"); 
    printf("1. ADDITION\n2. SUBTRACTION\n3. MULTIPLICATION\n4. DIVISION\n"); 
    printf("PLEASE ENTER YOUR CHOICE : "); 
    scanf_s("%d", &choice); 
    rewind(stdin); 

    while (choice >= 5 || choice <= 0) 
    { 
     printf("INVALID NUMBER, PLEASE TRY AGAIN.\n"); 
     printf("PLEASE RE-ENTER AGAIN : "); 
     scanf_s("%d", &choice); 
     rewind(stdin); 
     printf("\n----------------------------------PAGE BREAK------------------------------------\n"); 
    } 

    switch (choice) 
    { 
    case 1: 
     printf("YOU CHOICE IS NUMBER 1, ADDITION\n"); 
     printf("\n----------------------------------PAGE BREAK------------------------------------\n\n"); 
     modeadd(add, minus, times, divide); 
     break; 

    case 2: 
     printf("YOU CHOICE IS NUMBER 2, SUBTRACTION\n"); 
     printf("\n----------------------------------PAGE BREAK------------------------------------\n\n"); 
     modeminus(add, minus, times, divide); 
     break; 

    case 3: 
     printf("YOU CHOICE IS NUMBER 3, MULTIPLICATION\n"); 
     printf("\n----------------------------------PAGE BREAK------------------------------------\n\n"); 
     modetimes(add, minus, times, divide); 
     break; 

    default: 
     printf("YOU CHOICE IS NUMBER 4, DIVISION\n"); 
     printf("\n----------------------------------PAGE BREAK------------------------------------\n\n"); 
     modedivide(add, minus, times, divide); 
     break; 
    } 

    return choice; 
} 

int report(int add, int minus, int times, int divide) 
{ 
    printf("\n============\n"); 
    printf("FINAL REPORT\n"); 
    printf("============\n"); 
    printf("YOU HAVE PLAY ADDITION FOR %d TIMES.\n", add); 
    printf("YOU HAVE PLAY SUBTRACTION FOR %d TIMES.\n", minus); 
    printf("YOU HAVE PLAY MULTIPLICATION FOR %d TIMES.\n", times); 
    printf("YOU HAVE PLAY DIVISION FOR %d TIMES.\n", divide); 

    return 0; 
} 

有幾件事要注意。在c中,不存在「傳遞引用」這種東西,而是通過將變量的地址改爲&來代替變量的地址(該地址本身是通過值傳遞的)。取消引用地址(帶*)意味着您可以操縱位於該地址的值。

我在while循環中包含了對menu()的調用,否則程序將在第一次計算後結束,這不是你的意圖。

在菜單()中,請注意它將變量傳遞給title()而不是&。這是因爲它收到的變量已經是指向一個地址的指針,所以你可以按原樣傳遞它們。

title()通過對它們進行解引用來增加計數器,例如, (*添加)++;如果你不取消引用他們,你就增加了地址,這將不會產生你想要的效果!

我包含modeadd()等空例程,這些需要完成。

HTH

修訂

我做了另一種變化,表現出別的東西。我更改了參數report()來輸入ints而不是地址。這是因爲report()只需要知道這些值,它不會增加它們。現在請注意main()中調用menu()和report()的區別。

最新變化

加,減,現在時間和鴻溝傳遞無處不在。此外,我將增加計數器的增量移至ABeg()。它在這裏更有意義,因爲如果用戶實際選擇回答某些問題,您可以確保它只會增加。

+0

感謝您解決我的問題的一部分,但我的主要「錯誤」是意味着去我試圖調試我的程序,它出來的消息說,有一個建立錯誤。然後它說,在report()中,使用未初始化的局部變量'add','minus','times'和'divide'。所以,我知道可能會有一些遺漏或錯誤的代碼。但是,我找不到它。我也嘗試在main(),title(),menu()中添加,減去次數和除法作爲變量,但我也遇到了同樣的問題。所以我想知道我的錯誤在哪裏,我該如何糾正它。 – NewInProgramming

+0

好的。爲了讓你編譯,你需要在report()中初始化變量add,minus等。爲此,只需爲它們賦值0.(add = 0;)編譯器會抱怨,因爲您試圖在printf中使用這些變量。然而,如果你遵循我的其他建議,這些變量是不需要的。 HTH –

+0

好的。但是當我改變int標題的時候,我也需要改變modeadd()中的title()。但是,當我選擇返回到菜單(在modeadd())中時,這使我無法回到菜單。此外,如果我在mainad中添加while,在nextadd()中,當我選擇退出程序時,它會再次跳轉到菜單。我需要做些什麼來解決它。 – NewInProgramming

0

我終於忍不住了,找不到你的數學函數定義的任何代碼中的

modeadd(); 
modeminus(); 
modetimes(); 
modedivide(); 

定義代碼中的上述功能和編譯程序,

例如

void modeadd() 
{ 
int a,b; 
printf("Please enter the value for A="); 
scanf("%d",&a); 
printf("Please enter the value for B="); 
scanf("%d",&b); 
printf("A+B=%d",a+b); 
} 
0

作爲一種替代方法,我在這裏給出一個例子,說明如何使用C++和類來完成相同的操作。除了標準輸入/輸出的不同語法之外,關鍵的區別在於,加,減,時和除都是該類的私有成員。這意味着所有例程都可以自動訪問它們。這反過來意味着你不必一直傳遞它們,而且你也不需要關心傳遞地址或值。

#include "stdafx.h" 
#include <cstdio> 
#include <iostream> 
#include <ctime> 
#include <string> 

using namespace std; 

class MathQuiz 
{ 
public: 
    MathQuiz() 
    { 
     add = minus = times = divide = 0; 
    } 
    int menu() 
    { 
     int choice; 

     cout << "\t\t\t\t\t WELCOME TO THE MATH QUIZ.\n\a"; 
     cout << "\t\t\t IF YOU ARE READY, ENTER '1' FOR YES. IF NOT, ENTER '2' FOR NO.\n"; 
     cout << "\t\t\t\t PLEASE ENTER YOUR CHOICE : "; 
     cin >> choice; 
     system("cls"); 

     while (choice >= 3 || choice <= 0) 
     { 
      cout << "INVALID NUMBER, PLEASE TRY AGAIN.\n"; 
      cout << "PLEASE RE-ENTER AGAIN : "; 
      cin >> choice; 
      system("cls"); 
      cout << "\n----------------------------------PAGE BREAK------------------------------------\n"; 
     } 

     switch (choice) 
     { 
     case 1: 
      title(); 
      break; 

     default: 
      cout << "\t\t\t THANK YOU. WELCOME YOU TO TRY WHEN YOU ARE READY."; 
     } 
     return choice; 
    } 

    int report() 
    { 
     cout << "\n============\n"; 
     cout << "FINAL REPORT\n"; 
     cout << "============\n"; 
     cout << "YOU HAVE PLAY ADDITION FOR " + to_string(add) + " TIMES.\n"; 
     cout << "YOU HAVE PLAY SUBTRACTION FOR " + to_string(minus) + " TIMES.\n"; 
     cout << "YOU HAVE PLAY MULTIPLICATION FOR " + to_string(times) + " TIMES.\n"; 
     cout << "YOU HAVE PLAY DIVISION FOR " + to_string(divide) + " TIMES.\n"; 

     return 0; 
    } 


private: 
    int add, minus, times, divide; 

    void failadd() 
    { 
     return; 
    } 

    int modeadd() 
    { 
     int choice; 

     cout << "PLEASE CHOOSE THE DIFFICULT YOU WANT CHALLENGE FOR ADDITION.\n"; 
     cout << "1. BEGINNER\n2. INTERMEDIATE\n3. ADVANCED\n4. QUIT TO MENU\n"; 
     cout << "PLEASE ENTER YOUR CHOICE : "; 
     cin >> choice; 
     system("cls"); 

     while (choice <= 0 || choice >= 5) 
     { 
      cout << "INVALID NUMBER, PLEASE TRY AGAIN.\n"; 
      cout << "PLEASE RE-ENTER AGAIN : "; 
      cin >> choice; 
      system("cls"); 
      cout << "\n----------------------------------PAGE BREAK------------------------------------\n"; 
     } 

     if (choice == 1) 
     { 
      cout << "YOUR CHOICE IS NUMBER 1, BEGINNER.\n"; 
      cout << "\n----------------------------------PAGE BREAK------------------------------------\n\n"; 
      ABeg(); 
     } 

     else if (choice == 2) 
     { 
      cout << "YOUR CHOICE IS NUMBER 2, INTERMEDIATE.\n"; 
      cout << "\n----------------------------------PAGE BREAK------------------------------------\n\n"; 
      AInter(); 
     } 

     else if (choice == 3) 
     { 
      cout << "YOUR CHOICE IS NUMBER 3, ADVANCED.\n"; 
      cout << "\n----------------------------------PAGE BREAK------------------------------------\n\n"; 
      AAdv(); 
     } 

     else 
     { 
      cout << "\n----------------------------------PAGE BREAK------------------------------------\n\n"; 
      cout << "YOU HAVE CHOOSE TO BACK TO MENU. "; 
      title(); 
     } 

     return 0; 
    } 

    //Addition beginner question 
    int ABeg() 
    { 
     int response, i, corAns, wrongAns, num2, num1; 
     double answer, correct; 

     cout << "PLEASE ENTER THE NUMBER OF QUESTION YOU WANT TO TRY : "; 
     cin >> response; 
     system("cls"); 

     while (response < 0) 
     { 
      cout << "PLEASE ENTER A VALID NUMBER : "; 
      cin >> response; 
      system("cls"); 
     } 

     cout << "PLEASE ENTER YOUR ANSWER.\n"; 

     if (response == 0) 
     { 
      cout << "\nTHANKS FOR PLAYING!!!\n"; 
      return 0; 
     } 

     else 
     { 
      add++; 
      corAns = 0; 
      wrongAns = 0; 
      for (i = 1; i <= response; i++) 
      { 
       srand(time(0)); 
       num1 = rand() % 21; 
       num2 = rand() % 21; 
       correct = num1 + num2; 

       cout << to_string(i) + ". " + to_string(num1) + " \+ " + to_string(num2) + " = "; 
       cin >> answer; 
       system("cls"); 

       if (answer == correct) 
       { 
        cout << "I KNOW YOU WILL ANSWERED IT CORRECTLY.\n"; 
        corAns++; 
       } 

       else 
       { 
        cout << "SORRY BUT GOOD TRY. THE CORRECT ANSWER IS " + to_string(correct) + "\n"; 
        wrongAns++; 
       } 
      } 

      cout << "\nYOUR RESULT\n"; 
      cout << "===========\n"; 
      cout << "CORRECT ANSWER = " + to_string(corAns) + " QUESTION\n"; 
      cout << "INCORRECT ANSWER = " + to_string(wrongAns) + " QUESTION\n"; 

      if (corAns == response) 
      { 
       cout << "YOU ARE SO HIGHLY INTELLIGENT :). YOU GET THEM ALL RIGHT!!!\n"; 
       nextadd(); 
      } 
      else if (corAns == wrongAns) 
      { 
       cout << "GOOD TRY BUT YOU ARE NEARLY FAIL. KEEP WORKING HARDER AND YOU WILL DO BETTER NEXT TIME.\n"; 
       failadd(); 
      } 
      else if (corAns > wrongAns) 
      { 
       cout << "YOUR CORRECT ANSWER IS ABOVE HALF. GREAT JOB! XD. PLAY THE GAME AGAIN SOON.\n"; 
       nextadd(); 
      } 
      else 
      { 
       cout << "YOU HAVE FAIL IN THIS MATH QUIZ :(. PLEASE DO THE REVISION.\n"; 
       failadd(); 
      } 
     } 
     return 0; 
    } 

    int AInter() 
    { 
     return 0; 
    } 

    int AAdv() 
    { 
     return 0; 
    } 
    //Asking after addition question 

    int nextadd() 
    { 
     int choice; 

     cout << "\n----------------------------------PAGE BREAK------------------------------------\n\n"; 
     cout << "DID YOU WANT TO CHALLENGE HARDER QUESTION? OR DIFFERENT ARITHMETIC? OR ELSE.\n"; 
     cout << "1. CHALLENGE SAME OR DIFFERENT LEVEL WITH SAME ARITHMETIC.\n"; 
     cout << "2. DIFFERENT ARITHMETIC.\n"; 
     cout << "3. NO, I WANT TO QUIT THE QUIZ.\n"; 
     cout << "PLEASE ENTER YOUR ANSWER : "; 
     cin >> choice; 
     system("cls"); 

     while (choice >= 4 || choice <= 0) 
     { 
      cout << "INVALID NUMBER, PLEASE TRY AGAIN.\n"; 
      cout << "PLEASE RE-ENTER AGAIN : "; 
      cin >> choice; 
      system("cls"); 
      cout << "\n----------------------------------PAGE BREAK------------------------------------\n"; 
     } 

     switch (choice) 
     { 
     case 1: 
      cout << "\n----------------------------------PAGE BREAK------------------------------------\n"; 
      modeadd(); 
      break; 
     case 2: 
      cout << "\n----------------------------------PAGE BREAK------------------------------------\n"; 
      title(); 
      break; 
     default: 
      cout << "THANK YOU FOR YOUR PLAYING."; 
     } 

     return 0; 
    } 

    void modeminus() 
    { 
     return; 
    } 

    void modetimes() 
    { 
     return; 
    } 

    void modedivide() 
    { 
     return; 
    } 

    int title() 
    { 
     int choice; 

     cout << "PLEASE CHOOSE THE TITLE YOU WANT CHALLENGE.\n"; 
     cout << "1. ADDITION\n2. SUBTRACTION\n3. MULTIPLICATION\n4. DIVISION\n"; 
     cout << "PLEASE ENTER YOUR CHOICE : "; 
     cin >> choice; 
     system("cls"); 

     while (choice >= 5 || choice <= 0) 
     { 
      cout << "INVALID NUMBER, PLEASE TRY AGAIN.\n"; 
      cout << "PLEASE RE-ENTER AGAIN : "; 
      cin >> choice; 
      system("cls"); 
      cout << "\n----------------------------------PAGE BREAK------------------------------------\n"; 
     } 

     switch (choice) 
     { 
     case 1: 
      cout << "YOU CHOICE IS NUMBER 1, ADDITION\n"; 
      cout << "\n----------------------------------PAGE BREAK------------------------------------\n\n"; 
      modeadd(); 
      break; 

     case 2: 
      cout << "YOU CHOICE IS NUMBER 2, SUBTRACTION\n"; 
      cout << "\n----------------------------------PAGE BREAK------------------------------------\n\n"; 
      modeminus(); 
      break; 

     case 3: 
      cout << "YOU CHOICE IS NUMBER 3, MULTIPLICATION\n"; 
      cout << "\n----------------------------------PAGE BREAK------------------------------------\n\n"; 
      modetimes(); 
      break; 

     default: 
      cout << "YOU CHOICE IS NUMBER 4, DIVISION\n"; 
      cout << "\n----------------------------------PAGE BREAK------------------------------------\n\n"; 
      modedivide(); 
      break; 
     } 

     return choice; 
    } 


}; 

int main() 
{ 
    MathQuiz quiz; 
    int choice = 1; 
    while (choice == 1) 
    { 
     choice = quiz.menu(); 
    } 
    quiz.report(); 

    cout << "\n"; 
    cin.ignore(); 
    cin.get(); 
} 
相關問題