为什么这段代码不起作用?C++

Why isn't this code working? C++

本文关键字:不起作用 C++ 代码 段代码 为什么      更新时间:2023-10-16

这段代码应该要求用户输入他们的名字,然后在空格处将其分开。

应该先把名字放在变量中,把姓氏放在变量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,你甚至不需要创建变量lastfirst[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将总是包含一个前导空格。