[英]Display an array of 'double' numbers with unknown size in c++
更新1:在第二部分中,我需要帮助。 我要用户输入数字并使用零停止。 但是,然后我想显示该代码。 这就是我能走多远,因为当我想要显示它时,它会给我提供不同的数字。 我忽略了用户错误,假设他们在第一次输入时是正确的。 但是我确实希望他们输入负数。
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
float data;
int count=0;
int*arr=new int[count];
//get amount of numbers inputted
cout<<"Please enter floating point data.\n";
cout<<"After the last number has been entered press 0 (zero) \n";
cin>>data;
for (; data != 0 ; ++count)
{
cin>>data;
}
cout<<count<<endl;
cout<<endl;
//display back data
cout<<"The numbers enterd are: "<<endl;
for(int i=0; i<count; i++)
{
cout<<arr[i]<<endl;
}
cout<<endl;
system ("pause");
return 0;
}
**更新2:**此代码是我正在从事的一个较大项目的一部分。 我的教授永远也不会看到他只是在乎它在起作用的代码。 在这种情况下,我的代码设计不是优先事项。 而且我之前从未使用过std::vector
,所以我需要另一种方式来做。 但这就是我所做的,并且效果很好。 我还注释了delete []arr
因为如果不这样做,我的代码将无法正常运行。
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
double data;
int count=0;
double *arr =new double[count];
//get data
cout<<"Please enter floating point data."<<endl;
cout<<"After the last number has been entered press 0 (zero)"<<endl;
do
{
cin>>arr[count];
data = arr[count];
count++;
}while(data != 0);
//display back data
cout<<"The numbers entered are: "<<endl;
for(int i=0; i<(count-1); i++)
{
cout<<arr[i]<<endl;
}
//delete []arr;
system ("pause");
return 0;
}
给定的示例代码,
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
float data;
int count=0;
int*arr=new int[count];
//get amount of numbers inputted
cout<<"Please enter floating point data.\n";
cout<<"After the last number has been entered press 0 (zero) \n";
cin>>data;
for (; data != 0 ; ++count)
{
cin>>data;
}
cout<<count<<endl;
cout<<endl;
//display back data
cout<<"The numbers enterd are: "<<endl;
for(int i=0; i<count; i++)
{
cout<<arr[i]<<endl;
}
cout<<endl;
system ("pause");
return 0;
}
……有很多问题:
编码:无缘无故地使用float
。
C和C ++中的默认浮点类型为double
。 例如,文字3.14
的类型为double
,当传递给可变参数C函数时,任何float
值都会提升为double
。 仅在上下文提出要求的情况下使用float
,例如C API,或在有限的内存中存储不计其数的值。
编码:使用原始数组和new
的-expression。
使用std::vector
时,这项任务微不足道。 一次又一次地重新发明轮子不是一个好主意。 要求您重新发明std::vector
或其最基本功能的练习应该在这方面非常清楚:这不是必需的,因此大概不需要重新发明或其含义。
设计:通过特殊值来表示输入结束。
该值无法输入。
编码:不存储数字的输入循环。
每个数字都存储在与前一个数字相同的变量中,从而擦除前一个数字。 将数字存储在std::vector
。 例如,您可以使用push_back
方法。
编码:输出换行符约定不一致。
默认使用endl
或"\\n"
。 不要随意混合。 一致性非常重要,因为读代码的人(假设有能力的程序员)必须将由于某种原因而偏离已建立的默认值的所有情况视为错误。 如果没有理由,这会浪费时间和精力。
工具用法: system( "pause" )
愚蠢,适得其反且不可移植。
在您正在使用的Visual Studio中,只需通过Ctrl + F5运行该程序。 或在main
的最后一个右括号上放置一个断点,然后通过F5在调试器中运行程序。
编码: return 0;
是多余的。
main
是唯一具有默认返回值的函数。 它在C和C ++中都有。 将使用默认值0(表示成功)。
具有以上几点的示例代码,再加上一些固定的内容。
此代码适用于C ++ 11或更高版本,因此不会直接与Visual C ++ 2010一起编译 。
我将其保留为练习,以将其转换为C ++ 03,这是您当前的编译器可以处理的。 请考虑升级。 这是一个免费工具。
#include <iostream>
#include <iomanip> // std::setw
#include <string> // std::(string, stod)
#include <vector> // std::vector
using namespace std;
using Half_float = float; // Most often a silly choice.
using Float = double; // Default in C++.
using Extended_float = long double; // Same as double in Visual C++.
auto read_line()
-> string
{ string line; getline( cin, line ); return line; }
auto main() -> int
{
cout << "Please enter floating point numbers like " << 3.15 << ", one per line.\n";
cout << "After the last number just press return again (i.e. a blank line).\n";
vector<Float> numbers;
for( ;; )
{
cout << "#" << numbers.size() + 1 << "? ";
string const line = read_line();
if( line.empty() )
{
break;
}
numbers.push_back( stod( line ) ); // Here you CAN do input validation.
}
cout << numbers.size() << " numbers entered.\n";
cout << "\n";
cout << "The numbers entered were: \n";
for( int i = 0; i < int( numbers.size() ); ++i )
{
cout << setw( 3 ) << i << ": " << numbers[i] << "\n";
}
}
arr
的边界,该区域包含零个元素,除非您在第一个提示中输入0
。 arr
”(实际上会出现在任何元素arr
)在所有。 int
数组存储float
数据。 您应该使用std::vector
,它用作可变大小的数组。
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;
int main ()
{
float data;
int count=0;
vector<float> arr;
//get amount of numbers inputted
cout<<"Please enter floating point data.\n";
cout<<"After the last number has been entered press 0 (zero) \n";
while(cin >> data && data != 0)
{
arr.push_back(data);
}
count = arr.size();
cout<<count<<endl;
cout<<endl;
//display back data
cout<<"The numbers enterd are: "<<endl;
for(int i=0; i<count; i++)
{
cout<<arr[i]<<endl;
}
cout<<endl;
return 0;
}
如果您具有C ++ 11或更高版本,请使用auto
更换
for(int i=0; i<count; i++)
{
cout<<arr[i]<<endl;
}
与
for(auto v: arr){
cout << v << endl;
}
请记住,您需要支持C ++ 11的编译器才能使用auto。
如果C ++ <11,请使用std :: for_each
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;
void print(float v){
cout << v << endl;
}
int main ()
{
float data;
int count=0;
std::vector<float> arr;
//get amount of numbers inputted
cout<<"Please enter floating point data.\n";
cout<<"After the last number has been entered press 0 (zero) \n";
while(cin>> data && data != 0)
{
arr.push_back(data);
++count;
}
cout<<count<<endl;
cout<<endl;
//display back data
cout<<"The numbers enterd are: "<<endl;
std:for_each(arr.begin(), arr.end(), print);
cout<<endl;
system ("pause");
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.