简体   繁体   English

写一个循环,读取十个数字,然后输出它们

[英]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.

相关问题 C++ ; 编写一个读取十个整数的程序 - C++ ; Write a program that reads in ten whole numbers 编写一个程序,输出总共5位数字,其中包含5个数字,但不包含8个数字 - Write a program that outputs the total of 5-digit numbers with a five in them, but not with an 8 仅使用两个变量和一个循环将十个数字相加 - Add ten numbers using only two variables and a loop c ++终止while循环,以&#39;|&#39;读取数字 - c++ Terminate while loop that reads numbers with '|' while循环,读取两个整数,然后打印它们 - A while-loop that reads in two ints and then prints them 构建一个程序,该程序将10个整数放入数组中,并识别出重复的数字,并输出其位置编号,其中 - Building a program that puts 10 integers in an array and recognizes the numbers that are repeated and outputs them with their position numbers where C++,计算正负数并计算数字的平均值)编写一个程序,读取未指定数量的整数 - C++ ,Count positive and negative numbers and compute the average of numbers) Write a program that reads an unspecified number of integers 将响应限制为每行十个五角形数 - limiting responses to ten Pentagonal numbers per line 读取用逗号分隔的数字,并将其写入到用制表符分隔的文件中 - Read numbers separated by comma and write them to a file separated by tab 我正在尝试编写一个输出“n”(输入)斐波那契数的程序 - I'm trying to write a program which outputs "n" (input) fibonacci numbers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM