2012-02-10 32 views
-1

我已經找遍了所有的論壇來嘗試和理解這個問題。我無法完全理解問題的原因以及爲什麼找不到解決方案是因爲我對C++相當陌生,而且我不明白錯誤消息。完整初學者:空指針的問題?

這是我在C++中的代碼,它可以從排列或組合公式中找到可能的數目。每次我嘗試編譯和運行,我得到消息說:

First-chance exception at 0x6a8613af (msvcr100d.dll) in Combinations_Permutations.exe: 0xC0000005: Access violation reading location 0x00000005. Unhandled exception at 0x6a8613af (msvcr100d.dll) in Combinations_Permutations.exe: 0xC0000005: Access violation reading location 0x00000005.

我已經在許多其他論壇的「訪問衝突讀取地址0x00 ......」肯定可以表明空指針教訓。但我看不到我遇到這樣一個空問題。也許我的變量正在全球訪問,他們不是初始化? 這是我的代碼,我一直在這裏一段時間...就像我說我是相當新的。所以請告訴我我的錯誤。謝謝。

我的代碼:

#include <iostream> 
#include "conio.h"; 
using namespace std; 

int run_combination(int n, int r); 
int run_permutation(int n, int r); 
int solve_factorial(int f); 

int f_value = 1; //factorial value used recursively 
int n_input, r_input; 
char choice; 
char order; 
void main(){ 
      //if user types choice as 'q', while loop ends 
    while(choice != 'q'){ 
     printf("How many values? (1-9) \n"); 
     printf("User: "); 
     cin >> n_input;//user input for variable n 
     printf("n_input: %i", n_input); 

     printf("\nHow many will be chosen at a time out of those values? (1-9)\n"); 
     printf("User: "); 
     cin >> r_input; //user input for variable r 

     printf("\nDoes order matter? (y/n)\n"); 
     printf("User: "); 
     cin >> order; //'y' if order is taken into consideration(permutation) 
          //'n' if order it NOT taken into consideration(combination) 

     int solution = 0; //temporary variable that represents the solution after running 
          //n and r through the permutation or combination formula 

     //if user input values for n and r are in between 1 and 9, then run 
               //combination or permutation 
     if (n_input <= 9 && n_input >= 1 && r_input <= 9 && r_input >= 1){ 
      if (order == 'y') 
       solution = run_permutation(n_input, r_input); 
      else if (order == 'n') 
       solution = run_combination(n_input, r_input); 
      else 
       printf("\nError. Please type 'y' or 'n' to determine if order matters.\n"); 

      //if n < r, run_permutation or run_combination returns 0 
      if (solution == 0){ 
       printf("Error! You can't choose %i values at a time if there \n", 
        "are only %i total values. Type in new values next loop \n.", r_input, n_input); 
      } 
      else 
       printf("Number of possibilities: %s", solution); 
     } 
     else{ //else error message if numbers are out of range... 
      printf("Next loop, type in values that range from 1 to 9.\n"); 
     } 

      //option 'q' to quit out of loop 
      printf("Type 'q' to quit or enter any key to continue.\n"); 
      printf("User: "); 
      cin >> choice; 
    } 

    _getch(); 
} 

/* 
Returns solved combination of parameters n and r 
Takes the form: n!/r!(n-r)! 
*/ 
int run_combination(int n, int r){ 
    if (n < r) //solution is impossible because you can't choose r amounnt at a time if r is greater than n 
     return 0; 
    int n_fac = solve_factorial(n); //n! 
    int r_fac = solve_factorial(r); //r! 
    int nMinusr_fac = solve_factorial(n-r); //(n-r)! 

    int solve = ((n_fac)/((r_fac)*(nMinusr_fac))); // plugging in solved factorials into the combination formula 
    return solve; 
} 

int run_permutation(int n, int r){ 
    if (n < r) 
     return 0; 
    int n_fac = solve_factorial(n); 
    int nMinusr_fac = solve_factorial(n-r); 

    int solve = ((n_fac)/(nMinusr_fac)); //plugging in factorials into permutation formula 
    return solve; 
} 

int solve_factorial(int f){ 
    if (f-1==0 || f == 0){ //if parameter f is 1 or 0, return 1 
     int temp = f_value; 
     f_value = 1; //reset f_value so that f_value remains 1 at the start of every new factorial 
     return temp; 
    } 
    else{ //else multiply f_value by f-1 
     f_value *= f; 
     return solve_factorial(f-1); 
    } 
} 
+3

你現在可以學習如何使用調試器! – 2012-02-10 22:51:38

+3

再說一句。混合C型函數(printf)和C++ ioclases(cin)是一種糟糕的風格。除非有必要,否則不要混用它們。如果你使用cout而不是printf,你會逃避你的錯誤,代碼會更好看 – mikithskegg 2012-02-10 23:00:01

+2

'main'返回一個'int'而不是'void',看到這個[SO問題](http://stackoverflow.com/questions/636829/void-main-and-int-main) – 2012-02-10 23:06:58

回答

1

問題的行是:

printf("Number of possibilities: %s", solution); 

你告訴printfsolutionchar*,因此它試圖取消引用(char*)solution要打印的內容「C字符串」(假設您的特定錯誤消息爲solution的值爲5)。

變化%s%d,或使用std::cout代替printf獲得類型安全和避免這類問題擺在首位。

5

這是一個錯誤:

printf("Number of possibilities: %s", solution); 

solutionint,不是一個空結束的字符串:使用%d

使用std::cout這是類型安全,而不是printf(),就可以防止此錯誤:

std::cout << "Number of possibilities: " << solution; 
+0

@nightingale,這是否解決了它? – hmjd 2012-02-19 09:46:32