简体   繁体   中英

C++ simple program help String Data

Thanks for helping me in advance! I am used to Java and C# but C++ is completely different. I am trying to do part B of this lab: http://cs.binghamton.edu/~sgreene/cs240-2010f/labs/lab2.html

Is this the idea that is assigned in Part B? Also I dont know what exactly is missing in the header file that deals with indef endif, etc. I did research and I dont seem to miss anything. And lastly, what does the lab assignment mean when it says: "In the [] operator, the index should be checked to confirm it is between 0 and 9. If it is not, the string "Undefined" should be returned instead." Thanks

ok so here are my updated files

#include "TenStrings.h"

using namespace std;

//Default Constructor
TenStrings::TenStrings()
{
    public:

    TenStrings str[10];
    str[0] = "String 1";
    str[1] = "String 2";
    str[2] = "String 3";
    str[3] = "String 4";
    str[4] = "String 5";
    str[5] = "String 6";
    str[6] = "String 7";
    str[7] = "String 8";
    str[8] = "String 9";
    str[9] = "String 10";
}
;

main.cpp --------------------------*/

#include "TenStrings.h"
#include <iostream>

int main()
{
    TenStrings varTen;

    std::cout << str[2] << std::endl;
    return 0; 
}

also is the line missing in my header file: #include ?

I notice that the assignment carries this warning:

This is an individual assignment - you may NOT work in groups. Do not write or dictate code for anyone else, and do not use code from anyone else. If you break this rule, you may fail the entire class.

Hence I will provide only some guidance, not code.

First I dont know where to create the new data strings, whether it should be in my header class or tenstrings.cpp?

The assignment says:

Implement this TenStrings class such that it acts as an array of 10 constant-data strings. Start with the basic [] operator, and the constructor.

The word "implement" is key. Until you know about inline function definitions (which I'm guessing you haven't gotten to yet), implementation always needs to be in one of the cpp files (in this case, tenstrings.cpp .)

Should I create an array of empty strings? or an array of String1, string2, string3, etc?

The assignment says:

In the constructor, the 10 strings should initially be set to "First String", "Second String", etc....

In other words, the constructor for the TenString class should make the first string (ie, at index [0] ) have the exact value "First String" ; and so on up through the tenth string (at index [9] ) having the exact value "Tenth String" . Apparently, the intent is to be able to change these assignments through the [] operator, which you are to write as part of the TenString class.

I hope this helps. (I think that I've interpreted the assignment correctly, but I can't guarantee that.)

Oh, and by the way, there's no need for this line:

#include <string>

since the assignment specifically says not to use the String class.

EDIT : Also, the header file seems to be missing a line at the beginning. The missing line is related to the #ifndef / #define / #endif protection that's mentioned in the assignment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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