2013-04-17 75 views
0

我的代碼已完成並正在工作。但我無法弄清楚如何統計用戶進行的嘗試次數和輸入的無效賬號。我應該在cin >> accountNum之後開始主要執行此操作。用戶輸入9999退出後,應顯示輸入的嘗試次數和無效賬號。當我運行它時,我得到0的嘗試次數和-1輸入無效數字。C++數組和函數和計數器

#include <iomanip> 
#include <iostream> 
#include <fstream> 
using namespace std; 

void getAccountNumbers(int[], int); 
void displayAccountNumbers(int[], int); 
void selectionSort(int[], int); 
int binarySearch(const int[], int, int); 

int main() 
{ 
int accountNum; 
int results; 
int attempts = 0; 
int invalidNumbers = 0; 


const int ARRAY_SIZE = 18; // Array size 
int numbers[ARRAY_SIZE]; // Array with 18 elements 
int count = 0; 
//ifstream inputFile; 

getAccountNumbers(numbers, ARRAY_SIZE); 

cout << "Original Order" << endl; 
displayAccountNumbers(numbers, ARRAY_SIZE); 

selectionSort(numbers, ARRAY_SIZE); 

cout << "Sorted List" << endl; 
displayAccountNumbers(numbers, ARRAY_SIZE); 
cout << "********************" << endl; 

cout << "Enter an Account number or 9999 to quit" << endl; 
cin >> accountNum; 

if(accountNum == 9999) 
{ 
    cout << "Thank You!" << endl; 
} 

while(accountNum != 9999) 
{ 
    results = binarySearch(numbers, ARRAY_SIZE, accountNum); 
     if(results == -1) 
     { 
       cout << "That number was not found" << endl; 
       invalidNumbers = results++; 
     } 
     else 
    { 
     cout << "That number is valid " << endl; 
    } 
     attempts = results++; 
    cin >> accountNum; 
} 
cout << "Number of attempts: " << attempts << endl; 
cout << "Invalid numbers entered: " << invalidNumbers << endl; 


system("pause"); 
return 0; 
} 

void getAccountNumbers(int nums[], int size) 
{ 
ifstream inputFile; 
int count = 0; 

//Open the file 
inputFile.open("charges.txt"); 

while(count < size && inputFile >> nums[count]) 
    count ++; 
//Close the file 
inputFile.close(); 
} 

void displayAccountNumbers(int nums[], int size) 
{ 
for(int count = 0; count < size; count++) 
    cout << nums[count] << "\t"; 
cout << endl << endl; 

} 

void selectionSort(int nums[], int size) 
{ 
int startScan, minIndex, minValue; 

for(startScan = 0; startScan < (size - 1); startScan++) 
{ 
    minIndex = startScan; 
    minValue = nums[startScan]; 
    for(int index = startScan + 1; index < size; index++) 
    { 
     if(nums[index] < minValue) 
     { 
      minValue = nums[index]; 
      minIndex = index; 
     } 
    } 
    nums[minIndex] = nums[startScan]; 
    nums[startScan] = minValue; 
} 
} 

int binarySearch(const int nums[], int size, int value) 
{ 
int first = 0,  //First element 
    last = size - 1, // Last element 
    middle,   // Midpoint 
    position = -1; //Position of search value 

bool found = false; 
while(!found && first <= last) 
{ 
    middle = (first + last)/2; //Midpoint 
if(nums[middle] == value) 
{ 
    found = true; 
    position = middle; 
} 
else if(nums[middle] > value) // Value is in lower half 
    last = middle - 1; 
else 
    first = middle + 1; // Value is in upper half 
} 
return position; 
} 

回答

1

您的問題是在您嘗試添加到invalidNumbersattempts線。 ++ postfix操作符在它之前加上一個數字。你不必說invalidNumbers = results++;;您只需要invalidNumbers++;,同樣適用於attempts。你的代碼正在做的是將invalidNumbers(和attempts)設置爲results的值,然後替換爲results

+0

感謝您的幫助 – user1807815