Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.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++ - Fatal编程技术网

C++ 为什么我的数组存储不需要的值

C++ 为什么我的数组存储不需要的值,c++,C++,我的代码有问题-我遇到运行时错误。数组应该只存储5个值,但实际上它存储的值更多 #include <iostream> using namespace std; int main() { const int num = 5; string t[num], name; int m[num], score; for(int i=0; i < num; i++) { cout << "Enter the name for score # " <&l

我的代码有问题-我遇到运行时错误。数组应该只存储5个值,但实际上它存储的值更多

#include <iostream>

using namespace std;


int main()
{

const int num = 5;
string t[num], name;
int m[num], score;

for(int i=0; i < num; i++)
{
    cout << "Enter the name for score # " << i+1 << " :";
    cin >> name;
    t[i] = name;

    for(int j=i; j<= i ;j++)
    {
        cout << "Enter the score for score # " << j+1 << " :";
        cin >> score;
        m[j] = score;
    }
}

for(int i=0; i < num; i++)
    cout << m[i] << endl;

}
这和你写的基本相同

编辑更新:

好吧,来回答OP的实际问题。。。。我想是吧

这只是因为你没有包括

下面是整个项目,包括一些优化和错误检查

#include <iostream>
#include <string>

using namespace std;

int main()
{

const int num = 5;
string t[num], test;
int m[num];
bool integer = false; 

for(int i=0; i < num; i++)
{
    cout << "Enter the name for score # " << i+1 << " :";
    cin >> t[i];
    cin.clear();
    cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    integer = false; 
    while(integer == false){
       cout << "Enter the score for score # " << i+1 << " :";
       cin >> m[i];
       if(!std::cin.fail())
          integer = true;
       cin.clear();
       cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }

}

for(int i=0; i < num; i++)
    cout << m[i] << endl;

}

我觉得对name和score变量的需求是没有意义的,您可以直接将它们存储到数组中。此外,我会确保您执行一些错误检查,以查看何时使用cin INT,它们实际上是INT而不是字符串。希望这有帮助

您没有遇到运行时错误,因为您的数组存储的数据超过了它应该存储的数据量。输入名称时,它可能包含空格。这将使cin>>得分;只读取第一个字符,其余字符留在输入缓冲区内

以下是我运行代码的结果:

[wolf@Targaryen]:~$ r
Enter the name for score # 1 :Alex
Enter the score for score # 1 :100
Enter the name for score # 2 :Bob
Enter the score for score # 2 :99
Enter the name for score # 3 :Charlie
Enter the score for score # 3 :98
Enter the name for score # 4 :Douglas
Enter the score for score # 4 :97
Enter the name for score # 5 :Evin
Enter the score for score # 5 :96
100
99
98
97
96
[wolf@Targaryen]:~$

但是,您的代码确实存在问题。int j=i的循环;什么是forint j=i;你根本不需要使用这个循环。只需删除循环和封闭的大括号,使Js和i。。。。那么,运行时错误在哪里呢?错误是什么?你的内部循环完全没有必要。它只会循环一次。你的临时变量也是完全不必要的。这是如何编译的呢?你不包括在内。除此之外,运行时错误无法再现。我可以问一下,这是如何解决OP所说的错误的吗?我实际上已经使用了progenhard代码,虽然我自己也想到了,但我仍然得到了相同的错误。有办法吗?+1。这正是这里的解决方案。我不知道为什么人们在这里给你带来困难。也许我只是看不到,但这段代码似乎没有对OP的代码进行任何功能更改。但检察官说他犯了个错误。这是如何解决OP的错误的?@sehe这是怎样的答案?我不明白。至少这一个试图解决运行时错误
#include <iostream>
#include <string>

using namespace std;

int main()
{

const int num = 5;
string t[num], test;
int m[num];
bool integer = false; 

for(int i=0; i < num; i++)
{
    cout << "Enter the name for score # " << i+1 << " :";
    cin >> t[i];
    cin.clear();
    cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    integer = false; 
    while(integer == false){
       cout << "Enter the score for score # " << i+1 << " :";
       cin >> m[i];
       if(!std::cin.fail())
          integer = true;
       cin.clear();
       cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }

}

for(int i=0; i < num; i++)
    cout << m[i] << endl;

}
[wolf@Targaryen]:~$ r
Enter the name for score # 1 :Alex
Enter the score for score # 1 :100
Enter the name for score # 2 :Bob
Enter the score for score # 2 :99
Enter the name for score # 3 :Charlie
Enter the score for score # 3 :98
Enter the name for score # 4 :Douglas
Enter the score for score # 4 :97
Enter the name for score # 5 :Evin
Enter the score for score # 5 :96
100
99
98
97
96
[wolf@Targaryen]:~$
getline(cin, name);
#include <iostream>
#include <cstdio>

using namespace std;


int main()
{   

const int num = 5;
string t[num], name;
int m[num], score;

for(int i=0; i < num; i++)
{   
    cout << "Enter the name for score # " << i+1 << " :";
    getline(cin, name);
    t[i] = name;

    cout << "Enter the score for score # " << i+1 << " :";
    cin >> score;
    m[i] = score;
    getline(cin, name); // This line just clear out the buffer. "name" used as a trash
}   

for(int i=0; i < num; i++)
    cout << t[i] << ": " << m[i] << endl;

}