简体   繁体   English

C ++字符串类输入

[英]C++ String Class Input

I'm attempting to read in user inputted values about put them into a string class array, but I'm getting a BAD EXCESS error, I'm guessing its something really simple that I'm doing wrong. 我试图读取用户输入的值以将它们放入字符串类数组中,但是却遇到了BAD EXCESS错误,我猜测它做错了一些非常简单的事情。 Here is what I got. 这就是我得到的。 Any help would be great 任何帮助都会很棒

int main()
{
   const int SIZE = 5;
   string fruity[SIZE];
   int i;

   cout << "Enter the names of five kinds of fruit:" << endl;
   for(i = 0; i < SIZE; i++)
   {
      cout << "Enter Name of Fruit" << endl;
      getline(cin, fruity[i]);
   }

   cout << fruity[i] << endl;

   return 0;
}
cout << fruity[i] << endl;

This is invalid. 这是无效的。 Do this instead: 改为这样做:

for(i = 0; i < SIZE; i++)
     cout << fruity[i] << endl; 

Because at that point your i will be equal to SIZE , and fruity[SIZE] is invalid. 因为那时您的i将等于SIZE ,而fruity[SIZE]无效。

After the for loop, the variable i has the value SIZE and so when you try to access it you go out of bounds. 在for循环之后,变量i的值为SIZE ,因此当您尝试访问它时,您将超出范围。 You will have to use another loop if you want to output it like so: 如果要这样输出,则必须使用另一个循环:

for (i = 0; i < SIZE; i++) {
    cout << fruity[i] << endl;
}

In the following statement 在下面的语句中

cout << fruity[i] << endl;

"i" is out of range. “ i”超出范围。 The value of "i", when exiting the for loop "5" And your compiler should have warned you about using "i" outside the scope of the for-loop. 退出for循环“ 5”时的值为“ i”,并且您的编译器应已警告您不要在for循环范围内使用“ i”。 Your for-loop sets fruity[0], fruity[1], fruity[2], fruity[3], fruity[4], but "fruity[5]" is out of range. 您的for循环设置了fruity [0],fruity [1],fruity [2],fruity [3],fruity [4],但“ fruity [5]”超出了范围。 Hence, garbage data when you try to print "fruity[i]". 因此,当您尝试打印“ fruity [i]”时会浪费数据。 This leads to a seg fault crash. 这会导致段故障崩溃。

Always make variables as local as possible . 始终使变量尽可能局部化 C++ allows you to define loop variables within the loop C ++允许您在循环中定义循环变量

 for(int i = 0; i < SIZE; i++)
 {
   ...
 }
 // i no longer in scope

so that they fall out of scope at the end of the loop, which is as local as it gets. 因此它们在循环的最后就超出范围了,因为它是本地化的。

Doing this will reveal that you are currently using i for accessing the array after the loop , at which point i has the value SIZE , resulting in an access out of bounds . 这样做将显示您在循环后当前正在使用i来访问数组,此时i的值为SIZE ,从而导致访问超出范围 (Remember, arrays have the indexes 0..SIZE-1 .) (请记住,数组的索引为0..SIZE-1 。)

I have no idea what the final line in your program 我不知道你程序的最后一行

cout << fruity[i] << endl;

is supposed to do, but if you want to output the array's conetent (as other answers suggest), you will indeed need another loop. 应该这样做,但是如果要输出数组的锥度(如其他答案所示),则确实需要另一个循环。

Other, more minor points: 其他更重要的要点:

  1. We don't really know what string class you are using, because you omitted the std:: prefix. 我们真的不知道您在使用什么字符串类,因为您省略了std::前缀。 (The same goes for all the other identifiers from the std library you are using.) I disapprove of that . (对于您正在使用的std库中的所有其他标识符,也是如此。) 我不同意这一点

  2. The correct type for array indexes is std::size_t . 数组索引的正确类型是std::size_t

  3. The std::endl manipulator will insert a '\\n' into an output stream and flush the stream's buffer . std::endl操纵器将在输出流中插入'\\n' 并刷新流的缓冲区 In an interactive console program as yours is this usually doesn't do any harm. 在您自己的交互式控制台程序中,这通常不会造成任何危害。 Remember it, though, as flushing the buffer prematurely might considerably slow down a program. 但是请记住这一点,因为过早刷新缓冲区可能会大大减慢程序速度。 (I have seen a case were a program writing a lot of data into a file stream in trickles of a couple of bytes was slowed down by an order of magnitude due to this.) In your case, flushing the output stream#s buffer manually is never really necessary. (由于这种情况,我已经看到一种情况,程序将几个字节的细流写入文件流中的数据被减慢了一个数量级。)在您的情况下,手动刷新输出流的缓冲区从来没有真正必要。 (Of course, you do want your prompts to appear before you read from the input stream, but this is achieved by std::cout being tied to std::cin by default, making std::cout flush whenever the program attempts to read from std::cin .) (当然,您确实希望您的提示出现在从输入流中读取之前出现,但这是通过将std::cout默认绑定到std::cin来实现的,只要程序尝试读取,就使std::cout刷新来自std::cin 。)

The program, as I would write it, would look like this: 如我所写,该程序将如下所示:

// Beware, brain-compiled code ahead!
#include <string>
#include <iostream>

int main()
{
   const std::size_t size = 5;
   std::string fruity[size];

   std::cout << "Enter the names of five kinds of fruit:" << '\n';
   for(std::size_t i = 0; i < size; ++i)
   {
      std::cout << "Enter Name of Fruit" << '\n';
      std::getline(std::cin, fruity[i]);
   }

   for(std::size_t i = 0; i < size; ++i)
   {
     std::cout << fruity[i] << `\n`;
   }

   return 0;
}

You need a loop: 您需要一个循环:

for(int i = 0; i < SIZE; i++)
{
   cout << fruity[i] << endl;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM