Results 1 to 3 of 3

Thread: Problem with my IF ELSE IF statements in C++

  1. #1
    Join Date
    Aug 2009
    Posts
    54

    Problem with my IF ELSE IF statements in C++

    The below is my code. The problem is that I am not getting the cost correctly based on my conditions. It always displays the total cost something that I don't want. I want to specify some conditions based on which the cost and then the discount is to be calculated. But the code always displays some weird values. What modifications do I need to do in my code to get the conditional based total cost and discounts?

    Code:
    cost=(cost * 100)
    if (cost < 5)
    cost=(cost * 100);
    
    else if (cost > 5 && cost < 10);
    cost=(100 - (100 * 0.2));
    
    else if (cost > 10 && cost < 20)
    cost=(100 - (100 * 0.3));
    
    else if (cost > 20 && cost < 30)
    cost=(100 - (100 * 0.4));
    
    else if (cost > 30);
    cost=(100 -(100 * 0.5)); 
    
    discount = (100 - cost);
    
    cout<<"Total cost: " << cost << " and discount: " << discount;

  2. #2
    Join Date
    May 2008
    Posts
    2,015

    Re: Problem with my IF ELSE IF statements in C++

    else if (cost > 5 && cost < 10);
    Your problem lies over here. You have used ";" at the end of the "else if" statement. This indicates the end of condition if the value of "cost" is in between 5 and 10, then the compiler has nothing to do. That is the reason why you are getting such answers. Just remove the semicolon and you will get the proper result.

  3. #3
    Join Date
    May 2008
    Posts
    685

    Re: Problem with my IF ELSE IF statements in C++

    cost=(cost * 100)
    if (cost < 5)
    cost=(cost * 100);
    First and foremost problem lies over here, you are already multiplying the cost with 100 before implementing the conditions. Remove the first statement and also apply the changes that was said previously and then try to run the code. This time you should get the proper answers.

Similar Threads

  1. Different statements in SQL query
    By Kuna! in forum Software Development
    Replies: 10
    Last Post: 12-03-2011, 03:02 PM
  2. Conditional statements in C sharp
    By Vaikuntam in forum Software Development
    Replies: 3
    Last Post: 16-12-2010, 07:36 AM
  3. How to Inject SQL Statements in PHP?
    By DANIEL 602 in forum Software Development
    Replies: 5
    Last Post: 05-03-2010, 02:46 AM
  4. Difference between DML statements and DDL statements
    By Prashobh Mallu in forum Software Development
    Replies: 5
    Last Post: 11-01-2010, 12:07 PM
  5. SQL statements with JSP
    By blindsleeper in forum Software Development
    Replies: 2
    Last Post: 16-05-2009, 09:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •