【发布时间】:2020-09-02 03:09:37
【问题描述】:
我使用字符数组从用户那里获取输入,然后显示输出。但是,每次我输入中间有空格的值时,只会打印空格前的第一个单词。
例如,这是我输入的:
Customer No.:7877 323 2332
这将是输出:
Customer No.:7877
我已经搜索了可能的解决方案,但似乎找不到合适的解决方案。
这是我的参考代码:
#include<iostream>
using namespace std;
int main()
{
char custNum[10] = " "; // The assignment does not allow std::string
cout << "Please enter values for the following: " << endl;
cout << "Customer No.: ";
cin >> custNum;
cout << "Customer No.: " << custNum << endl;
}
【问题讨论】:
-
不使用 std::string 的任何特殊原因?
-
@d4rk4ng31,我们被要求暂时不要使用它。如果可以的话会容易得多。
-
嗯....好吧,无论如何,尝试使用
char* str = new char[1];scanf("%[^\n]s",str);代替 -
Don't SKIMP on buffer size (e.g.
char custNum[32]) to read the line as a string requires a minimum of14characters (double that and take the next power of two for a粗略的破解)
标签: c++ arrays char character limit