Moving through an array one step at a time

This is a discussion on Moving through an array one step at a time within the C++ Programming forums, part of the General Programming Boards category; Hi Everyone I'm having a real issue with my array. I am currently doing an exercise for a chapter on ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    3

    Moving through an array one step at a time

    Hi Everyone

    I'm having a real issue with my array. I am currently doing an exercise for a chapter on arrays.

    I have initialised the bool array, and all the elements are set to false.

    I need to go through the array and only change one of the false elements to true each time, so if a false element is found and turned to true; the loop needs to stop and continue once more input is received.

    I have tried several different ways to do this but none seem to work, does anyone have any suggestions?

    Thank you!

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,709
    I have tried several different ways to do this but none seem to work, does anyone have any suggestions?
    Show your work and we can fix it.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    3
    Quote Originally Posted by whiteflags View Post
    Show your work and we can fix it.
    Here you go but this isn't finished, I am just trying to get this part to work correctly.
    I'm sorry if it's a really poor attempt.
    Code:
    #include "Reservation.h"
    #include <iostream>
    using namespace std;
    
    
    
    Reservation::Reservation(void)
    {
    
    	SeatsArray[0] = false;
    	SeatsArray[1] = false;
    	SeatsArray[2] = false;
    	SeatsArray[3] = false;
    	SeatsArray[4] = false;
    	SeatsArray[5] = false;
    	SeatsArray[6] = false;
    	SeatsArray[7] = false;
    	SeatsArray[8] = false;
    	SeatsArray[9] = false;
    	int Selection = 0;
    	
    
    }
    
    
    
    void Reservation::ClassSelection()
    {
    	while (Selection != 0)
    	{
    	cout << "Welcome the the airline booking system!" << endl
    		<< "Would you like to fly 1st or 2nd class? Enter '1' for 1st and '2' for 2nd" << endl
    		<< "Enter here: "; cin >> Selection;
    	FillArray(Selection);
    	SetSelection(Selection);
    	}
    	exit (1);
    
    
    }
    
    void Reservation::SetSelection(int Selection)
    {
    
    }
    
    
    
    void Reservation::FillArray(int Selection)
    {
    	if (Selection == 1)
    	{
    		for  ( int i = 0; i < 5; i ++ )
    		{
    		  SeatsArray[i] = true;
    		   
    
    		 if (SeatsArray[i + 1] == false ) 
    		 {
    			i = 5; // Try to get the for loop to end;
    		 	 
    		
    		}
    		
    		
    	}
    		
    	
    }
    }

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,305
    Yeah, I suggest you post some code.

    Edit: Okay so you did. I now suggest you show the definition of SeatsArray. I'm curious as to why you are intialising 10 items (without a loop) and then only accessing 5 items later in a loop.
    You also need to fix your indentation so it is not misleading where the loop ends etc.
    Last edited by iMalc; 04-24-2011 at 02:23 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,709
    Quote Originally Posted by matinthehat View Post
    Here you go but this isn't finished, I am just trying to get this part to work correctly.
    I'm sorry if it's a really poor attempt.
    OK, well, you originally said:

    I need to go through the array and only change one of the false elements to true each time, so if a false element is found and turned to true; the loop needs to stop and continue once more input is received.
    There are a couple of things to note about the way you put this problem. First, think about how the input relates to the logic of the program. If you have to continue a loop as long as there is input, I would think that input somehow controls the loop. If the input is a seat number, then you would check if the seat is available and give that person a seat if it is, or ask them where else they should sit. You have to do that until everyone has a place to sit.

    So:
    Code:
    while there is input available:
      check if there are seats available:
        if not, break
      else:
        ask for a seat
        check if that seat is available
        if it is, seat the person
        if not, repeat
    Have fun coding that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why does the memory increase step by step?
    By zcrself in forum C Programming
    Replies: 9
    Last Post: 07-14-2010, 12:04 AM
  2. Synchronizing time step.
    By Auradrummer in forum Game Programming
    Replies: 2
    Last Post: 04-29-2009, 02:45 PM
  3. moving from one array adress to another
    By jrb47 in forum C++ Programming
    Replies: 2
    Last Post: 11-10-2006, 04:32 PM
  4. Moving to the next structure array
    By mattz in forum C Programming
    Replies: 2
    Last Post: 11-30-2001, 02:43 PM
  5. Moving values in an array
    By venomgts550 in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2001, 05:41 PM

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