c++ - 当用户从命令行给出浮点数时,为什么我的 C++ 程序一直冻结?
问题描述
我正在用 c++ 编写一个程序,它将找到幂集的总和。我想包括一些错误处理来清除诸如 0、字符或非整数之类的东西。我的策略很简单。程序进入一个无限while循环,只有当用户输入的模数为零时才能退出。我期望发生的事情:用户输入 2.4 (2.4 % 1 = .4, .4 != 0) 程序再次循环并告诉用户输入一个整数。会发生什么:用户进入 2.4,程序冻结。
有人可以告诉我发生了什么事吗?
谢谢
// This C++ program will compute sum of all subsets of a set "S" stored
// in an array data structure. We assume that these subsets are the
// powerset of S and not the permutation set.
// The program workflow is as follows:
//
// 1) Prompt the user to provide the length and elemets to a custom array, if none, use
// the default array.
// 2) The user creates a custom array.
// 3) Sort the array using the QuickSort function.
// 4) Get the sum by running the SubArraySum function
#include<iostream>
#include<limits>
#include<cmath>
using namespace std;
// Utility functions for quicksort
void Swap(int *x, int *y)
{
int temp = *x;
*x = *y;
*y = temp;
}
int RemoveDuplicates(int arr[], int size)
// This function removes duplicate elements by setting their value to
// zero. This works because the purpose of the main code is to sum the subarrays.
// Adding zero to the sum multiple times does not change the sum. However, technically this will
// shorten the array.
// So, we need to keep track of the number of duplicates and give this to the SubArraySum
// function since it used the length of the array to calculate the sum.
{
int dupCount = 0;
for(int i = 0; i < size-1; i++)
{
if(arr[i] == 0)
{
;
}
if(arr[i] == arr[i+1])
{
arr[i] = 0;
dupCount++;
}
}
return dupCount;
}
int Partition(int arr[], int low, int high)
{
int i = low - 1;
int pivot = arr[high];
for(int j = low; j <= high - 1; j++)
{
if(arr[j] < pivot)
{
i++;
Swap(&arr[i], &arr[j]);
}
}
Swap(&arr[i+1], &arr[high]);
return i+1;
}
void PrintArray(int arr[], int size)
{
cout << "[ ";
for(int i = 0; i < size-1; i++)
{
cout << arr[i] << ", ";
}
cout << arr[size-1] << " ]" << endl;
}
// Quicksort to sort the array
void QuickSort(int arr[], int low, int high)
{
if(low < high)
{
int p_idx = Partition(arr, low, high); // p_idx is the partition index
QuickSort(arr, low, p_idx-1);
QuickSort(arr, p_idx+1, high);
}
}
void GetSum(int arr[], int n, int dupCount)
// Computes sum all sub-array
// arr[] is an array provided by the user or if none given the
// default array is arr[] = {1,2,3,4...10}
{
int p = pow(2, n-1-dupCount);
long int sum = 0;
//cout << "These are the subarrays: " << endl;
// The outer forloop picks the starting point for each subarray
for (int i=0; i < n; i++)
{
sum += p*arr[i];
}
cout << "This is the sum: " << sum << endl;
}
void SubArraySum(int arr[], int size)
{
PrintArray(arr, size);
QuickSort(arr, 0, size-1);
int dupCount = RemoveDuplicates(arr, size);
GetSum(arr, size, dupCount);
}
void DefaultSubArraySum()
{
int arr[] = {1,2,3,4,5,6,7,8,9,10};
int n = sizeof(arr)/sizeof(arr[0]);
SubArraySum(arr, n);
}
// Driver program to test above function
int main()
{
cout << "Enter a whole number for the length of the array or enter 0: ";
// This while loop will chatch any entries that were not whole numbers and force
// the user into a loop until they give a valide number.
double user_length;
cin >> user_length;
while(1)
{
double is_whole = fmod(user_length,1.0);
if(cin.fail())
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cout << "Entry invalid. Please enter a whole number or enter 0: ";
cin >> user_length;
}
if(is_whole != 0.0)
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cout << "Entry invalid. Please enter a whole number or enter 0: ";
cin >> user_length;
}
if(is_whole == 0.0)
{
break;
}
}
// If the user does not specify an array the default sub array sum will be calculated.
if(user_length == 0.0)
{
// Default code if the user enters 0.
DefaultSubArraySum();
}
else
{
cout << "this user_length: " << user_length << endl;
int arr[(int)user_length];
cout << "Enter your array values: " << endl;
for(int i = 0; i < user_length; i++)
{
cout << i+1 << ": ";
cin >> arr[i];
}
int n = sizeof(arr)/sizeof(arr[0]);
SubArraySum(arr, n);
}
int clear_buffer = cin.get();
return 0;
}
解决方案
好的,所以问题是我使用 % 作为我的模数运算符。这是一个二元运算符,当我使用变量 int user_length 运行它时它失败了。为了解决这个问题,我删除#include<math.h>
并添加了#include <cmath>
. 我删除了 % 运算符并将其替换为 fmod(x,y) 函数。这纠正了冻结问题。但是,代码仍在与字符作斗争。出于某种奇怪的原因,如果一个字符连续输入两次,is_whole 将变为 0,并且我的默认函数将运行。向@churill 大声疾呼,因为他发现了 % mod 冲突。
这是一个简化的,主要是工作代码。
#include<iostream>
#include<limits>
#include<cmath>
using namespace std;
void PrintArray(int arr[], int size)
{
cout << "[ ";
for(int i = 0; i < size-1; i++)
{
cout << arr[i] << ", ";
}
cout << arr[size-1] << " ]" << endl;
}
// Driver program to test above function
int main()
{
cout << "Enter a whole number for the length of the array or enter 0: ";
// This while loop will chatch any entries that were not whole numbers and force
// the user into a loop until they give a valide number.
double user_length;
cin >> user_length;
while(1)
{
double is_whole = fmod(user_length, 1.0);
cout << "This is the value of is_whole: " << is_whole << endl;
if(cin.fail())
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cout << "Entry invalid. Please enter a whole number or enter 0: ";
cin >> user_length;
}
if(is_whole != 0.0)
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cout << "Entry invalid. Please enter a whole number or enter 0: ";
user_length = 0;
cin >> user_length;
}
if(is_whole == 0.0)
{
break;
}
}
// If the user does not specify an array the default sub array sum will be calculated.
if(user_length == 0.0)
{
// Default code if the user enters 0.
cout << "The user entered 0 " << endl;
}
else
{
cout << "this user_length: " << user_length << endl;
int arr[(int)user_length];
cout << "Enter your array values: " << endl;
for(int i = 0; i < user_length; i++)
{
cout << i+1 << ": ";
cin >> arr[i];
}
int n = sizeof(arr)/sizeof(arr[0]);
PrintArray(arr, n);
}
int clear_buffer = cin.get();
return 0;
}
推荐阅读
- c# - 修改实体框架查询以将特定字段带入多级选择
- c++11 - 使用 main 函数将文件编译为特定文件名而不是“a.out”
- google-apps-script - UrlFetch 状态代码 200 正常,但响应显示“不支持的浏览器”
- php - EDirectory:Symfony:必须定义参数“alias_faq_url_divisor”
- c# - 算法比较 2 个列表并找到错误最少的方法
- python - 在 Python 脚本中运行 AppleScript
- c++ - int的返回位置
- sql - 如何更改存储在 S3 中的 Athena 结果的名称?
- shell - 使用 AWSCLI 设置 AWS cloudwatch diskspaceavailable 警报
- c++ - 如何将列添加到多维向量(C++)?