为什么这段代码不起作用?C++
Why isn't this code working? C++
这段代码应该要求用户输入他们的名字,然后在空格处将其分开。
应该先把名字放在变量中,把姓氏放在变量lastname
中#include <iostream>
using namespace std;
int main()
{
char string[80];
char first[20];
char lastname[20];
bool f = true;
int c = 0;
cout << "Whats your Name? n";
gets(string);
for(int i =0; i < strlen(string); i++){
if(string[i] == ' ') {
f = false;
c = 0;
}
if(f) {
first[c] = string[i];
} else if(!f) {
lastname[c] = string[i];
}
c++;
}
for(int i = 0; i < strlen(first); i++) {
cout << first[i] << "n";
}
for(int i = 0; i < strlen(lastname); i++) {
cout << lastname[i]<< "n";
}
return 0;
}
除非您真的需要只使用C函数来编写此代码,否则使用c++字符串会容易得多。
类似于(这是未经测试的):
std::string input;
std::string first;
std::string lastname;
// prompt the user
std::cout << "What's your name? ";
// get a line of input
std::getline(std::cin, input);
// find a space in the string
size_t space = input.find_first_of(" ");
// was the space found?
if (space != std::string::npos)
{
// copy out the first and last names
first = input.substr(0, space);
lastname = input.substr(space + 1);
// output them to stdout
std::cout << first << std::endl << lastname << std::endl;
}
这意味着你不必担心null终止字符串或字符串长度或诸如此类的事情。正如flolo所说,您的代码没有做到这一点,因此肯定会遇到问题。C字符串的内存布局是一个末尾为空字节的字符数组,这就是strlen()之类的东西知道字符串末尾在哪里的方式。另外,如果有人输入了一个超过20个字符的名字,那么代码将会遇到很糟糕的情况,这并不是特别不可能。
你没有说你的程序是如何出错的。但是我看到的一个错误是由于c字符串以0结尾的事实。你必须加上"如果……"==" a
lastname[c]=0
(在您将c重置为0之前)和循环后a std::string
.
谈论艰难的方式。这样更容易使用char[]
,但是如果你坚持用gets
,就不要用fgets
(这是不可思议的破碎),但std::string
,第二,找到结束一劳永逸的字符串。所以要么(首选:
std::string line;
std::getline( std::cin, line );
if ( ! std::cin )
// Something when wrong...
typedef std::string::const_iterator Iter;
Iter begin = line.begin();
Iter end = line.end();
或:
char line[80];
if (fgets( line, stdin ) == NULL )
// Something went wrong...
typedef char const* Iter;
Iter begin = line;
Iter end = line + strlen( line );
if ( end != begin && *(end - 1) == 'n' )
--end;
然后找到第一个空格:
Iter pivot = std::find( begin, end, ' ' );
然后创建两个字符串first和last,要么:
std::string first( begin, pivot );
std::string last( pivot == end ? end : pivot + 1 );
或
char first[80] = { ' ' }; // nul fill to ensure trailing ' '
std::copy( begin, pivot, first );
char last[80] = { ' ' };
std::copy( pivot == end ? end : pivot + 1, end, last );
然后输出:
std::cout << first << std::endl;
std::cout << last << std::endl;
当然,如果你使用first
,你甚至不需要创建变量last
和first[c]=0;
0;你可以输出一个临时的:
std::cout << std::string( begin, pivot ) << std::endl;
std::cout << std::string( pivot == end ? end : pivot + 1, end ) << std::endl;
其他未提及的一些小问题:
if(f) {
first[c] = string[i];
} else if(!f) { // <- this "if" statement looks like you did not understand "if .. else"
lastname[c] = string[i];
}
所以最好这样写:
if(f) {
first[c] = string[i];
} else {
lastname[c] = string[i];
}
和部分
if(string[i] == ' ') {
f = false;
c = 0;
}
应该更好
if(string[i] == ' ') {
f = false;
c = 0;
continue;
}
,否则你的CC_11将总是包含一个前导空格。
相关文章:
- 我的神经网络不起作用 [XOR 问题]
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- C++为什么尽管我调用了void函数,它却不起作用
- 为什么在保护模式下继承升级不起作用
- 循环在计数器中不起作用
- 在其他文件中创建类时在 c++ 项目中不起作用
- Visual studio代码重构似乎不起作用(例如,重命名符号-f2)
- 为什么二进制搜索在我的测试中不起作用
- 我的代码中有错误吗?使用BGI图形的C++代码对我不起作用
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 带有指定长度字符* 参数的 std::regex_search 在 VS2017 中不起作用?
- Bjarne Stroustrup Book - std_lib_facilities.h - 不起作用(未知类型名称)
- 为什么简单的算术减法在"if"条件下不起作用?
- 为什么Stroustup书中的has_f不起作用
- 你能检查一下为什么在这个代码中从链接列表中删除项目不起作用吗
- 嵌套While循环不起作用(C++问题)
- C++Matching Brackets 2解决方案不起作用
- 为什么这段代码不起作用,我该如何解决?
- 我正在开发服务器,ip作为参数传递不起作用