Link to home
Create AccountLog in
Avatar of WestonGroup
WestonGroup

asked on

how to assign number value of array to string value of another array

I have 2 global variables  const char* airplanes[48] = {Plane 1, ....Plane 46}

int runways[7] = {1,2,3,4,5,6,7}

I have the below functions and trying to have the user enter a plane number and runway

then print out all the planes and have it list which planes are assigned to 1 of 7 runways

my problem is when I go to print them out it lists all 48 planes.....and then the runways as 0

if I input say plane 48   runway 7  when it prints it prints planes 1-48 which is what I want...then it places runways 7 at plane 7 not plane 48... not sure why.

The rest of the planes get 0 and then some planes get junk numbers


void displayrunwaystatus(void)
{
     int j;
     int h;
     int r;
     int k;
     int p;
     
     do{
     cout <<"Enter 1 to view available runways" <<endl;
     cout <<"Enter 2 to assign a plane to a runway" <<endl;
     cout <<"Enter 3 to view all plane status" <<endl;
     cout <<"Enter 0 to return to main menu" <<endl;
     cout <<endl;
     cout <<"Choice = ";
     cin  >>j;
     cout <<endl;
     
      if(j == 1)
     {
                 
          for(int i = 0; i < 7; i++)
          {
                 
                  if(runway_status[i] != runways[i])
                  {
                  cout <<left <<"Runways " <<setw(5) <<runways[i] <<" available" <<endl;
                  cout <<endl;
                  }
                  if(runway_status[i] == runways[i])
                  {
                  cout <<left <<"Runways " <<setw(5) <<runways[i] <<" in use" <<endl;
                  cout <<endl;
                  }
          }
          system("pause");
          cout <<endl;
     }
     if(j == 2)
     {
     cout <<"Enter a Plane number to assign a runway" <<endl;    

     cout <<endl;
   
     cout <<"Plane ";
     cin  >>p;
     p = p-1;
     cout <<"Runway ";
     cin  >>k;
     r = k-1;
     runway_status[r] = runways[r];
     if(k > 7)
     {
          cout <<endl;
          cout <<"There are only 7 runways" <<endl;
          cout <<endl;
          break;
     }
     
     
     cout <<airplanes[p] <<" as been assigned to Runway " <<runway_status[r] <<endl;

     
     system("pause");
     cout <<endl;
     }
     if(j == 3)
     {
          for(int z = 0; z < 48; z++)
          {
                 
                  cout <<airplanes[z] <<" Runways "<<runway_status[z] <<endl;
                 
          }
          cout <<endl;
          system("pause");
     
     }
                 
       }while(j != 0);
   
system("pause");
}
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of WestonGroup
WestonGroup

ASKER

that worked however

the output prints plane 1 -48 then runways now match to that specifc plane

the problem is plane 8-14 always get printed with garbage number  4473948 and I don't know why

all the others are printed with zeros except those

so it looks like this

plane 1   is on Runway 0
plane 2 is on Runway 0
plane 3 is on Runway 4  <----if I entered 4 for that plane
....
plane 8 is on Runway 4473948

not sure why it does this?


void displayrunwaystatus(void)
{
     int j;
     int h[7];
     int r;
     int k;
     int p;
     
     do{
     cout <<"Enter 1 to view available runways" <<endl;
     cout <<"Enter 2 to assign a plane to a runway" <<endl;
     cout <<"Enter 3 to view all plane status" <<endl;
     cout <<"Enter 0 to return to main menu" <<endl;
     cout <<endl;
     cout <<"Choice = ";
     cin  >>j;
     cout <<endl;
     
      if(j == 1)
     {
                 
          for(int i = 0; i < 7; i++)
          {
                 
                  if(h[i] != runways[i])
                  {
                  cout <<left <<"Runways " <<setw(5) <<runways[i] <<" available" <<endl;
                  cout <<endl;
                  }
                  if(h[i] == runways[i])
                  {
                  cout <<left <<"Runways " <<setw(5) <<runways[i] <<" in use" <<endl;
                  cout <<endl;
                  }
          }
          system("pause");
          cout <<endl;
     }
     if(j == 2)
     {
     cout <<"Enter a Plane number to assign a runway" <<endl;    

     cout <<endl;
   
     cout <<"Plane ";
     cin  >>p;
     p = p-1;
     cout <<"Runway ";
     cin  >>k;
     r = k-1;
     runway_status[p] = runways[r];
     h[r] = runways[r];
     if(k > 7)
     {
          cout <<endl;
          cout <<"There are only 7 runways" <<endl;
          cout <<endl;
          break;
     }
     
     
     cout <<airplanes[p] <<" as been assigned to Runway " <<runway_status[p] <<endl;

     
     system("pause");
     cout <<endl;
     }
     if(j == 3)
     {
          for(int z = 0; z < 48; z++)
          {
          cout <<left << setw(10) <<airplanes[z] <<" Runways "<<runway_status[z] <<endl;                  
          }
          cout <<endl;
          system("pause");
     
     }
                 
       }while(j != 0);
   
system("pause");
}
>>not sure why it does this?

For the planes where you haven't assigned a runway, it prints the default values, and you are plain lucky that it is 0 and not some rubbish, since you aren't initializing any values in the array. Think of '0' as 'unassigned to a runway' and skip those when printing, e.g.
     if(j == 3)
     {
          for(int z = 0; z < 48; z++)
          {
            if (runway_status[z] != 0) 
              cout <<left << setw(10) <<airplanes[z] <<" Runways "<<runway_status[z] <<endl;  
             else                
              cout <<left << setw(10) <<airplanes[z] <<" Runways "<<"<unassigned>" <<endl;  
          }
          cout <<endl;
          system("pause");
     
     }

Open in new window

figured it out, the array was too small

thanks for the help