繁体   English   中英

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

[英]Why isn't this code working? C++

此代码应询问用户其名称,然后在空格处将其分割。

它应将名字放在变量中,名字放在de变量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结尾的事实。 您必须添加“如果... == first[c]=0; (在将c重置为0之前),然后在循环之后,一个lastname[c]=0

谈论用困难的方式做事。 使用std::string会更容易,但是如果您坚持使用char[] ,则不要使用gets (这是不固定的),而要使用fgets和第二个,请一劳永逸地找到字符串的结尾。 因此,(首选:

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, ' ' );

然后首先创建两个字符串,最后创建两个字符串:

std::string first( begin, pivot );
std::string last( pivot == end ? end : pivot + 1 );

要么

char first[80] = { '\0' };  //  nul fill to ensure trailing '\0'
std::copy( begin, pivot, first );
char last[80] = { '\0' };
std::copy( pivot == end ? end : pivot + 1, end, last );

然后输出:

std::cout << first << std::endl;
std::cout << last << std::endl;

当然,如果你使用std::string ,你甚至不需要创建变量, firstlast ; 您可以输出一个临时的:

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;
 }

因为否则您的lastname将始终包含前导空格。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM