:confused: Using loops..

This is a discussion on :confused: Using loops.. within the C++ Programming forums, part of the General Programming Boards category; Salut..i was wondering how to use a loop in order to store values every time. For instance i have this ...

  1. #1
    Registered User snapshooter's Avatar
    Join Date
    Sep 2004
    Posts
    37

    :confused: Using loops..

    Salut..i was wondering how to use a loop in order to store values every time. For instance i have this code:

    [code]
    const int max=5;
    int a[max];

    for(int i=0;i<max;i++)
    {
    a[i]=1;
    }

    when user type something(num or string), then i want the first element of the array to become 1 etc..and when user type the same thing the second element become 1...etc
    how to do that?

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Use cin to read a value from the user:
    Code:
    std::cin >>  Input;
    Then use a variable to keep track of the last entered value. When the user has inputted a value ocmpare this to the variable. I fthey are equal, enter 1 into the array.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User snapshooter's Avatar
    Join Date
    Sep 2004
    Posts
    37
    could you send me an example?

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,803
    instead of assigning a value to a just do something like this
    Code:
    const int max = 5;
    int a[max];
    for(int i = 0; i < max; i++)
    {
    
      std::cin>>a[i];
    
    }
    Woop?

  5. #5
    Registered User snapshooter's Avatar
    Join Date
    Sep 2004
    Posts
    37
    i am trying to make a function to do that..?

  6. #6
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,803
    What exactly are you trying to do? The solution I gave you will take an array of ints and fill them with user input. Which seems to me what you were asking
    Woop?

  7. #7
    Registered User snapshooter's Avatar
    Join Date
    Sep 2004
    Posts
    37
    i want to make a little program in order to book seats of an airplane, i have to use an 1d array[10], (0-5)---->First class seats,(6-10)------->Economical seats. When user type 1 goes to First class and 2 goes to Eco, so when user types 1 the first element of the array must be 1..when it press 1 again the second element must be 1 etc..and i do not know how to store the previous values...

  8. #8
    Registered User snapshooter's Avatar
    Join Date
    Sep 2004
    Posts
    37
    for example:

    [code]

    switch(num)
    {

    case 1: cout<<function(what am asking for)<<" seat taken from First class;
    case 2: cout<<function_2(...)<<"seat taken from eco class;
    }

  9. #9
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Quote Originally Posted by snapshooter
    could you send me an example?
    Nope, if I do Quzah might be furious...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Too many loops D:
    By F5 Tornado in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2007, 12:18 AM
  2. Confused about Memory
    By gL_nEwB in forum C++ Programming
    Replies: 22
    Last Post: 06-20-2006, 07:32 PM
  3. confused.. in selecting my line of deapth
    By jawwadalam in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-04-2003, 01:21 PM
  4. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 03:28 PM
  5. exiting loops with ease?
    By KingRuss in forum Game Programming
    Replies: 3
    Last Post: 09-24-2001, 08:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21