Thread: C program help!

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    1

    C program help!

    Hey guys! I'm new to all of this and i was hoping that someone would be able to assist me with an issue i have with this program.




    Code:
    #include <stdio.h>
    main()
    {
        int ctr;
        int idSearch;
        int found = 0;
    
    
        int custID[10] = {313, 453, 502, 101, 892, 475, 792, 912, 343, 633 };
        float custBal[10] = { 0.00, 45.43, 71.23, 301.56, 9.08, 192.41, 389.00, 229.67, 18.31, 59.54};
        int tempID, inner, outer, didSwap;
        float tempBal;
    
    
        for (outer = 0; outer < 9; outer++)
        {
            didSwap = 0;
            for(inner = outer; inner < 10; inner++)
            {
                if (custID[inner] < custID[outer])
                {
                    tempID = custID[inner];
                    tempBal = custID[inner];
                    custID[inner] = custID[outer];
                    custBal[inner] = custBal[outer];
                    custID[outer] = tempID;
                    custBal[outer] = tempBal;
                    didSwap = 1;
                }
            }
            if (didSwap == 0)
            {
                break;
            }
        }
        printf("\n\n*** Cutomer Balance Lookup ***\n");
        printf("What is the customer number? ");
        scanf(" %d, &idSearch");
        for (ctr=0; ctr<10; ctr++)
        {
            if (idSearch == custID[ctr])
            {
                found = 1;
                break;
            }
            if (custID[ctr] > idSearch)
            {
                break;
            }
        }
    
    
        if (found)
        {
            if (custBal[ctr] > 100.00)
            {
                printf("\n*** That customer's nBalance is $%.2f.\n", custBal[ctr]);
                printf("   No credit!\n");
            }
            else
            {
                printf("\n**The customer's credit is ggood!\n");
            }
        }
        else
        {
            printf("**You must have typed an incorrect cusomer ID.");
            printf("\n ID %3d was not found in list. \n", idSearch);
        }
        return 0;
    }


    I'm currently using code blocks for this and when i run this program there is absolutely no syntax errors. However, the moment you type something in and hit enter the program stops and a screen comes up saying that there is something wrong with the program and then it dies. It's really sad but i would really appreciate it if someone could give me some advice on what could possibly be the problem. Thank you!

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I suggest you turn up the warning level of your compiler - this is what I got:

    Code:
    main.c|3|warning: return type defaults to 'int'|
    main.c||In function 'main':|
    main.c|38|warning: too few arguments for format|
    main.c|41|warning: 'idSearch' may be used uninitialized in this function|
    ||=== Build finished: 0 errors, 3 warnings ===|
    Does this help you narrow down the cause of the problem?

  3. #3
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    I think Matticus's suggest will help you find the error so I'll make an observation instead

    I"m glad you break out of the bubblesort early if no swaps have occurred. There is another improvement to the bubblesort you can make as well: for example, consider what the value of custID[9] is after the first iteration of the loop and how it compares to custID[8]...

    Edit: I decided my clue was a bit vague, so adding some additional "clues". In your current code the inner loop of your bubblesort will in the worst case scenario execute 90 times. You can improve this so it in the worst case it will execute only 63 times.
    Last edited by SirPrattlepod; 08-22-2013 at 06:38 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-11-2012, 12:25 AM
  2. Replies: 1
    Last Post: 03-03-2009, 04:47 PM
  3. Replies: 5
    Last Post: 08-16-2007, 11:43 PM
  4. Replies: 18
    Last Post: 11-13-2006, 01:11 PM
  5. How To Make The Program Installed In Program Files Folder?
    By javacvb in forum Windows Programming
    Replies: 4
    Last Post: 11-05-2003, 05:33 PM