Thread: C Newbie Hitting Trouble

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    3

    C Newbie Hitting Trouble

    Hey, I am trying to make a program about a food bank that can record donations, record requests, fulfill requests[permitted the donations have enough to], and show current donations/unfulfilled requests. I can't seem to have the requests be added without crashing. Any help is appreciated =D!

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main()
    {
        int i, j, index, ch, found, don_quant, don_amt[100], don_count, req_amt[100], req_count, req_quant;
        char word [20], don_inv_type[100][20], req_inv_type[100][20];
        don_count = 0;
        req_count = 0;
        printf(" 1. Add a Donation \n 2. Add a request \n 3. Fulfull a request \n 4. Print status \n 5. Exit \n");
        scanf("%d", &ch);
        while(ch !=5){ 
          if(ch == 1){
                printf("Enter inventory type: ");
                scanf("%s", &word);
                printf("Enter amount: ");
                scanf("%d", don_quant);
                found = -99;
                for(i=0; i<don_count; i++){
                         if(strcmp(don_inv_type[i], word) == 0)
                         found = i;
                         }
                if(found == -99){
                         strcpy(don_inv_type[i], word);
                         don_amt[i] = don_quant;
                         don_count++;
                         }
                else
                         don_amt[found] += don_quant;
                         }
          else if(ch == 2){
                           printf("Enter inventory type: ");
                            scanf("%s", &word);
                            printf("Enter amount: ");
                            scanf("%d", req_quant);
                            strcpy(req_inv_type[req_count], word);
                            req_amt[req_count] = req_quant;
                            req_count++;
                        }
          else if(ch == 4){
                            printf("Printing Donation table: \n \n");
                            for(i=0; i<don_count; i++)
                                printf("%s %d \n", don_inv_type[i], don_amt[i]);
                            printf("Printing Request table: \n \n");
                            for(i=0; i<req_count; i++)
                                printf("%s %d \n", req_inv_type[i], req_amt[i]);
                         }
          printf(" 1. Add a Donation \n 2. Add a request \n 3. Fulfull a request \n 4. Print status \n 5. Exit \n");
          scanf("%d", &ch);   
                }
        system("PAUSE");
        return 0;
    }
    Edit: Also, the donation amount[don_amt] for each type becomes a huge number =/. The 3rd option is deliberately missing; skipped it.
    Edit: Initial problem resolved; will keep open in case I hit a new issue with the program, thanks!
    Last edited by kevin13; 10-16-2012 at 06:39 PM. Reason: Add. Information

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Just having a quick glance, I saw this -
    Code:
    printf("Enter inventory type: ");
                scanf("%s", word);                  //removed &
                printf("Enter amount: ");
                scanf("%d", &don_quant);            //added &
                found = -99;
    This should put you on the right track.
    Fact - Beethoven wrote his first symphony in C

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    3
    Quote Originally Posted by Click_here View Post
    Just having a quick glance, I saw this -
    Code:
    printf("Enter inventory type: ");
                scanf("%s", word);                  //removed &
                printf("Enter amount: ");
                scanf("%d", &don_quant);            //added &
                found = -99;
    This should put you on the right track.
    Thank you! Sorry the answer was so simple; still getting used to debugging xD.

    If I run into further problems or get stuck with the fulfilling requests, I'll update again.

  4. #4
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    It seems that you wrote all your code in one go and it didn't work.

    Everyone makes mistakes left right and centre when typing a program out - The trick is to do a lot of testing as you are writing the code.

    If I were you, I'd have started with the menu -> Prompting for input, and then having a "You have selected blah-blah" output for each entry - Test it over and over, make sure it works great. I then would have gone to the first option and concentrated on that small section (Putting in temporary printf's - testing it lots before I moved on). I then would continue with this pattern until the program does everything I want - I might even think of more things as I'm going along.
    Fact - Beethoven wrote his first symphony in C

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    3
    Hey, more on said program, I want to remove requests/donations as they are fulfilled/depleted. I plan to run a for loop as so:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main()
    {
        int i, j, x, index, ch, found, don_quant, don_amt[100], don_count, req_amt[100], req_count, req_quant;
        char word [20], don_inv_type[100][20], req_inv_type[100][20];
        don_count = 0;
        req_count = 0;
        printf(" 1. Add a Donation \n 2. Add a request \n 3. Fulfull a request \n 4. Print status \n 5. Exit \n");
        scanf("%d", &ch);
        while(ch !=5){ 
          if(ch == 1){
                printf("Enter inventory type: ");
                scanf("%s", word);
                printf("Enter amount: ");
                scanf("%d", &don_quant);
                found = -99;
                for(i=0; i<don_count; i++){
                         if(strcmp(don_inv_type[i], word) == 0)
                            found = i;
                         }
                if(found == -99){
                         strcpy(don_inv_type[i], word);
                         don_amt[i] = don_quant;
                         don_count++;
                         }
                else
                         don_amt[found] += don_quant;
                         }
          else if(ch == 2){
               printf("Enter inventory type: ");
               scanf("%s", word);
               printf("Enter amount: ");
               scanf("%d", &req_quant);
               strcpy(req_inv_type[req_count], word);
               req_amt[req_count] = req_quant;
               req_count++;
                        }
          else if(ch ==3){
               found = -99;
               for(index=0; index<don_count; index++){
                         if(strcmp(don_inv_type[index], req_inv_type[0]) == 0)
                         found = index;
                          }
               if(found==-99)
                    printf("\n-----Request Cannot Be Fulfilled-----\n");
               else if(don_amt[found] > req_amt[0]){
                    don_amt[found]-=req_amt[0];
                    printf("\n-----Request Fulfilled-----\n");
                    req_amt[0]=0;
                    for (x=found; x<don_count; x++)
                        strcpy(don_inv_type[x], don_inv_type[x+1]);
                    don_inv_type[don_count] = 0;
                    don_count--;
                    }
               else if(don_amt[found]==req_amt[0]){
                    don_amt[found]-=req_amt[0];
                    printf("\n-----Request Fulfilled-----\n");
                    req_amt[0]=0;
                    for (x=found; x<don_count; x++)
                        strcpy(don_inv_type[x], don_inv_type[x+1]);
                    don_inv_type[don_count] = 0;
                    don_count--;
                    for (x=0; x<req_count; x++)
                        strcpy(req_inv_type[x], req_inv_type[x+1]);
                    req_inv_type[req_count] = 0;
                    req_count--;
                    }
               else{
                    req_amt[0]-=don_amt[found];
                    printf("\n-----Request Partially Fulfilled-----\n");
                    don_amt[found]=0;
                    for (x=0; x<req_count; x++)
                        strcpy(req_inv_type[x], req_inv_type[x+1]);
                    req_inv_type[req_count] = 0;
                    req_count--;
                    }
               }
          else if(ch == 4){
                            printf("\nPrinting Donation table: \n \n");
                            for(i=0; i<don_count; i++)
                                printf("%s %d \n", don_inv_type[i], don_amt[i]);
                            printf("\nPrinting Request table: \n \n");
                            for(i=0; i<req_count; i++)
                                printf("%s %d \n", req_inv_type[i], req_amt[i]);
                         }
          printf("\n 1. Add a Donation \n 2. Add a request \n 3. Fulfull a request \n 4. Print status \n 5. Exit \n");
          scanf("%d", &ch);   
                }
        printf("\n\nThank You For Using This Program.\nHave A Nice Day!\n");
        system("PAUSE");
        return 0;
    }
    I am not certain if I'm on the right track, and, if I am, how do I remove the last row in each array, rather than having the last row a repeat of the one before it? Thank you again Click_Here, and to anyone else who can offer any advice =).

    P.S. Checked and everything besides the red is working as I was going for.

    Edit: Aware that the amounts haven't been bumped; stopped before I wrote that code to make sure there weren't two issues happening at the same time.
    Last edited by kevin13; 10-16-2012 at 09:40 PM. Reason: Clarifying

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    A development process
    Start splitting the code up into functions, so your main looks like
    Code:
    if ( ch == 1 ) {
        doAddDonation();
    } else if ( ch == 2 ) {
        doInventory();
    }
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie function trouble
    By Swerve in forum C++ Programming
    Replies: 6
    Last Post: 03-04-2008, 04:33 AM
  2. Newbie- having trouble with functions
    By bnmwad in forum C Programming
    Replies: 7
    Last Post: 02-22-2005, 04:41 PM
  3. anjuta [newbie] trouble
    By darksaidin in forum Linux Programming
    Replies: 20
    Last Post: 12-22-2003, 05:22 PM
  4. Replies: 1
    Last Post: 09-15-2002, 04:56 AM
  5. Newbie with trouble
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 10-19-2001, 08:04 PM

Tags for this Thread