views:

83

answers:

2

These are my variables:

    const int sizeOfLicToCheckFor = 3;
string licNameToCheckFor[ sizeOfLicToCheckFor ] = { "PROF", "PERS", "PREM" };

when I run my program licNameToCheckFor is only initialized with "PROF" and nothing else. What am I doing wrong?

+2  A: 

Works for me.

Why do you think it is not working?

#include <string>
#include <iostream>

const int sizeOfLicToCheckFor = 3;
std::string licNameToCheckFor[ sizeOfLicToCheckFor ] = { "PROF", "PERS", "PREM" };

int main(int argc, char** argv)
{
    std::cout << licNameToCheckFor[0] << " ";
    std::cout << licNameToCheckFor[1] << " ";
    std::cout << licNameToCheckFor[2] << " ";
}

> vi t.cpp
> g++ t.cpp
> ./a.exe
PROF PERS PREM >
Martin York
Would the debugger show incorrect variable values? I was testing to see if my strings were being properly initialized with the debugger.
TheFuzz
What debugger were you using? What did it show to you? May be it showed only first element of the array? "Need more input" (c)
Vlad Lazarenko
Im using the debugger from VS2008 C++. I ran the code a few times, and no dice. Its "PROF" every single time.
TheFuzz
I tried CString, and It shows all the values in the array in the debugger. What is with string?
TheFuzz
@TheFuzz: There is nothing wrong with string. It is probably you mis-reading your debugger output.
Martin York
@Martin York. I dont think I'm reading my debugger output wrong, because when I use CString the values are correctly shown in the debugger. One of my project setting are set to use multi byte characters. Could std::string not be able to handle multibyte characters?
TheFuzz
In the above example you are using string. So it is hard to tell what you are doing if you now claim to use CString. Especially since the string literals are using normal characters not wide characters. What I can tell you is that std::string is working perfectly in both release and debug (if it was broken a lot of people would be complaining).
Martin York
I told you CString works because that means I'm not reading my debugger output wrong. I'm still trying to figure out why std::string isn't working correctly. Which is why my example is using std::string
TheFuzz
The fact theat you even think that std::String is not working is the problem.
Martin York
I have ran my program using std::string, and it doesn't work correctly when I use it. When I switch over to CString my program runs correctly. I'm just trying to figure out how I might be using it wrong or if there is something funky about std::string with a project set to use mult byte characters. I know that there isn't anything wrong with std::string, but I can assure you that std::string is not working for my current situation and I'm trying to figure out why. Thanks
TheFuzz
+2  A: 

How are you checking if it was initialized properly? Most probably you are doing that wrong because code is absolutely correct:

#include <string>
#include <iostream>

using namespace std;

int
main ()
{
    const int sizeOfLicToCheckFor = 3;
    string licNameToCheckFor[ sizeOfLicToCheckFor ] = { "PROF", "PERS", "PREM" };
    for (int i = 0; i < sizeOfLicToCheckFor; ++i)
    {
        cout << licNameToCheckFor[i] << endl;
    }
}

The output:

$ g++ -o test ./test.cpp
$ ./test 
PROF
PERS
PREM

You can also simplify your code by not specifying number of strings in array, like this:

string licNameToCheckFor [] = { "PROF", "PERS", "PREM" };
Vlad Lazarenko