954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Array with random function

[code = cplusplus]

int serials[MAX_PAX];

for (int i = 0; i

Seamus McCarthy
Light Poster
35 posts since Apr 2008
Reputation Points: 11
Solved Threads: 0
 

remove the statement break; that causes your loop to exit after its first pass.
what is the purpose of the max variable? It gets reset to zero every loop iteration, so it really serves no purpose.

vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
 

Yep removed the break and i removed the max aswell, left that dere by mistake. I still only holds one number, i tink it is cos the new random number generated when i book a new seat overwrite the element in the array which store the first random number. Any advice??

Seamus McCarthy
Light Poster
35 posts since Apr 2008
Reputation Points: 11
Solved Threads: 0
 

show us your randomnun( ) function - I'm betting I'll spot the problem in 3.2 nanoseconds.

vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
 

>I'm betting I'll spot the problem in 3.2 nanoseconds.
If it's what I think it is, what makes you think spotting the problem will take that long? ;)

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
>I'm betting I'll spot the problem in 3.2 nanoseconds. If it's what I think it is, what makes you think spotting the problem will take that long? ;)


photons from from screen to eyeball, then a little time for neurons to process.

Is this a new kinder, gentler avatar for you?

vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
 
int randomnum()
{

	    
#include<cstdlib>
#include<ctime>

//First seed the random number generator
srand((int)time(0));

int random_number;
int span = 10000;
int start = 10000;

//any time you want a random number
//between random_start and random_start + random_span
random_number = rand() % start + span;
//That random_number may occur more than once in a program.

 
gotoxy(25,16); cout << "reference number = ";


return random_number;

}

I also call on the random number in a another function cursorpoint();
			cout << randomnum();
Does that ave anything to do with it??
Seamus McCarthy
Light Poster
35 posts since Apr 2008
Reputation Points: 11
Solved Threads: 0
 

srand((int)time(0));
Put this function call in main( ), only call it once. Your loop goes so fast, the random number generator is getting reseeded with the same time value again and again, so you will generate the same "random" value repeatedly.

vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
 

>photons from from screen to eyeball, then a little time for neurons to process.
But you already knew what the problem was...we both did. That's negative time there. ;)

>Is this a new kinder, gentler avatar for you?
I can't be myself unless I have a mean looking avatar?

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

>But you already knew what the problem was...we both did. That's negative time there.
We both had high probability suspicions, but there's always room for some new twist.

>I can't be myself unless I have a mean looking avatar?
That all depends on the inner you.

vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
 
#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<ctime>
#include<string>
#include "cursor.h"
#include <windows.h>  //Sleep()


using namespace std;

const int MAX_PAX = 120;
const int ROWS = 30;
const int START_X = 9;
const int START_Y = 4;

int x ;
int y ;

char menu; 
void showPlane();
void cursorpoint();
int randomnum();
void cancelseat();
void populatePlane(char letter[],int rows[], int pax);
void greeting();
bool isUniqueSerial(int serial, int serials[], int pax);
const int total = 120;
int serial[total];
//const int mas = 120;
int serials[MAX_PAX];
//int max = 0;



void main()
{
	greeting();
	
}
	

void drawLine1()
{
   for(int i = 0; i < ROWS - 1; i++)
   {
      cout << (char)205 << (char) 203;
   }
}

void drawLine2()
{
   cout << (char)186;
   
   for(int i = 0; i < ROWS; i++)
   {
      cout << ' ' <<  (char)186;
   }
}


void drawLine3()
{
   cout << (char)204;
   
   for(int i = 0; i < ROWS - 1; i++)
   {
      cout << (char)205 << (char) 206;
   }
   
   cout << (char)205 << (char)185;
}

void drawLine4()
{
   for(int i = 0; i < ROWS - 1; i++)
   {
      cout << (char)205 << (char)202;
   }
}

void showPlane()
{
   
   int start_x = START_X + 1;
   int start_y = START_Y + 1;
   
   gotoxy(start_x, start_y -1);
   for(int i = 0; i < ROWS; i++)
   {
      cout << setw(2) << i + 1;
   }
   
   
   
   gotoxy(start_x, start_y);
   cout << (char)201;
   drawLine1();
   cout << (char)205 << (char)187;
   
   gotoxy(start_x - 1, start_y + 1);
   cout << 'A';
   drawLine2();
   
   gotoxy(start_x, start_y + 2);
   drawLine3();
   
   gotoxy(start_x - 1, start_y + 3);
   cout << 'B';
   drawLine2();
   
   gotoxy(start_x, start_y + 4);
   cout << (char)204;
   drawLine4();
   cout << (char)205 << (char)185;
   
   gotoxy(start_x, start_y + 5);
   cout << (char)186;
   for(int i = 0; i < ROWS-1; i++)
   {
      cout << "  ";
   }
   cout << ' ' << (char)186;
   
   gotoxy(start_x, start_y + 6);
   cout << (char)204;
   drawLine1();
   cout << (char)205 << (char)185;
   
   gotoxy(start_x - 1, start_y + 7);
   cout << 'C';
   drawLine2();
   
   gotoxy(start_x, start_y + 8);
   drawLine3();
   
   gotoxy(start_x - 1, start_y + 9);
   cout << 'D';
   drawLine2();
   
   gotoxy(start_x, start_y + 10);
   cout << (char)200;
   drawLine4();
   cout << (char)205 << (char)188;
  
}    

void cursorpoint()
{

    where_mouse(x,y); //this will wait for mouse to be clicked
    
	gotoxy(x,y);    //This will position the cursor at x,y on the screen (dos style window)

	//x will have the x position and y will have the y position of mouse where clicked.
	

	 if  (x >= 11  && x <= 69  && (x % 2) != 0 && y >= 6 && y <= 14 && y !=10 && (y  % 2) == 0 )		// x odd numbers
	    	
		 {  
		 	
			cout << (char)219;
//			cout << x << y;
//			cout << randomnum();
   			gotoxy (20,19); cout << "Press M to return to Menu" << endl;			
			cout << randomnum();


	   	 }
	   	 
	else if (x < 11  || x > 69  || (x % 2) == 0 || y < 6 || y > 14 || y ==10 || (y  % 2) != 0 )	   	 
	   	 {
	   	 
	gotoxy(10,1);  cout << "Error Please re book your seat by pressing B" << endl;
	   	 }
	char A,B,C,D;
	const int capacity = 4;
	char letter[capacity]= {0};
	int pax = 0;
	
	
	for (int i = 0; i <MAX_PAX; i++)
	{
//	int max = 0;
	serials[i] = randomnum();
//	max++;
		// store random num in int serial
	cout /*<<"serial"*/ << serials[i];	
//	break;
	
	}
	
	
	for (int i = 0; i < capacity; i++)
		{
		if ( y == 6)
			{ letter[pax] = 'A';
			  pax++;
			  break;
			} 
		else if ( y == 8)
			{ letter[pax] = 'B';
			  pax++;
			  break;
			}
		else if ( y == 12)
			{ letter[pax] = 'C';
			  pax++;
			  break;
			}
		else if ( y == 14)
			{ letter[pax] = 'D';
			  pax++;
			  break;
			}  
			
		}
		gotoxy(25,17); cout << "seat = " <<  letter << endl;
//		cout << pax;		//passanger counter
	
	    const int capacity2 = 120;
	    int rows[capacity2];
		for (int i = 0; i < capacity2; i++)
		{
		if ( x == 11)
			{ rows[i] = 1;
			  break;
			} 
		else if ( x == 13)
			{ rows[i] = 2;
			  break;
			}
		else if ( x == 15)
			{ rows[i] = 3;
			  break;
			}
		else if ( x == 17)
			{ rows[i] = 4;
			  break;
			}
		else if ( x == 19)
			{ rows[i] = 5;
			  break;
			}
		else if ( x == 21)
			{ rows[i] = 6;
			  break;
			}
		else if ( x == 23)
			{ rows[i] = 7;
			  break;
			}			
		else if ( x == 25)
			{ rows[i] = 8;
			  break;
			}
		else if ( x == 27)
			{ rows[i] = 9;
			  break;
			}
		else if ( x == 29)
			{ rows[i] = 10;
			  break;
			}
		else if ( x == 31)
			{ rows[i] = 11;
			  break;
			}			
		else if ( x == 33)
			{ rows[i] = 12;
			  break;
			}
		else if ( x == 35)
			{ rows[i] = 13;
			  break;
			}
		else if ( x == 37)
			{ rows[i] = 14;
			  break;
			}			
		else if ( x == 39)
			{ rows[i] = 15;
			  break;
			}			
		else if ( x == 41)
			{ rows[i] = 16;
			  break;
			}
		else if ( x == 43)
			{ rows[i] = 17;
			  break;
			}
		else if ( x == 45)
			{ rows[i] = 18;
			  break;
			}						
		else if ( x == 47)
			{ rows[i] = 19;
			  break;
			}			
		else if ( x == 49)
			{ rows[i] = 20;
			  break;
			}
		else if ( x == 51)
			{ rows[i] = 21;
			  break;
			}
		else if ( x == 53)
			{ rows[i] = 22;
			  break;
			}			
		else if ( x == 55)
			{ rows[i] = 23;
			  break;
			}			
		else if ( x == 57)
			{ rows[i] = 24;
			  break;
			}
		else if ( x == 59)
			{ rows[i] = 25;
			  break;
			}
		else if ( x == 61)
			{ rows[i] = 26;
			  break;
			}			
		else if ( x == 63)
			{ rows[i] = 27;
			  break;
			}			
		else if ( x == 65)
			{ rows[i] = 28;
			  break;
			}
		else if ( x == 67)
			{ rows[i] = 29;
			  break;
			}
		else if ( x == 69)
			{ rows[i] = 30;
			  break;
			}			
		}	
			gotoxy(25,18); cout << "row = "<< rows[0];  

}  

 
int randomnum()
{

	    
#include<cstdlib>
#include<ctime>

//First seed the random number generator
srand((int)time(0));

int random_number;
int span = 10000;
int start = 10000;

//any time you want a random number
//between random_start and random_start + random_span
random_number = rand() % start + span;
//That random_number may occur more than once in a program.

 
gotoxy(25,16); cout << "reference number = ";


return random_number;

}

void cancelseat()
{
//	serials[MAX_PAX];
	int refnumber;
    
    cout << "Please enter your reference number" << " ";
    cin  >> refnumber;
    
    for (int i = 0; i < MAX_PAX; i++ )
   { 
    if( refnumber == serials[i])
       {
        cout << " Your seat has been cancelled" << endl;
        serials[i] = 0;
        break;
       }
       
    else if ( refnumber != serials[i])
        {
         cout << "Sorry no seat reference number exist" << endl;
         break;
         
        }
	}   
    gotoxy (0,4); cout << "Press M to return to Menu" << endl;
    gotoxy (0,5); cout << "Press E to exit" << endl;
    gotoxy (0,6); cout << "Enter choice --->";
   

}

void greeting()
{
	const string MENU =  "   Menu   \n "
                             "   B = Book seat \n "
                             "   C = Cancel Booking\n "
                             "   E = Exit\n"
                     "   Enter choice--->";
    		cout << MENU;

	
	for(;;)
	{
		
	    
	    cin >> menu;
        menu = toupper(menu);
        
    	system("CLS");
       
        
	
	    if ( 'E' == menu ) 
	    {
	    break;
	    }
            
        else if ( 'B'== menu )
        {
        	showPlane();
        	cout << endl;
	        cursorpoint();
	        srand((int)time(0)); 
        }
        else if ( 'C' == menu )

        {
         
           cancelseat();
//         Sleep(3000);
//         system("CLS");
         
         
            
        }
   		else if ( 'M' == menu )

        {
         
		cout << MENU;
         
         
            
        } 
    
       
   }
   
  }


That still doesn't fix it though, i know my code is very messy but i've only been doin programming for a few months and the lecturers n Ireland are poor enuf to be honest!!

Seamus McCarthy
Light Poster
35 posts since Apr 2008
Reputation Points: 11
Solved Threads: 0
 

See comments in the code below pertaining to your random number function

int randomnum()
{
#include<cstdlib> //these two line belong at the beginning of program,
#include<ctime>  //not inside a function

//First seed the random number generator
srand((int)time(0));  //this line belongs in main, near its beginning
                          //it should only be called one time during program run

int random_number;
int span = 10000;
int start = 10000;

//any time you want a random number
//between random_start and random_start + random_span
random_number = rand() % start + span;   //good
//That random_number may occur more than once in a program.

 
gotoxy(25,16); cout << "reference number = ";  //what's this do??
//this function generates a number, it should not do any display


return random_number;

}
vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
 

Tried it no good i'm afraid!! well ya cout << reference number should be outside the function, again my programmin skills aren't very neat!!

Seamus McCarthy
Light Poster
35 posts since Apr 2008
Reputation Points: 11
Solved Threads: 0
 

You'll need to be a bit more specific as to what's not working. What result do you expect, what result actually occurs? Can you narrow down which function/part of a function is misbehaving?

You should write programs in such a way that you can test each function - make a small test program if you need to that feeds input, examines output till you're satisfied that part works.

Trying to whip out the whole thing at one go is a sure recipe for headaches.

vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
 

When i click on a seat the reference num is given and when i book a new seat a new reference is given, i want to able to cancel both reference numbers in the void cancelseat() but the function only recognises the newest reference number and doesnt' delete the first reference number.

Ya true thanks i will in the future!!

Seamus McCarthy
Light Poster
35 posts since Apr 2008
Reputation Points: 11
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You