开发者

Can I do a multidimensional char array in c++?

First off, this is a "homework" question so vector libraries and string libraries are off limits. I'm trying to get to the basics of c++.

My intention with this code is to make and use an array of string arrays. A list of words in other words.

When I run this code I get a bunch of nonsense.

If there is a better way to make a list of words in c++, I would love to hear about it.

const int cart_length = 50;
const int word_length = 50;

int main()
{

char cart_of_names[cart_length][word_length]; 
float cart_of_costs[cart_length];

char name[word_length];
cout << "enter the name of the first item: ";
cin >> name;
for(int i=0; i<word_l开发者_C百科ength; i++)
{
    cart_of_names[0][i] = name[i];
}
cout << endl;
cout << "that is: ";
for(int x=0; x<word_length; x++)
{   

        cout << cart_of_names[0][x];
}
cout << endl;

return 0;
}


If the string entered is not 50 characters long (cart_length), then less than 50 characters will be valid in the name. You should have an if(cart_of_names[0][x]==0) break; in your second loop.


I don't exactly understand what you are looking for. Following code will help you to read and print a list of 50 words. Hope this would help you.

const int cart_length = 50;
const int word_length = 50;

int main()
{

    char cart_of_names[cart_length][word_length]; 
    float cart_of_costs[cart_length];

    for(int i=0; i<cart_length; i++)
    {
        cout << "enter the name of the " << i + 1 << "th item: ";

        cin >> cart_of_names[i];
    }

    cout << "that is: ";

    for(int x=0; x < cart_length; x++)
    {       
        cout << cart_of_names[x] << endl;
    }

    return 0;
}


Check out STLSoft's fixed_array_2d (and it's higher order siblings). There's a detailed discussion of how they're implemented for maximum performance in Matthew Wilson's Imperfect C++.


If you can't use std::string, at least look at the functions like strncpy() from C for your name copying. Also, you're forgetting that c-style strings are null terminated.


Unless you're forbidden to use STL (which would be just mean), just use std::list<std::string>. www.cplusplus.com has detailed descriptions and examples for those classes.

Otherwise, you're stuck with an array of char arrays: in that case, be prepared for a lot of buffer overflow errors. Look around on the above site for the char[] management functions (strncpy() and the like), they'll make your life a bit easier (but not a lot).


In C, the best way I found to conceptualize what you are trying to do is using an array of char*. Same effect, but if you start to work with it I believe you may find it is easier on the brain.


It looks pretty close to me. Strings in C are null-terminated, which means that the end of the string is indicated by a null character. In a sense, a string in C is really just an array of bytes.

When you do:

cout << "enter the name of the first item: ";
cin >> name;

If I enter the string "Book", in memory it'll look like something like:

|0|1|2|3|4|5..49|
|B|o|o|k|0|*HERE BE DRAGONS*

Well, really it will contain the ASCII values corresponding to those letters, but for our purposes, it contains those letters. There here be dragons is memory that that you didn't initialize, so it contains whatever garbage your platform sets it to.

So when you copy your string, you need to instead look for that 0 byte at the end of the string.

for(int i=0; name[i]!=0; i++)
{
    cart_of_names[0][i] = name[i];
}

Then when you output it, you don't actually need to do it a character at a time. You can just do cout<<cart_of_names[0]. cout knows where the string ends because of that terminating null character.


If you use strcpy() instead of cart_of_names[0][i] = name[i];

it may work better but I cringe just looking at all that code.


"If there is a better way to make a list of words in c++, I would love to hear about it."

Include #include <string> and use std::string. The std::string type is part of the C++ specification, I think.

#include <iostream>
#include <string>

int main(void) {
  std::string list[7];

  list[0] = "In C++";
  list[1] = "you can use";
  list[2] = "the `std::string` type.";
  list[3] = "It removes";
  list[4] = "many of the problems";
  list[5] = "introduced by";
  list[6] = "C-style strings.";

  for (int k=0; k<7; k++) std::cout << list[k] << ' ';
  std::cout << '\n';
  return 0;
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜