Thread: Array Help

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    13

    Array Help

    hey new to programming i know this code isnt anywhere near done but just seeing if someone could help with why it keeps crashing right away.

    Code:
    #include<stdio.h>
    #include<string.h>
    
    
    
    
    #define MAXNAME 50
    #define MAXDEPT 50
    
    
    int main() {
    
    
        int money[MAXNAME][MAXDEPT];
        int numNames, numDept, i, j;
        char name, dept;
    
    
    
    
    
    
        scanf("%d", &numNames); //read number of executives
            for(i=0;i<numNames;i++){
                scanf("%s", name); // scan for names of executives.
    
    
        scanf("%d", &numDept); // read number of departments.
            for(j=0;j<numDept;j++){
                scanf("%s", dept);// scan for names of deptment.
    
    
                }
            }
            scanf("%d",&money[numNames][numDept]);
    return 0;
    
    
    }

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    your variable 'name' (and 'dept') only has room for 1 character. you need to make it an array of char with a size sufficient for the data you plan to enter

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    13
    Code:
     
     int money[MAXNAME][MAXDEPT];    int numNames, numDept, i, j;
        char name[MAXLEN+1];
        char dept[MAXLEN+1];
    
    
    
    
    
    
        scanf("%d", &numNames); //read number of executives
            for(i=0;i<numNames;i++){
                scanf("%s", name); // scan for names of executives.
    
    
        scanf("%d", &numDept); // read number of departments.
            for(j=0;j<numDept;j++){
                scanf("%s", dept);// scan for names of deptment.
    
    
                scanf("%d", &money[i][j]); // here i need to scan for an amount of money taken by each person from each department. but when i run it only lets me scan for the first person.
                }
            }
    so it only lets me scan for the amount taken from each departemnt by that one person how to do i get it to loop and scan for all the executives.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Try putting a leading space in the scanf format string, for example " %s".
    It would also help if you prompt the user with a executive number and department number so that they know which they are entering.

  5. #5
    Registered User
    Join Date
    Feb 2013
    Posts
    13
    Code:
     int money[MAXNAME][MAXDEPT];
        int numNames, numDept, i, j;
        char name[MAXLEN+1];
        char dept[MAXLEN+1];
        char exec[MAXLEN+1];
    
    
        scanf("%d", &numNames); //read number of executives
            for(i=0;i<numNames;i++){
                scanf("%s", name); // scan for names of executives.
    
    
    
    
    
    
        scanf("%d", &numDept); // read number of departments.
            for(j=0;j<numDept;j++){
                scanf("%s", dept);// scan for names of deptment.
    
    
                }
            }
            for(i=0; i<numNames; i++){
                for(j=0; j<numDept; j++){
                    scanf("%d", &money[i][j]); // money embezzled assigned to each dept and name
            }
        }
    
    
    
    
        printf("Mr.%s has embezzled $%d.\n", name, money[i][j]);
    
    
    
    
    
    
    return 0;

    at the end i am trying to get it to print the two last names along with how much total money they have embezzled. can someone help me.

  6. #6
    Registered User
    Join Date
    Mar 2013
    Location
    Albuquerque, NM USA
    Posts
    5
    My suggestions would be:
    1) Don't use quite so many blank lines between groups of statements (enhances readability).
    2) Precede each scanf() with a printf() to prompt the user. (This was suggested by nonoob already).
    3) Consider using file I/O instead of relying on the stdin/stdout too much. You're likely to go that way anyway.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-20-2012, 08:41 AM
  2. Replies: 9
    Last Post: 08-23-2010, 02:31 PM
  3. Replies: 1
    Last Post: 10-21-2007, 07:44 AM
  4. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  5. Replies: 1
    Last Post: 04-25-2006, 12:14 AM