- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
更新 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
类型,任何 float
值在传递给可变 C 函数时都会提升为 double
.仅在上下文强加要求的情况下使用 float
,例如一个 C API,或者在有限的内存中存储无数的值。
编码:使用原始数组和新
表达式。
使用 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";
}
}
关于c++ - 在 C++ 中显示大小未知的 'double' 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35965614/
我有一个交叉表函数,我过去曾多次成功使用它,但现在它在最后转储所有数据,而不是将其旋转到输出表中。它似乎无法找到交叉表。我通过以下方式对其进行了研究; 如果 tablefunc 不存在则创建扩展; -
表1(客户表) Id, CustomerId, IsKnownCustomer,phonemacaddress 1, 空 0 00:9a:34:cf:a4 2, 004024 1 00:6f:64:c
知道为什么我总是收到这个烦人且无用的错误代码/描述吗? Failed to pull image myapidemodocker.azurecr.io/apidemo:v4.0: rpc error:
我正在进行 PHP 登录,并且之前可以正常工作,但我尝试使用户名功能不区分大小写,但此后代码一直无法正常工作。我删除了我添加的所有内容,以尝试使其不区分大小写,即 strtolower()。页面上显示
有人会帮助我提供有关此错误的任何可能信息吗?原因?登录?在哪里寻找/开始? Cannot use output buffering in output buffering display handl
我已经添加了这样的脚本 我在我的 test.js 中做了这个 var app = angular.module('MyApp', ['ngRoute']).config
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 8 年前。 Improve this qu
我有这个sql语句: selectAllUsersByCriteria = 连接.prepareStatement( “从用户那里选择*?=?” ); 下面的方法运行该语句: public Array
我有一个白色的 EditText,在 Android 3.1 及更高版本中,光标不显示(因为它也是白色的)。有关信息,我使用 android:background="@android:drawable
我正在尝试使用 Keras 实现深度学习模型。但是我有一个未知形状实现的问题。我一直在寻找类似的错误,但没有找到。 这是我的代码。 Xhome = dataset[:,32:62] Xaway = d
关注此introduction可以通过导入命名空间 System.Xml 来使用 XMLReader 类。在我的 Visual Studio 项目中,我使用 .NET 4.0,但 System.Xml
我有一个动态链接库的程序。该程序将函数指针传递给该库以执行。 但是 ubsan(Undefined Behavior Sanitizer)指定指针位于错误的函数类型上。那只会发生 如果回调函数有一个类
我正在尝试在我的 Swift SpriteKit 应用程序中使用 AVAudioSession。我遇到了奇怪的“未声明类型”问题。例如…… import AVFoundation var audioS
如果在编译期间(在实际编译和运行程序之前)其参数之一的值已知/未知,如何专门化模板函数? 我还不知道怎么做。 想法 1: #include #include int main(void){
我看到一些人的代码是这样的: while (!(baseType == typeof(Object))) { .... baseType = baseType.BaseType;
我正在尝试使用 GoColly 框架获取所有 HREF 链接,但是只允许任何域的 url 为根 URL 或子域(否路径)。我已经注释掉了我的 REGEXP。文件扩展名没有事情。我只是在“/”之后不想要
我有一个包含多个实体的数据库,特别是 Book 和 User。它们之间存在这样的 ManyToMany 关系: 书: @Entity @Table(name = "Books") public cla
如果我将范围的初始部分设置为 Range("A:A"),如何确保将整行传递给排序? 数据 id、fname、mname、lname、后缀、状态、位置、时区 通过在 id 中搜索起点和终点来选择范围。
我对kubernetes很陌生,而对于docker来说就不那么多了。 我一直在研究示例,但是我对自动缩放器(似乎无法缩放)感到困惑。 我在这里通过示例https://kubernetes.io/doc
我在 ChildWindow 中使用 SL Toolkit 5 中的 BusyIndicator 控件。 在某些解决方案中,它可以工作,但在其他解决方案中,使用完全相同的代码(至少看起来),我在运
我是一名优秀的程序员,十分优秀!