开发者

to check the end of arrays

开发者 https://www.devze.com 2023-01-25 11:23 出处:网络
开发者_JS百科main() { char buff[10]; int a; for(int i=0; i<10;i++) { cin >> buff[i]; a=sizeof(buff[i]);
开发者_JS百科main()
{
    char buff[10];

    int a;

    for(int i=0; i<10;i++)

    {

        cin >> buff[i];

        a=sizeof(buff[i]);

        if(a==10)

            cout << "full";

    }

    for(int i=0; i<10;i++)

        cout << buff[i]<<"\n";

}

why des the control never comes to cout << "full"; i am new to coding...when i do dry run it works fine


You've defined buff to be an array of char. You then look at the sizeof one of those char's. The result of the sizeof operator depends only on the type of object you pass as its argument. In this case, what you're doing is equivalent to sizeof(char), which is defined to be 1 in both C and C++. Since 1 doesn't equal 10, your test will always produce false.

Edit: as an aside, my advice would be to back up, regroup, and start over. You generally do not want to read a string one character at a time, and until you gain some experience you probably shouldn't use arrays in C++ either. For an initial attempt at reading a string, what you generally want is something like:

#include <string>
#include <iostream>

int main() { 
    // define a string object to hold the data
    std::string input;

    // ask the user for some input
    std::cout << "Please type something:";

    // read data from the user:
    std::getline(std::cin, input);

    // we'll print it out again, to show what we got:
    std::cout << input;
    return 0;
}

Edit2: I should add that the original attempt (using strlen) was at least somewhat closer than the edited version (using sizeof). If you decide you really do want to read a string one character at a time, correct use of strlen would tell you when you'd read as much data as you'd made room for. Correct use isn't necessarily entirely obvious though. You'd need something more like this:

// Make room for 10 characters *plus* a terminating NUL and fill with NULs:
char buffer[11] = {0}; 

for (int i=0; i<10; i++) {
    std::cin >> buffer[i];

    if (strlen(buffer) == 10)
        std::cout << "full";
}

This is still pretty useless though. The if (strlen ... doesn't really give you any new or different information from the loop itself -- it will be true when (and only when) the last iteration of the loop is executing. As such, it's result is pretty much the same as if you wrote something like:

for (int i=0; i<10; i++) {
    std::cin >> buffer[i];
}
std::cout << "full";

One final point: as shown above, with C-style strings (arrays of char), the string has to be terminated with a NUL character, so you have to define the array with one more element than the maximum length of string you want to be able to store.


Because in this code:

a=sizeof(buff[i]);

you are getting the size of one of the elements in the buff array -- not of the whole array itself.

And since the elements in the array are chars, the size will always be one (as dictated by the Standard).

If you are trying to get the index of the element (that is, you want to hit on the 10th element), you need to do this:

 if(i==9)
        cout << "full";

(9 instead of 10 because arrays are zero-based, and the 10th item is at buff[9]. First is buff[0], second at buff[1], etc...)


I think it's because you call strlen on buff[i], when I'd guess you want to call strlen on buff. Remember, buff[i] is a single character.


well your test is

a==10

edit because John can't realize when the original code of a post has been edited after answers. so please disregard the following 3 lines because they have already been corrected:


and a = strlen(buff[i]);

you are basically comparing 1 character read by cin so always 1 with 10 which always return false hence never prints full


what you want to test is

if(i == 9)

why 9, because you are covering from 0 to 9 (<10) in your loop


its called zero based indexing. 10 array locations starting from 0.

0 1 2 3 4 5 6 7 8 9 = 10 locations.

0

精彩评论

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