Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何打印存储在数组中的值_C++_Arrays - Fatal编程技术网

C++ 如何打印存储在数组中的值

C++ 如何打印存储在数组中的值,c++,arrays,C++,Arrays,我正在创建一个函数,在这个函数中,我输入6个值(3个字符串,3个整数),将这些值存储在数组中,然后将每个值成对地打印出来 以下是我所拥有的: #include <iostream> #include <string> using namespace std; int main() { const int SIZE = 3; int time[SIZE] = {}; string name[SIZE] = {};

我正在创建一个函数,在这个函数中,我输入6个值(3个字符串,3个整数),将这些值存储在数组中,然后将每个值成对地打印出来

以下是我所拥有的:

#include <iostream>
#include <string>
using namespace std;

int main()
{
    const int SIZE = 3;
    int time[SIZE] = {};        
    string name[SIZE] = {};     

    for (int a = 0; a < 3; a++)
    {
        cout << "Enter runner name: ";
        getline (cin, name[+1]);        
        cout << "Enter runner time: ";
        cin >> time[+1];
        cin.ignore();
    }

    for (int a = 0; a < 3; a++)
    {
        cout << name << " finished in " << time << "\n";
    }
    return 0;
}
目前,我的输出如下所示:

name1 finished in time1
name2 finished in time2
name3 finished in time3
0x22fdf0 finished in 0x22fe10
0x22fdf0 finished in 0x22fe10
0x22fdf0 finished in 0x22fe10
如何将输入存储在数组中,然后将这些值输出给用户


如果已经回答了,请道歉。我还没有找到一个示例,其中数组填充了用户输入值,然后返回到显示器。

您需要使用
[]
下标操作符对数组进行实际索引。同样,打印时,应尝试为数组中的元素编制索引。因为当您尝试打印数组本身时,编译器会隐式地将数组转换为指针,指针在打印时将打印该数组中第一个元素的内存地址,而不是元素本身

因此,您可以取消对数组的引用以获取每个数组中的第一个值,但更好的方法是通过for循环中的
a
变量对其进行索引,如下所示:

#include <iostream>
#include <string>
using namespace std;

int main() {
    const int SIZE = 3;
    int time[SIZE] = {};
    string name[SIZE] = {};

    for (int a = 0; a < 3; a++) {
        cout << "Enter runner name: ";
        getline(cin, name[a]);
        cout << "Enter runner time: ";
        cin >> time[a];
        cin.ignore();
    }

    for (int a = 0; a < 3; a++) {
        cout << name[a] << " finished in " << time[a] << "\n";
    }
    return 0;
}
但是,现在只需将第一个元素打印三次。因此,为了解决这个问题,您需要使用一些指针算法,并将值增加a,以使元素0、1和2分别超过第一个元素:

for (int a = 0; a < 3; a++) {
    cout << *(name + a) << " finished in " << *(time + a)<< "\n";
}
for(int a=0;a<3;a++){

CUD<代码> GETLINE(CIN,No.[+1)];<代码> >请说明这是什么?“<代码>名称[+3] < /COD>?看起来像是在猜测写什么,而不是看一本好的C++书,如何使用循环读取数组。建议:杀死魔法数3。替换<代码>(int A=0;A<3;A++)。
带有
for(int a=0;a
for (int a = 0; a < 3; a++) {
    cout << *(name + a) << " finished in " << *(time + a)<< "\n";
}