- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
好的,我将在这里插入我正在开发的这个程序的代码,以帮助我完成工作。我不想详细说明它是如何使用的,但最重要的是我正在创建一个程序:从用户那里获取两个值,对它们进行操作,然后将它们存储到一个数组中。
现在我可以用一个非常简单的线性程序来做到这一点,但我遇到困难的地方是当程序启动时它要求用户输入两个值,然后程序存储它,然后再次循环,它将要求用户再输入两个值,然后我希望它再次存储,存储的次数取决于用户需要的数据点数量,最后我希望它打印出他们输入的所有操作值,最后我想让它把它导出到一个 txt 文件,但那会在以后出现。我只想了解基本情况。谁能帮我解决这个问题?
#include <iostream>
#include <set>
double dCoordinates(double x)
{
return x / 1000;
}
void Store(int x , int y)
{
int anCoorValues[] = { x, y };
}
int main()
{
std::cout << "How many data points do you need to enter?" << std::endl;
int nAmountOfDataPoints;
std::cin >> nAmountOfDataPoints;
for (int nCount = 0; nCount < nAmountOfDataPoints; nCount++)
{
std::cout << "Enter E/W Coordinates" << std::endl;
double dEW;
std::cin >> dEW;
std::cout << "Enter N/S Coordinates" << std::endl;
double dNS;
std::cin >> dNS;
Store(dCoordinates(dEW),dCoordinates(dNS));
}
}
最佳答案
您应该在 main 中声明一个容器,然后在需要时将其作为参数传递给其他函数。然后在任何时候你都可以输出容器。在您的情况下,最好使用 std::vector<std::pair<double, double>>
例如(顺便问一下,为什么Store函数的参数是int类型的?)
#include <vector>
#includde <utility>
double dCoordinates( double x )
{
return x / 1000;
}
void Store( std::vector<std::pair<double, double>> &v, double x , double y )
{
v.push_back( { x, y } );
}
int main()
{
std::cout << "How many data points do you need to enter?" << std::endl;
int nAmountOfDataPoints;
std::cin >> nAmountOfDataPoints;
std::vector<std::pair<double, double>> v;
v.reserve( nAmountOfDataPoints ) ;
//...
// Here you can output the folled vector
for ( const std::pair<double, double> &p : v )
{
std::cout << "( " << p.first << ", " << p.second << " )" << std::endl;
}
关于c++ - 如何将多个值存储到一个数组中以便稍后打印出来?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21838315/
我正在编写一个插件,有一个ajax调用向用户显示数据。 如果用户想在ajax成功时添加一些js?他可以从他的 js 脚本中做到这一点吗,比如定位这个 ajax 成功事件。 例如: $(documen
我有 html 代码,例如 - x 最初插入 div 'insert_calendar_eda_form'。 Javascript代码 calendar_eda_add
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 3 年前。 Improve this qu
我已经使用命令 sudo start myservice 启动了一个 upstart 服务。我想要一种方法,以便稍后我(或任何人)可以检查该服务是否已启动并正在运行。检查它的命令是什么? 最佳答案 找
我是一名优秀的程序员,十分优秀!