I could use some help getting my code to work.
The user inputs the time in digits (ex: 7 35) and
then the time is output in text (ex: seven thirty-five).
I have populated an array but i can't get anything to
print from the array. Here is what I have come up with
so far.....

#include <iostream>
#include <string>

using namespace std;

string harray[23];
string marray[58];
int hournum;
int minnum;
void setuparray();
string h;
string m;


int main()
{
    while (hournum != 9 && minnum != 99)
    {
        cout << "Enter time: ";
        cin >> hournum >> minnum;
        h = harray[hournum + 1];
        m = marray[minnum + 1];
        cout << h << m;
    }

    system("pause");
    return 0;
}


void setuparray()
{
    harray[0] = "one";
    harray[1] = "two";
    harray[2] = "three";
    harray[3] = "four";
    harray[4] = "five";
//i left out some of it because it is long


         marray[0] = "one";
    marray[1] = "two";
    marray[2] = "three";
    marray[3] = "four";
    marray[4] = "five";
    marray[5] = "six";
    marray[6] = "seven";
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Welcome to DaniWeb!

(First, please use code tags when you post code!)

You should use a std::vector<std::string>; Give that a shot and let us know if you have a specific problem.

Dave

As Dave suggested vector<string> myContainer; is the right(read: safe) way to go; not that yours is wrong. Can you post some sample output to explain your problem further?

I am not getting any output at all, the loop works correctly but it doesn't print anything to the screen, it just ask for input again. I have tried the suggestions but they haven't worked and I might not be using them correctly, I am very new to this.

It is not going to stop asking for input until you enter 9:99. while (hournum != 9 && minnum != 99) Dave

It is not going to stop asking for input until you enter 9:99.

while (hournum != 9 && minnum != 99)
Dave

True.
And also

h = harray[hournum + 1];
m = marray[minnum + 1];

You forgot to initialize hournum & minnum.

Edited 6 Years Ago by nbaztec: n/a

After a closer look you didn't call

void setuparray();

inside main(). Do it.

nbaztec, he is inputting those variables before using them, so any initialization would be overridden anyway.

Dave

No no, don't do that. There is no reason to use a global variable here. It is very bad practice. Your setuparray() function should return the vector<string> :

std::vector<std::string> setuparray()
{
std::vector<std::string> yourArray;
yourArray.push_back("one");
yourArray.push_back("two");
return yourArray;
}

Then in main:

std::vector<std::string> yourStringArray = setuparray();

Dave

he is inputting those variables before using them, so any initialization would be overridden anyway.

I agree yes but except while entering the loop they wont.

while (hournum != 9 && minnum != 99)

I agree there's 1 in 65535 chance for both (lol! :P) but still we can't rule it out.

There is no reason to use a global variable here.

Agreed x10. Globals are a no-no.

Or
You can also use a separate function to fill the names for you & similarly retrieve them for you.

void fillArray(string *thisArray)
{
//Fill here.
}
string getName(string *thisArray, int thisNum)
{
 return thisArray[thisNum-1];
}

After calling my void function in main() it works. Thank you so much for your help.

This question has already been answered. Start a new discussion instead.