Thread: how do I assign a char array to an int array?

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    7

    how do I assign a char array to an int array?

    What I would really like to do is enter a name(1). Just one name at a time. have it become part of a char array.Ex.
    Code:
     char passarray [10][30]
    but at the same time assign it a value in an int array. Ex.
    Code:
    int seatarray [6][2]
    I got a big hot mess here.....
    Code:
    #include <stdio.h>       // standard rules#include<string.h>
    #include <stdlib.h>
    
    
    void runmenu (void);
    void getnames (void);
    void getseat(void);
    
    
    void getemptylistseat(void);
    void getsortlastname(void);
    void getdeletenames(void);
    void printInstructions(void);
    int getOption(void);
    int option;
    int x,y;
    
    
    char passenger[10][30],temp[12];
    int array[6][2];
    
    
    int main (void)            //  program starts execution
    {
        printInstructions();
        runmenu();
    return 0;
    
    
    }
    void runmenu (void)
    {
        do{
            printf("\n Select your function, then enter the information requested.\n\n"); // option for functions
            
            printf("  1. Show the number of empty seats\n");
            printf("  2. Show a list of empty seats\n");
            printf("  3. Show by last name an alphabetical list of assigned seats\n");
            printf("  4. Assign a customer to a seat\n");
            printf("  5. Delete a seat assignment\n");
            printf("  6. QUIT\n");
            
            option = getOption();
    
    
            switch(option)
            {
                case 1:
                    getseat();
                    break;
                case 2:
                    getemptylistseat();
                    break;
                case 3: 
                    getsortlastname();
                    break;
                case 4:
                    getnames();
                    break;
                case 5:
                    getdeletenames();
                    break;
                case 6:
                    printf("Quitting, Goodbye\n\n\n");
                    break;
                default:
                    printf("Invalid entry");
            }
         }while(option != 6);
    
    
    }
    int getOption(void)
    {
        int option;
        scanf("%d", &option);
        system ( "cls" );
        return option;
    }
    void getdeletenames(void)
    {    
        
        printf("\n\n\nWhat passenger would you like to delete?\n\n\n");
         scanf("%s", passenger[x]);
        
    }
    void getseat(void)
    {
        printf("\n\n\n\this function is suposed to show number of empty seats %p.\n\n\n",array);
    
    
    }
    void getsortlastname(void)
    {
        printf("\n\n\nThis function is susposed to sort by last name and show assigned seat\n\n");
        for(x=0;x<9;x++)
        {
            for(y=0;y<9;y++)
                {
                if(strcmp(passenger[y],passenger[y+1])>0)
                    {
                    strcpy(temp,passenger[y]);
                    strcpy(passenger[y],passenger[y+1]);
                    strcpy(passenger[y+1],temp);
                    }
                }
        }
    printf("The names sorted by Surname are\n");
    for(x=0;x<10;x++)
    printf("\n%s is in seat %d",passenger[x],array);
        printf("\n\n");
    }
    void getnames (void)
    {
        getemptylistseat();
        printf("\n\nEnter the passengers name: \n\n example: Smith,Bob\n\n");
        for(x=0;x<1;x++)
        {
            scanf("%s",passenger[x]);
            printf("Enter Seat Number");
            scanf("%d",array);
    
    
        }
        printf("\n\n");
    }
    void getemptylistseat (void)
    {
        for ( x = 0; x < 2; x++ ) {
        for ( y = 0; y < 6; y++ )
                array[0][0] =1;/* Set each element to a value */
                array[0][1] =2;
                array[1][0] =3;
                array[1][1] =4;
                array[2][0] =5;
                array[2][1] =6;
                array[3][0] =7;
                array[3][1] =8;
                array[4][0] =9;
                array[4][1] =10;
                array[5][0] =11;
                array[5][1] =12;
        
      }
      printf( "Seats :\n" );
      for ( x = 0; x < 6;x++ ) {
        for ( y = 0; y < 2; y++ )
        {
            printf( "[%d][%d]  =  %d  ", x, y, array[x][y] );
        }
        printf( "\n" );
      }
      getchar();
    
    
    }
    void printInstructions (void)
    {
        printf("********************************************************************************");    
        printf("**     The Cape Fear Airlines has one plane with a seating capacity of 12.    **** It makes one flight daily.  This program that will allow the user to enter ****     a list of first and last names then reserve their seat accordingly.    **********************************************************************************");
    }

    Basically I want to take the first part and put it into the last part
    Code:
    void getnames (void){
        getemptylistseat();
        printf("\n\nEnter the passengers name: \n\n example: Smith,Bob\n\n");
        for(x=0;x<1;x++)
        {
            scanf("%s",passenger[x]);
            printf("Enter Seat Number");
            scanf("%d",array);
    
    
        }
        printf("\n\n");
    }
    void getemptylistseat (void)
    {
        for ( x = 0; x < 2; x++ ) {
        for ( y = 0; y < 6; y++ )
                array[0][0] =1;/* Set each element to a value */
                array[0][1] =2;
                array[1][0] =3;
                array[1][1] =4;
                array[2][0] =5;
                array[2][1] =6;
                array[3][0] =7;
                array[3][1] =8;
                array[4][0] =9;
                array[4][1] =10;
                array[5][0] =11;
                array[5][1] =12;
        
      }
      printf( "Seats :\n" );
      for ( x = 0; x < 6;x++ ) {
        for ( y = 0; y < 2; y++ )
        {
            printf( "[%d][%d]  =  %d  ", x, y, array[x][y] );
        }
        printf( "\n" );
      }
      getchar();

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
        for ( x = 0; x < 2; x++ ) {
        for ( y = 0; y < 6; y++ )
                array[0][0] =1;/* Set each element to a value */
                array[0][1] =2;
                array[1][0] =3;
                array[1][1] =4;
                array[2][0] =5;
                array[2][1] =6;
                array[3][0] =7;
                array[3][1] =8;
                array[4][0] =9;
                array[4][1] =10;
                array[5][0] =11;
                array[5][1] =12;
    A few points
    1. You need to move this 'idea' into a separate function called initialiseEmptySeatList.
    2. You need a unique value to indicate an empty seat.
    3. Use the loop to cut down on the amount of code.
    Code:
    void initialiseEmptySeatList ( ) {
        for ( x = 0; x < 2; x++ )
            for ( y = 0; y < 6; y++ )
                array[y][x] = -1;
    }
    So when you get to doing this -> "4. Assign a customer to a seat", you will have some code like this:

    Code:
    // a passenger number, which is an index into your passenger array
    int pNum = 3;
    
    // a position in your seat array
    int row = 4;
    int col = 0;
    
    // assign the seat
    if ( seat[row][col] == -1 ) {
        printf("Occupied!\n");
    } else {
        // assign passenger to a seat
        seat[row][col] = pNum;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    7
    it sounds simple enough....

    so can I "initialize a pNum" in the same manner?

  4. #4
    Registered User
    Join Date
    Dec 2012
    Posts
    7
    OK I cut this down to be more manageable..
    Im out a gas...

    I just dont know how to assign a char name a value so I can link it to a seat... everything Ive tried blows up. this is the closest ive been.

    thanx in advance for the help



    Code:
    #include <stdio.h> 	  // standard rules#include<string.h>
    #include <stdlib.h>
    void getemptylistseat(void);
    int x,y;
    int pNum;
    
    
    char passenger[12][30],temp[13];
    int array[6][2];
    
    
    int main (void)
    
    
    {
    
    
    	getemptylistseat();
    	
    	printf("\n\nEnter the passengers name: \n\n example: Smith,Bob\n\n");
    	for(x=0;x<1;x++)
    	{
    			scanf("s%",passenger[x]);
    				passenger[x] = pNum;
    		}							
    
    
    		{printf("\nEnter seat number:  example [4][1]\n");
    
    
    			scanf("%d, %d",array[x],array[y]);
    		}
    	return 0;
    }
    void getemptylistseat (void)
    {
    	for ( x = 0; x < 2; x++ ){
        for ( y = 0; y < 6; y++ )
                array[y][x] = pNum;
      }
      printf( "Seats :\n" );
      for ( x = 0; x < 6;x++ ) {
        for ( y = 0; y < 2; y++ )
        {
            printf( "[%d][%d]  =  %d  ", x, y, array[x][y] );
        }
        printf( "\n" );
      }
     // getchar();
    
    
    }

  5. #5
    Registered User
    Join Date
    Dec 2012
    Posts
    7
    crap!! that isn't the write one I forgot to add the if/else statements....

  6. #6
    Registered User
    Join Date
    Dec 2012
    Posts
    3
    where are the comments and the purpose? Maybe a structure is what you are looking for.

    Structures in C - Tutorial - Cprogramming.com

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > scanf("s%",passenger[x]);
    > passenger[x] = pNum;
    Did you even compile this before posting it here?

    These kinds of trivial regressive steps make it hard to believe you're really thinking about what you're doing.

    One last shove in the right direction - let's see if you can complete the two "// read input comments"
    Code:
    #include <stdio.h>    // standard rules
    #include<string.h>
    #include <stdlib.h>
    
    #define MAX_PASSENGERS  12
    #define MAX_NAME_LENGTH 30
    #define SEAT_ROWS       6
    #define SEAT_COLS       2
    #define FREE_SEAT       -1
    
    void initialiseEmptySeatList ( int seats[SEAT_ROWS][SEAT_COLS] ) {
      int x, y;
      for ( y = 0; y < SEAT_ROWS; y++ ) {
        for ( x = 0; x < SEAT_COLS; x++ ) {
          seats[y][x] = FREE_SEAT;
        }
      }
      // fake some occupied seats for testing
      seats[0][0] = 42;
      seats[1][1] = 42;
    }
    
    void getemptylistseat ( int seats[SEAT_ROWS][SEAT_COLS] ) {
      int x, y;
      for ( y = 0; y < SEAT_ROWS; y++ ) {
        for ( x = 0; x < SEAT_COLS; x++ ) {
          if ( seats[y][x] == FREE_SEAT ) {
            // this seat is free
            printf("[%d][%d]",y,x);
          }
          printf("\n");
        }
      }
    }
    
    int main ( ) {
      int pNum;
      char passengers[MAX_PASSENGERS][MAX_NAME_LENGTH] = { { 0 } };
      int seats[SEAT_ROWS][SEAT_COLS] = { { 0 } };
    
      initialiseEmptySeatList(seats);
      for ( pNum = 0 ; pNum < MAX_PASSENGERS ; pNum++ ) {
        getemptylistseat(seats);
        printf("Enter Name ");
        char name[MAX_NAME_LENGTH];
        // read input
        printf("Enter seat position");
        int x,y;
        // read input
        if ( seats[y][x] != FREE_SEAT ) {
          printf("[%d][%d] is occupied\n", y, x );
        }
      }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Dec 2012
    Posts
    7
    no.... probably not. I was working with two or there different ones....one with if else statements and another that didn't work at all...not to mention it was wicked early in the a.m.

    With the load of garage I got now... I'm one point away from an "A" for the class. I've spent another 8 hours pouring over the net trying to find something that doesn't read like VCR instructions. I've asked my instructor more than twice how to link the
    Code:
    char array
    to the
    Code:
    int array
    .

    Then we have the strict homework policy here....which I believe in 100%

    I just can't see it....I need to find someone to hold my hand and walk me through.

    The semester will be over with in 45mins. So, I'm gonna take the hit and find the answer tomorrow.

    Thanx for for your time.
    Sorry to regress and/or be trivial....but your absolutely right I can't think anymore. Sort can't see the forest because of the trees kinda thing. You don't wanna know how many times I've changed my plan of attack for this problem.

    anyway, enough drival... Thanx for your time again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-03-2012, 01:23 AM
  2. Replies: 2
    Last Post: 03-20-2012, 08:41 AM
  3. Replies: 3
    Last Post: 11-17-2008, 12:36 PM
  4. Trying to assign array values to another array
    By edhc44 in forum C++ Programming
    Replies: 10
    Last Post: 02-01-2008, 10:26 PM
  5. Assign value of multiple char array elements to int
    By 3saul in forum C Programming
    Replies: 3
    Last Post: 02-03-2006, 05:31 AM