我的输入是否正确写入我的数组
Is my input being written properly to my array?
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <ctype.h>
#include <cmath>
using namespace std;
int main(int argc, char *argv[])
{
char buffer[100]= {};
int length = 0;
cout << "Enter a string: ";
do
{
cin >> buffer;
}
while(cin.eof());
length = strlen(buffer);
int squareNum = ceil(sqrt(length));
cout << squareNum;
cout << buffer;
}
基本上我要做的是用我输入的字符串填充一个字符数组。但是我相信它只是写入数组,直到出现空格。
Ex.
Input: this is a test
Output: this
Input:thisisatest
Output:thisisatest
为什么它停在空间?我很确定它必须与 .eof 循环有关
while(cin.eof());
你不太可能在阅读一个单词后进入eof()。 你想要
while(! cin.eof());
或者更准确地说是循环之类的东西
while(cin >> buffer);
或者,更好的是,省去字符数组并使用 string
和 getline
.
您可以使用std::getline()
来获取每一行,例如
std::getline (std::cin,name)
这样,您的输入就不会被空格分隔符分隔
与其使用 cin.eof()
,不如尝试类似的东西:
std::string a;
while (std::getline(std::cin, a))
{
//...
}
相关文章:
- 为什么我的数组双精度函数不起作用?
- 为什么我的 scanf() 没有在我的数组上迭代我的 for 循环?
- C++ - 块不移动,我的数组不起作用
- 如何为我的数组选择更大的数据类型?
- C ++程序如何返回我的数组或写入全局变量
- 为什么我的数组值与此处的全局变量不匹配?
- 为什么我的数组或函数不起作用?
- 为什么我的数组值会更新为随机值?
- 为什么我的数组输出一个不在其中的元素
- 我该如何循环遍历我的数组(缓冲区——包含一个文本文件),并将其打印成30字节的块
- 对数组进行排序时遇到问题。为什么我的数组无法正确排序?
- 无法将任何内容存储到我的数组中
- 这两个代码之间的差异(为什么我的数组也有额外的空间,即使我限制了它)
- 如何在FFT之后设置我的数组的频带
- 我正在尝试计算 char 数组中的内容,直到 null 终止,但每次编译时,我都会得到一个比我的数组大的数字
- 为什么我的数组说部分通过写入访问冲突?
- 如何动态分配我的数组,重新分配给两次,等等
- 为什么输入文件中的数字没有进入我的数组 c++?
- 为什么我的数组代码的反向不能正常工作
- 我的数组大小调整代码在新数据上引发异常[size_t]