[英]Write a loop that reads ten numbers and then outputs them
I simply cannot figure out how to correctly output the numbers. 我根本无法弄清楚如何正确输出数字。 I know it has something to do with how I have the cout, but I can't figure out exactly what it is. 我知道这与我如何拥有cout有关,但我无法确切地知道它是什么。
#include <iostream>
using namespace std;
int main()
{
const int size = 10;
int values[size];
cout << "Please enter up tp 10 positive numbers." << endl;
for (int i=0; i < size; i++)
{
cin >> values[i];
}
cout << endl;
cout << values[size];
system("pause");
return 0;
}
这里有一个提示:要打印出数字,你需要使用一个循环。
#include <iostream>
using namespace std;
int main()
{
const int size = 10;
int values[size];
cout << "Please enter up tp 10 positive numbers." << endl;
for (int i=0; i < size; i++)
{
cin >> values[i];
}
cout << endl;
for (int i=0; i < size; i++)
{
cout << values[i]<<endl;
}
system("pause");
return 0;
}
You need to put another for loop below for when you print out the values. 当您打印出值时,需要在下面放置另一个for循环。 Currently you are only cout-ing the number at postion SIZE of your array. 目前,您只需在数组的位置SIZE处输入数字。 This is the last value. 这是最后一个值。
Try something like this: 尝试这样的事情:
cout << "Your values are :" << endl;
for (int j=0; j < size; j++)
{
cout << values[j] << " ";
}
Use 采用
for (int i=0; i < size; i++)
{
cout << values[i];
}
Happy Learning :) 快乐学习:)
you need another loop to output your values: 你需要另一个循环输出你的值:
// Read Values
for (int i=0; i < size; i++)
{
cin >> values[i];
}
// Print Values
for (int i=0; i < size; i++)
{
cout << values[i];
}
cout << values[size];
There is no way to recieve the all input and print it at the same time. 无法接收所有输入并同时打印它。 You have to use 2 for loops for that. 你必须使用2 for循环。 one to read the input and the other to print the array elements. 一个用于读取输入,另一个用于打印数组元素。
If you want to do it with less code, you can use the STL in c++. 如果你想用更少的代码来做,你可以在c ++中使用STL。 there is are vectors, LinkedLists, Queues. 有向量,LinkedLists,队列。
they're acts like an array but you don't have to allocate memory for the elements. 它们就像一个数组,但你不必为元素分配内存。
You just write vector <int> myVector;
你只需要编写vector <int> myVector;
To allocate memory for the vector, and then each time you add an element into the vector, it's allocate by itself a place. 要为向量分配内存,然后每次向向量中添加元素,它都会自行分配一个位置。 You don't need to worry about the allocations. 您无需担心分配问题。
To add an element I think there is a function called "backInsert(object x)" this function add the new element to the and of the vector. 要添加一个元素,我认为有一个名为“backInsert(object x)”的函数,这个函数将新元素添加到向量和向量中。
x.backInsert(userInput); // user input is an integer
to print the vector you can use this 打印矢量,你可以使用它
void printVector(const vector<int> &v)
{
std::copy(v.begin(), v.end(),
std::ostream_iterator<int>(std::cout, " "));
}
Edit : here is a simple program 编辑 :这是一个简单的程序
#include <iostream>
#include <vector>
#include <iterator>
void main()
{
std::vector<int> x;
for (int i = 0; i < 10; i++)
x.push_back(i+1);
std::cout << "vector elements: \n";
std::copy(x.begin(), x.end(),
std::ostream_iterator<int>(std::cout, " "));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.