Thread: problems with repeating sentences..

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    26

    problems with repeating sentences..

    i am on the way of writing a program as my assignment..

    it is about the total value of purchase after discount during a megasale.

    i encounter problem when writing it..

    the problem go as follw:

    10% discount is given to purchase>RM1000
    5% discount is given to purchase>500 and <1000
    2% discount is given to purchase>100 and<500
    no discount for purchase<= 100

    as when i debug and run it,

    it either appear all four sentences of " the total value of purchase is RMXXX"

    or the value is wrong...

    i am curious about the appearance all those four sentences simultaneously..

    and how come the value will wrong as i entered the correct formula: "total= a*(100-b)/100"

    please generously help me.. thank you..

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Show your code
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    26

    sorry.. i dont understand.. please have a look at it..

    i suppose code means the source code file...
    then i attached it already..
    Code:
    #include<stdio.h>
    int main ()
    
    {
    int a,b=0,total;
    
    
    printf("\nEnter the value of purchase: RM ");
    scanf("%d",&a);
    
    
     {
    if (a>1000)
    {b=10)
      }
    
    {
    if ((a>500) && (a<=1000))
     {b=5)
     }
    
      {
    if ((a>100) && (a<=500))
    (b=2)
      }
    
    
     {
    if (a<=100)
    (b=0);
     }
    
    {
    if (a<=0)
    printf("\nYou entered an invalid value."); }
    
    
    total= (a*(100-b)/100);
    
    printf("\nThe total value of purchase is RM %d.",total);
    
    return (0);
    
    }
    please have a look at it...
    thanks..

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    i suppose code means the source code file...
    Post the smallest and simplest compilable program that demonstrates the problem. The program you posted obviously does not complex due to elementary syntax errors.

    Also, kindly indent your code.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Also you are failing the indentation

    in your case the solution will be to use
    Code:
    if()
    {
    }
    else if()
    {
    }
    else if()
    {
    }
    else
    {
    }
    construction
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Feb 2008
    Posts
    26
    Thanks for the help that you all have given me..

    although it was just a little help..
    and fortunately, i have successfully completed my program..
    thanks again and cheer..

  7. #7
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    Oh, and for problems like this, you may want to look into using floats (or doubles if very large numbers are involved (it doesn't seem like that's the case here)) as fractional values with integers will almost always cause accuracy-related problems.
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

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. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 06:35 AM
  3. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  4. Rendering problems (DirectX?)
    By OnionKnight in forum Tech Board
    Replies: 0
    Last Post: 08-17-2006, 12:17 PM
  5. Help me for repeating decimal numbers plz
    By Supra in forum C Programming
    Replies: 3
    Last Post: 09-27-2001, 03:18 PM