【问题标题】:char array input limit?char数组输入限制?
【发布时间】:2012-11-13 04:16:56
【问题描述】:

如何使用 C++ 从控制台读取 1000 个字符?

对答案的评论更新:“我想要的是用户可以输入一个段落(比如 500 或 300 个字符)” - 即不总是 1000 个字符

使用以下代码,我只能输入一个限制(大约两行)。我做错了什么?

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include <stdlib.h>
void main()
{
    char cptr[1000];
    cout<<"Enter :" ;
    gets(cptr);
    getch();
}

【问题讨论】:

  • &lt;iostream.h&gt; 仅适用于 C++ 的古老版本。你不应该使用它。改为包含&lt;iostream&gt;
  • 实际上,我使用的是相当古老的 c++ 版本。我有 borland c++ 3.0 问题是我所有的学校电脑都有这个版本并且他们不更新它所以我必须以这种方式编码。我有一个学校项目要为我的董事会考试做,它必须在我的学校电脑上运行,所以....

标签: c++ arrays limit


【解决方案1】:

希望这会有所帮助:

#include<iostream>

using namespace std;

int main()
{
    const int size = 1000;
    char str[size];

    cout << "Enter: " ;

    cin.read(str, size);

    cout << str << endl;
}

【讨论】:

  • @user1819921 using namespace std; 在 C++ 中非常标准。
  • 替代方案是在 cout、cin 和 endl 前加上 std::(即 std::cout、std::cin、std::endl)。
【解决方案2】:

使用getchar 在for循环中一次读取一个字符,如下所示:

            int i;
            for (i = 0; i < 1000; i++){
              cptr[i] = getchar(); 
            }

编辑:如果您想尽早中断循环,例如然后在新行字符上:

            int i;
            for (i = 0; i < 1000; i++){
                char c  = getChar();
                if(c == '\n'){
                  break;//break the loop if new line char is entered
                }
                cptr[i] = c; 
            }

【讨论】:

  • ,但如果我使用 getchar ,用户将不得不输入 1000 个字符。我想要的是用户可以输入一个段落(比如 500 或 300 个字符)
  • @user1819921:很好。您可以对字符添加类型检查,如果类型匹配则中断循环。用新的线路检查更新了答案。
  • 嘿,在你给出的代码中,我使用了 cin 而不是 getchar,因为我在使用 getchar 时遇到了一些错误。但即便如此,我最多可以输入 2 行。不知道我为什么会遇到这个问题。
  • @user1819921:让我们进行一些故障排除。在循环中添加语句以计算i 的值,一旦它进入if。这会告诉我们,如果它进入了 if 块?另外,在 for 循环之后,再次添加 cout 以打印 i 的值,以了解它运行了多少次?
  • void main() { char c,cptr[1000];诠释我;对于 (i = 0; i >c; if(c == '\n'){ break;//如果输入新行字符则中断循环 } cptr[i] = c; } cout
【解决方案3】:

这可能是因为您正在阅读新的一行。 gets(char* ptr) 在遇到新行时停止读取,并将终止字符附加到字符串。

【讨论】:

  • 那么,我该如何解决这个问题?比如说,用户想要输入一段 300-500 个字符左右的段落
  • @user1819921 在循环中调用gets,直到消耗完所有输入。在每个后续调用中,将指向字符串结尾的指针设置为加一。
  • "设置指向字符串结尾加一的指针"没有得到这部分,你能写代码吗?
  • @user1819921 实际上,我建议使用cin.read 尝试答案。假设您可以使用iostream,它可能会简单得多。
【解决方案4】:

这是用户可以输入段落(1000、500 或 300 个字符)的解决方案。

代码:

#include <iostream>
using namespace std;

int main()
{

  char ch;
  int count = 0;
  int maxCharacters=0;
  char words[1024]={' '};

  cout<<"Enter maxCharacters 300,500,1000 >";
  cin>>maxCharacters;

  cout << "\nProceed to write chars, # to quit: \n";

  cin.get(ch);
  while( (ch != '#')  )
  {
    cin.get(ch);    // read next char on line
    ++count;        // increment count

    words[count]=ch;
    cout <<words[count];     // print input

    if (count>= maxCharacters) break;

  }
  cout << "\n\n---------------------------------\n";
  cout << endl << count << " characters read\n";
  cout << "\n---------------------------------\n";
  for(int i=0;i<count;i++) cout <<words[i];
  cout << "\n"<< count << " characters \n";

  cout<<" \nPress any key to continue\n";
  cin.ignore();
  cin.get();

   return 0;
}

输出:

Enter maxCharacters 300,500,1000 >10

Proceed to write chars, # to quit:
The pearl is in the river
The pearl

---------------------------------

10 characters read

---------------------------------
 The pearl
10 characters

Press any key to continue

【讨论】:

  • 嘿,thnx fr d code.im 在输入字符时遇到问题,一旦我输入大约 2 行,进一步的输入停止和执行被挂起。 eg : after cout
  • 在 1 中,我可以输入的最大字符数为 126 个字符,之后不会进行进一步的输入,但如果我多次输入少于 126 个字符的字符,则输入会正常进行。跨度>
猜你喜欢
  • 2021-11-30
  • 2011-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-15
  • 2021-08-29
相关资源
最近更新 更多