initializing values for arrays, and chaging them w/o using scanf

This is a discussion on initializing values for arrays, and chaging them w/o using scanf within the C Programming forums, part of the General Programming Boards category; hey guys!! just discovered this site! It's great! went over the threads..very helpful to guys like us.. anyway, i got ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    11

    Post initializing values for arrays, and chaging them w/o using scanf

    hey guys!! just discovered this site! It's great! went over the threads..very helpful to guys like us..
    anyway, i got this program that needs to simulate a simple airline seat reservation. I can initialize values for my text arrays but cant modify or change them without using scanf(). Thanks!!

    -----------------------------------------
    #include <conio.h>
    #include <stdio.h>
    char a[7][7]={"A","A","A","A","A","A","A"},b[7][7]={"B","B","B","B","B","B","B",},c[7][7]={"C","C","C","C","C","C","C"},d[7][7]={"D","D","D","D","D","D","D",};
    int x;char y;
    main()
    {
    do{
    clrscr();
    printf("\n1\t%s\t%s\t%s\t%s\n\n",a[0],b[0],c[0],d[0]);
    printf("2\t%s\t%s\t%s\t%s\n\n",a[1],b[1],c[1],d[1]);
    printf("3\t%s\t%s\t%s\t%s\n\n",a[2],b[2],c[2],d[2]);
    printf("4\t%s\t%s\t%s\t%s\n\n",a[3],b[3],c[3],d[3]);
    printf("5\t%s\t%s\t%s\t%s\n\n",a[4],b[4],c[4],d[4]);
    printf("6\t%s\t%s\t%s\t%s\n\n",a[5],b[5],c[5],d[5]);
    printf("7\t%s\t%s\t%s\t%s\n\n",a[6],b[6],c[6],d[6]);
    printf("Column Number : ");
    scanf("%d",&x);
    printf("Enter Column Letter : ");
    scanf("%s",y);
    if(y=='a')
    a[x][x]='X';
    getch();
    }while(x!=100);
    }

  2. #2
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    char a[7][7]={"A","A","A","A","A","A","A"}
    Wrong, I don't understand your idea, but you initalized this wrong.

    printf("\n1\t%s\t%s\t%s\t%s\n\n",a[0],b[0],c[0],d[0]);
    Well, youre trying to print a char or a string? if you're trying to print a whole string (array of char terminated by null) you should remove all the [0] from the names, just stay, a,b,c,d. and if you're trying to print a single char, change all the %s to %c

    scanf("%s",y);
    Wrong 'y' is a char, and not an array of char, change to %c, and don't forget the & operator.
    Last edited by Vber; 03-15-2003 at 08:21 AM.

  3. #3
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Also, instead all this printfs, you can use a loop...
    Last edited by Vber; 03-15-2003 at 08:29 AM.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>Wrong, I don't understand your idea, but you initalized this wrong.
    No, they didn't.

    >>you should remove all the [0] from the names, just stay, a,b,c,d.
    No, they shouldn't

    The variables a, b, c and d are all 2d arrays, so they are declared correctly, and printed correctly.

    This bit doesn't look quite right:
    >>if (y == 'a') a[x][x] = 'X';
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Well, but to make this program he shouldn't do with 2D array of strings, I showed him on PM how to do this with a simple array of char, after I understood the rules.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>shouldn't do with 2D array of strings, // with a simple array of char
    Why? How about if they wanted to have something meaningful in the array, like "reserved", "free", "taken", "window seat". Anyway, that's a lesson for tomorrow for the OP.. so we'll leave it there.

    Another problem:
    >>scanf("%d",&x);
    You'll need to flush the input buffer after issuing that call.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Hammer ask him the rules, to understand why he shouldn't do this with 2D arrays...

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Vber
    Hammer ask him the rules, to understand why he shouldn't do this with 2D arrays...
    I understand where you're coming from Maybe sly should post again with more information...
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Registered User
    Join Date
    Mar 2003
    Posts
    11
    hey guys! im back! seems we have a bit of confusion. well actually its my fault cause i did not state clearly the problem. well the program im making is for my finals case study in our university. our professor said that in making the program we should use functions and arrays, either 2d or single. the problem is im quite confused when it comes to arrays..i was taught by him that in declaring a char array there must be two elements..for example :
    Code:
    char text[5][30];
    The first one is the number of elements then the next one is the maximum number of characters. is that right? About the rules, our prof said we should stick to the rules. very strict when it comes to that. and i should be ready to defend that if ever..so i hope you guys wouldnt give me those advanced syntax cause im just a newbie in C programming...

  10. #10
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    >> The first one is the number of elements then the next one is the maximum number of characters. is that right?

    If you want to declare a char array, then this all you need :
    Code:
    // Declare a char array which has a capacity of 30 elements
    char text[30];
    The declaration you gave was one of a two-dimensional array.
    Code:
    // Declare a two-dimensional
    // Number of one-dimensional char arrays : 5
    // Capacity of each char array : 30
    char text[5][30];
    Last edited by The Dog; 03-16-2003 at 05:35 AM.

  11. #11
    Registered User
    Join Date
    Mar 2003
    Posts
    11

    Talking

    wheeee!! i guess i did it this time!!
    i got it now! the program actually works now! thanks for the help guys, below is the code..you are all welcome to comment on how to improve it well. anyway it still lacks the screen design it needs...so im off to improve is looks..

    Code:
    #include <conio.h>
    #include <stdio.h>
    #include <ctype.h>
    main()
    {
    char a[8][2]={"A","A","A","A","A","A","A","A"},b[8][2]={"B","B","B","B","B","B","B","B"},c[8][2]={"C","C","C","C","C","C","C","C"},d[8][2]={"D","D","D","D","D","D","D","D",};
    char y;
    int x;
    do{
    clrscr();
    printf("\n1\t%s\t%s\t%s\t%s\n\n",a[1],b[1],c[1],d[1]);
    printf("2\t%s\t%s\t%s\t%s\n\n",a[2],b[2],c[2],d[2]);
    printf("3\t%s\t%s\t%s\t%s\n\n",a[3],b[3],c[3],d[3]);
    printf("4\t%s\t%s\t%s\t%s\n\n",a[4],b[4],c[4],d[4]);
    printf("5\t%s\t%s\t%s\t%s\n\n",a[5],b[5],c[5],d[5]);
    printf("6\t%s\t%s\t%s\t%s\n\n",a[6],b[6],c[6],d[6]);
    printf("7\t%s\t%s\t%s\t%s\n\n",a[7],b[7],c[7],d[7]);
    do{
    gotoxy(1,17);
    clreol();
    printf("Column Number (1-7) : ");
    scanf("%d",&x);
    }while(x!=1&&x!=2&&x!=3&&x!=4&&x!=5&&x!=6&&x!=7);
    do{
    gotoxy(1,18);
    clreol();
    printf("Enter Column Letter : ");
    scanf("%s",&y);
    y=toupper(y);
    }while(y!='A'&&y!='B'&&y!='C'&&y!='D');
    if(y=='A')
     {
      if(a[x][0]=='X')
      printf("Sorry, the seat is already Reserved. Please select another.");
     a[x][0]='X';
     }
    if(y=='B')
     {
     if(b[x][0]=='X')
      printf("Sorry, the seat is already Reserved. Please select another.");
     b[x][0]='X';
     }
    if(y=='C')
     {
     if(c[x][0]=='X')
     printf("Sorry, the seat is already Reserved. Please select another.");
     c[x][0]='X';
     }
    if(y=='D')
     {
     if(d[x][0]=='X')
     printf("Sorry, the seat is already Reserved. Please select another.");
     d[x][0]='X';
     }
    getch();}while(x!=100);
    }

  12. #12
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    >>main()
    Main returns int always, so change this to int main() and then return an value at the end of the main function.

    printf("\n1\t%s\t%s\t%s\t%s\n\n",a[1],b[1],c[1],d[1]);
    printf("2\t%s\t%s\t%s\t%s\n\n",a[2],b[2],c[2],d[2]);
    printf("3\t%s\t%s\t%s\t%s\n\n",a[3],b[3],c[3],d[3]);
    printf("4\t%s\t%s\t%s\t%s\n\n",a[4],b[4],c[4],d[4]);
    printf("5\t%s\t%s\t%s\t%s\n\n",a[5],b[5],c[5],d[5]);
    printf("6\t%s\t%s\t%s\t%s\n\n",a[6],b[6],c[6],d[6]);
    printf("7\t%s\t%s\t%s\t%s\n\n",a[7],b[7],c[7],d[7]);
    Could be changed with:
    Code:
    for (i=1; i<=7; ++i) 
    	printf("%d\t%s\t%s\t%s\t%s\n",i,a[i],b[i],c[i],d[i]);
    scanf("%s",&y);
    Y is a char and not an array of chars (string) so change this to:
    Code:
    scanf("%c",&y);
    Last edited by Vber; 03-16-2003 at 09:23 AM.

  13. #13
    Registered User
    Join Date
    Mar 2003
    Posts
    11
    i did try the looping thing on the printf..gets a little messed up when i add the screen design using ascii stuff...i guess im off to the long way...anyway thanks!

  14. #14
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Well, what gets messed? what you mean? maybe because a forgot to add another \n at my for loop printf().

  15. #15
    Registered User
    Join Date
    Mar 2003
    Posts
    11

    Smile

    it gets messed up when i add the needed screen design..oh its not because of the "\n" you forgot to place. i tried the loop before but its no use because i use the gotoxy() and gets really messy when i add the ASCII codes to make borders and stuff..

Popular pages Recent additions subscribe to a feed

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