Problems with Calculating

This is a discussion on Problems with Calculating within the C Programming forums, part of the General Programming Boards category; hi i am having a problem with my program. Its basically supposed to calculate this way: Licence price (per 1 ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    11

    Problems with Calculating

    hi i am having a problem with my program. Its basically supposed to calculate this way:

    Licence price (per 1 copy) Number of licences
    $125____________________5 or less
    $100____________________6-10
    $75_____________________more than 10

    Also posting ($25) and new service fee charge ($200). But i dont know what i did wrong in the code. It keeps printing out the same value for total.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
      char choice;
      int Client_ID_number[5];
      char Client_Company_name[10];
      char Client_address[20];
      int Number_of_software_licences[3];
      char New_subscriber[1];
      char Postage_and_handling[1];
      char Support_service[1];
      int total;
     
     printf ("             ******************************************************                     ");
     printf ("    ******[CLIENT LIST PROGRAM by Abhyash Timsina]**********                    ");
     printf ("   **********************************************************                   ");
     printf ("\na. Add a New Client\n");
     printf ("b. Manage a Current Client\n");
     printf ("c. Exit Program\n\n");
     printf ("Enter your choice:- ");
     scanf ("%c", &choice);
     
      if ( choice == 'a' )
      {
           printf ("\nEnter Company Name: ");
           scanf ("%s", &Client_Company_name[10]);
           printf ("Enter Company Address: ");
           scanf ("%s", &Client_address[20]);
           printf ("Enter Number of Licences: ");
           scanf ("%s", &Number_of_software_licences[3]);
           if ( Number_of_software_licences[3] <= 5 )
              {
                    total = 125 * Number_of_software_licences[3];
              }
           else if ( Number_of_software_licences[3] >= 6 || Number_of_software_licences[3] <= 10 )
              {
                   total = 100 * Number_of_software_licences[3];
              }
           else if ( Number_of_software_licences[3] > 10 )
              {
                   total = 75 * Number_of_software_licences[3];
              }
           printf ("Postage & Handling Fee? (y or n): ");
           scanf ("%s", &Postage_and_handling[1]);
           if ( Postage_and_handling[1] == 'y' )
              {
                   total + 25;
              }
           else if ( Postage_and_handling[1] == 'n' )
              {
                   total + 0;
              }
           printf ("Support Service? (y or n): ");
           scanf ("%s", &Support_service[1]);
           if ( Support_service[1] == 'y' )
              {
                   total + 200;
              }
           else if ( Support_service[1] == 'n' )
              {
                   total + 0;
              }
           printf ("The Client (%s) Total Bill is: %d\n\n", &Client_Company_name[10], &total);
           
      }
      else if ( choice == 'b' )
      {
           printf ("Test");
      }
      else if ( choice == 'c' )
      {
          return 0;
      } 
    
      system("PAUSE");	
      return 0;
    }

  2. #2
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    What do you think this does?
    Code:
    scanf ("%s", &Client_Company_name[10]);
    Hint: refer to your C book on the proper usage of scanf, and arrays, and the & operator.

    Hint 2: scanf is not very good for reading free form strings. See How do I get a line from the user?

    Hint 3: C arrays start at 0 (zero).

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    11
    can you please give me the answer. it will be easier for me to understand..

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You mean easier for you to turn in for homework. Do your own work, you lazy bastard.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    11
    ... cant believe how rude people are

    i just need to know whats wrong with the calculation

  6. #6
    Dump Truck Internet valis's Avatar
    Join Date
    Jul 2005
    Posts
    357
    I see nothing wrong with giving you the logical answer but no the code.
    (also people bastards to keep out the riff-raff)

    & is the address of operator, &array[index] is actually (array + index) where array is a base address (so &apples[10] = &apples + 10, basically). You are trying to fill 'array' with a user string using scanf, yet you pass the end of your arrays--you see the problem?

    edit:
    Just to let you know, with your current code user input is spilling over one or more of your variables into other variables (because you are passing the end of your arrays).
    Last edited by valis; 04-25-2006 at 12:43 AM.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,443
    Code:
    $ gcc -W -Wall -ansi -pedantic -O2 foo.c
    foo.c: In function `main':
    foo.c:32: warning: char format, different type arg (arg 2)
    foo.c:65: warning: int format, pointer arg (arg 3)
    foo.c:7: warning: unused variable `Client_ID_number'
    foo.c:11: warning: unused variable `New_subscriber'
    foo.c: At top level:
    foo.c:4: warning: unused parameter 'argc'
    foo.c:4: warning: unused parameter 'argv'
    foo.c: In function `main':
    foo.c:49: warning: statement with no effect
    foo.c:53: warning: statement with no effect
    foo.c:59: warning: statement with no effect
    foo.c:63: warning: statement with no effect
    echo what everyone else has said.
    Almost every instance of your use of arrays is wrong.

    - Storing beyond the end of the array
    scanf ("%s", &Number_of_software_licences[3]);

    - Treating an array of chars as an int
    if ( Number_of_software_licences[3] <= 5 )

    - Forgetting that a string has a \0, so the minumum length of an array is 2
    char Support_service[1];
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. Rendering problems (DirectX?)
    By OnionKnight in forum Tech Board
    Replies: 0
    Last Post: 08-17-2006, 12:17 PM
  3. Recursion
    By Lionmane in forum C Programming
    Replies: 11
    Last Post: 06-04-2005, 12:00 AM
  4. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 03:35 PM

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