I just know I'm doing something stupid.

The part of my assignment I'm working on is to prompt the user to enter the name of a stock, earnings per share, and price to earning ratio, and then display it after everything is entered.

What it's doing now is asking for stock name, earnings per share, then price-to-earning ratio, then displaying the inputted information, then asking for price-to-earning ratio four more times.

I know I've probably missed something big, but I keep googling and searching the forums here and I can't see any for loops with compound statements inside, so I wonder if that's what I'm screwing up?

Here's the code I'm working on, and thank you for any help you can give.

for (i=0; i < 5; i++)
        cout << "Enter the name of the stock: ";
        cin >> share.name;

        for (j = 0; j < 5; j++)
            cout << "Enter the earnings per share: ";
            cin >> share.earn;

            for (k = 0; k < 5; k++)
                cout << "Enter the price-to-earning ratio: ";
                cin >> share.PtE;
          cout << "You entered: \n"
         << share.name << "         "
         << share.earn << "         "
         << share.PtE;
7 Years
Discussion Span
Last Post by Mouche

Seems like you need to store all the values. But your code will only store the last value. that has been given like input.

If you need to store all the values that have been given. You should consider using Arrays.


Thank you.

My program is supposed to have five different stocks (eventually), but I've been trying to create various stubs to make sure each part is working. I thought the nested for loop was necessary to request all the information and to keep them together as records, isn't it?

I'm planning on having arrays like share.name[0], share.earn[0] to save the input, but should I have it at this point already?


Yes, when you are trying to store different values, its mandatory for you to have the suitable data storage. That is the array or variable list by which you can store the values.


Consider using a vector. To add data to the end of the vector, you could just use the function push_back(). That way you don't have to keep track of which index it is.

Also, you may have problems with that cin.ignore(). Check out good ways to do this in the sticky called "How do I flush the input stream?":

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.