gpt4 book ai didi

C++ 数组、函数和计数器

转载 作者:行者123 更新时间:2023-11-28 07:39:58 29 4
gpt4 key购买 nike

我的代码已完成并可以正常工作。但我无法弄清楚如何计算用户尝试的次数和输入的无效帐号。我应该在 cin >> accountNum 之后在 main starting 中执行此操作。用户输入9999退出后,应该会显示尝试次数和输入的无效扣费帐号数。当我运行它时,尝试次数为 0,输入无效数字为 -1。

#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;
}

最佳答案

您的问题出在您尝试添加到 invalidNumbersattempts 的行中。++ 后缀运算符将其前面的数字加一。你不必说 invalidNumbers = results++;;您只需要 invalidNumbers++;,这同样适用于 attempts。您的代码所做的是将 invalidNumbers(和 attempts)设置为 results 的值,然后将 1 添加到 results 相反。

关于C++ 数组、函数和计数器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16050627/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com