Thread: Input help needed

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    40

    Input help needed

    ok i'm trying to input the following input into my program:

    Code:
    4 6
    Ryan 4
    Peter 5
    Harold 2
    John 9
    Carlos 1
    Terry 7
    end
    the first two are for this array but the problem is i dont know how to input "Ryan" "Peter" and stuff by having the first letter of the name stored in an array[1] and the number afterwards stored in array2[1]. It should stop scanning in numbers once the user types in "end". Here's the code i have right now but its a lil funky and does not work:

    Code:
    #include <stdio.h>
    
    main()
    {
         char board[30][30];
         int b_rows, b_cols;
         char gem[27];
         int gem_value[27];
         char c;
         int rows, cols, num=0;
    
         scanf("%d%d", &b_rows, &b_cols);
         printf("Rows = %d Cols = %d\n", b_rows, b_cols);
    
         for(rows=0; rows<b_rows; rows++)
           for(cols=0; cols<b_cols; cols++)
             board[rows][cols] = '.';
    
    rows=0;
    while(rows<27)
      {
          scanf("%c", &gem[rows]);
          if(gem[rows] != 'e')
            scanf("%d", &gem_value[rows]);
          rows++;
      }
    
         for(rows=0; rows<27; rows++)
           printf("Gem Name: %c Gem Value: %d\n", gem[rows], gem_value[rows]);
    and here's the output with the input from above:

    Code:
    4 6
    Rows = 4 Cols = 6
    Ryan 4
    Peter 5
    Harold 2
    John 9
    Carlos 1
    Gem Name:
     Gem Value: -688123
    Gem Name: R Gem Value: -688123
    Gem Name: y Gem Value: -688123
    Gem Name: a Gem Value: -688123
    Gem Name: n Gem Value: 4
    Gem Name:
     Gem Value: -688123
    Gem Name: P Gem Value: -688123
    Gem Name: e Gem Value: -688123
    Gem Name: t Gem Value: -688123
    Gem Name: e Gem Value: -688123
    Gem Name: r Gem Value: 5
    Gem Name:
     Gem Value: -688123
    Gem Name: H Gem Value: -688123
    Gem Name: a Gem Value: -688123
    Gem Name: r Gem Value: -688123
    Gem Name: o Gem Value: -688123
    Gem Name: l Gem Value: -688123
    Gem Name: d Gem Value: 2
    Gem Name:
     Gem Value: -688123
    Gem Name: J Gem Value: -688123
    Gem Name: o Gem Value: -688123
    Gem Name: h Gem Value: -688123
    Gem Name: n Gem Value: 9
    Gem Name:
     Gem Value: -688123
    Gem Name: C Gem Value: -688123
    Gem Name: a Gem Value: -688123
    Gem Name: r Gem Value: -688123
    can anyone give me some hints or tips on how to make this work? any help would be much appreciated thanks!

  2. #2
    Registered User
    Join Date
    Oct 2002
    Posts
    40
    is there a way to store just the first letter of the name in the first array and then the number in the second without using strings? Again i only need to store the first letter in the array not the whole name. Thanks

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by blindleaf
    is there a way to store just the first letter of the name in the first array and then the number in the second without using strings? Again i only need to store the first letter in the array not the whole name. Thanks
    As you need to check the input against the word "end", you'd probably be best of by reading into a temporary array, doing that check, then assigning the first element of the input array to the store array.

    >>array[0] = input[0];
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    40
    ahhh i still can't figure this out here's what i have now, but its basically doing the same thing can anyone help?

    Code:
    main()
    {
         char board[30][30];
         int b_rows, b_cols;
         char gem[27], temp[27];
         int gem_value[27];
         char c;
         int rows, cols, num=0, index=0;;
    
         scanf("%d%d", &b_rows, &b_cols);
         printf("Rows = %d Cols = %d\n", b_rows, b_cols);
    
         for(rows=0; rows<b_rows; rows++)
           for(cols=0; cols<b_cols; cols++)
             board[rows][cols] = '.';
    
    rows=0;
    while(num == 0)
      {
        scanf("%c%d", &gem[rows], &gem_value[rows]);
        for(index=rows; index<rows+4; index++)
          temp[index] = gem[index];
        if(temp[rows] == 'e')
            num = 1;
        else
            gem[rows] = temp[rows];
        rows++;
      }
    
    for(rows=0; rows<27; rows++)
           printf("Gem Name: %c Gem Value: %d\n", gem[rows], gem_value[rows]);

  5. #5
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    index = 0;

    loop
    {
    fgets(temp, sizeof(temp), stdin);
    /* other operations */
    gem[index++] = temp[0];
    }


    Don't use a counter to copy each element of temp to gem or you'll just be doing the same thing as before. If I understand what you're asking correctly.
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    40
    i can't use fgets

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf in different functions for the same file
    By bchan90 in forum C Programming
    Replies: 5
    Last Post: 12-03-2008, 09:31 PM
  2. Printing Length of Input and the Limited Input
    By dnguyen1022 in forum C Programming
    Replies: 33
    Last Post: 11-29-2008, 04:13 PM
  3. Trouble with a lab
    By michael- in forum C Programming
    Replies: 18
    Last Post: 12-06-2005, 11:28 PM
  4. Help needed for validing input
    By cc870 in forum C Programming
    Replies: 2
    Last Post: 08-20-2005, 04:36 AM
  5. input validation - new to programming
    By bigzeppelin2k in forum C Programming
    Replies: 2
    Last Post: 10-31-2003, 06:44 PM