开发者

Why isn't this code working? C++

This code should ask the user for a their name and then split it at the space.

It should put the firstname in the variable first, and the last name in de variable 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 <&l开发者_如何转开发t; first[i] << "\n";
}
for(int i = 0; i < strlen(lastname); i++) {
    cout << lastname[i]<< "\n";
}

return 0;
}


Unless you really need to write this using only C functions, it would be much easier to use C++ strings.

Something like (this is untested):

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

This means you don't have to worry about null-terminating strings or string lengths or anything like that. As flolo said, your code doesn't do that and thus will definitely run into problems. The memory layout of a C string is an array of characters with a null byte on the end, which is how things like strlen() know where the end of the string is. Also, your code's going to have a horrible time the moment somebody enters a name with more than 20 characters in it, which isn't particularly implausible.


You dont say how your program does behave wrong. But one error I see is due to the fact that c-strings are 0-terminated. You must add in your "if ...==" a first[c]=0; (before you reset c to 0) and after the the loop a lastname[c]=0.


Talk about doing things the hard way. It would be easier using std::string, but if you insist on using char[], don't use gets (which is irremdially broken), but fgets, and second, find the end of the string once and for all. So either (preferred:

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();

or:

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;

Then find the first space:

Iter pivot = std::find( begin, end, ' ' );

Then create the two strings first and last, either:

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

or

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

Then output:

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

Of course, if you're using std::string, you don't even need to create the variables first and last; you can output a temporary:

std::cout << std::string( begin, pivot ) << std::endl;
std::cout << std::string( pivot == end ? end : pivot + 1, end ) << std::endl;


Some minor issues not mentioned from the others:

    if(f) {
        first[c] = string[i];
    } else if(!f) { // <- this "if" statement looks like you did not understand "if .. else"
        lastname[c] = string[i];
    }

So better write:

    if(f) {
        first[c] = string[i];
    } else { 
        lastname[c] = string[i];
    }

And the part

 if(string[i] == ' ') {
        f = false;
        c = 0;
 }

should be better

 if(string[i] == ' ') {
        f = false;
        c = 0;
        continue;
 }

because otherwise your lastname will always contain a leading space.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜