Jump to content

Array counting help

- - - - -

  • Please log in to reply
3 replies to this topic

#1
AtomSai

AtomSai

    Newbie

  • Members
  • Pip
  • 3 posts
Suppose you had this:


    for (int i = 0; i < index; i++) {

        for (int k = index; k > 0; k--) {

            std::cin >> array[i];

            std::cin.get();

            std::cout << "You've added " << array[i] << std::endl;

            std::cout << "You have " << k << " slots left."

                    << std::endl << std::endl;

        }

    }


and index was == 5

Keep the above in mind.
Before I added my

for (int k = index; k > 0; k--) {

it would increment until i has reached the array's max storage space (which in this case was 5)

I'm trying to get k to decrement every time the user adds a new value into an array slot. Whatever I have up there does it successfully, but loops.

Anyways, after it successfully fills up the array with values in its slots, it will display them. If I am confusing you (which I probably am..sorry for the bad explanation) here is the whole program:


/* 

 * File:   main.cpp

 * Author: Admin

 *

 * Created on November 12, 2010, 8:50 PM

 */


#include <cstdlib>

#include <iostream>


/*

 * 

 */

int main(int argc, char** argv) {

    int index;

    int count;

    int left;


    /**

     * Record how many slots there will be within the array itself

     * @param argc

     * @param argv

     * @return

     */

    std::cout << "How many slots do you want your array to have: ";

    std::cin >> index;

    std::cin.get();


    /**

     * Record the size of the array

     * @param argc

     * @param argv

     * @return

     */

    int array[index];


    /*

     *

     */

    std::cout << std::endl << "Okay. You have " << index << " slots in your array";

    std::cout << std::endl << "What would you like the values of your "

            << index << " slots to be: "

            << std::endl << std::endl;


    /**

     * Record all the wanted values for the slots

     * @param argc

     * @param argv

     * @return

     */

    for (int i = 0; i < index; i++) {

        for (int k = index; k > 0; k--) {

            std::cin >> array[i];

            std::cin.get();

            std::cout << "You've added " << array[i] << std::endl;

            std::cout << "You have " << k << " slots left."

                    << std::endl << std::endl;

        }

    }


    /**

     * Display everything

     * @param argc

     * @param argv

     * @return

     */

    for (int i = 0; i < index; i++) {

        std::cout << array[i] << std::endl;

    }


    std::cin.get();

    return 0;

}


Thanks.

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
Try replacing your confirmation line with this:
            std::cout << "You've added " << array[i] << " into index " << i << std::endl;
You will notice it will always be index 0, because the inner for loop loops 5 times without touching the outer for loop, so i will remain the same i times (and loop i times extra).
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
AtomSai

AtomSai

    Newbie

  • Members
  • Pip
  • 3 posts

Nullw0rm said:

Try replacing your confirmation line with this:
            std::cout << "You've added " << array[i] << " into index " << i << std::endl;
You will notice it will always be index 0, because the inner for loop loops 5 times without touching the outer for loop, so i will remain the same i times (and loop i times extra).
Thanks for your comment. It didn't do exactly what I wanted, but definitely gave me an alternative + an idea on how to fix it up.

#4
mnirahd

mnirahd

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 330 posts
Hi,

I understand that each time user inputs an element to fill a slot in array, you want to tell him that how many slots are left for user to fill. Try the following code,



for (int i = 0; i < index; i++) {

            std::cin >> array[i];

            std::cin.get();

            std::cout << "You've added " << array[i] << std::endl;

            std::cout << "You have " << index - (i + 1) << " slots left."

                    << std::endl << std::endl;

  

    }


You will not see extra loops where you see the same message again and again.

I hope this helps!

Munir




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users