I'm took an fundamental of C++ .
Here is my code, could you please help me to point out the error .
Thanks

#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <string>

using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::fixed;
using std::setprecision;

int main()
{
    int unit_of_desks = 0;
    int Length = 0;
    int Width = 0;
    int Type_of_Wood;
    int drawers = 0;
    int Base_Cost;
    int Area;
    int Cost_per_drawers;
    int Total_cost;
    int Total_cost_all_desks;
    char Mahogany = 'M';
    char Oaks = 'O';
    char Regular = 'R';
        
        cout << "Input how many drawers: " ;
        cin >> drawers ;
        
        cout << "Input the Unit of desks: " ;
        cin >> unit_of_desks ;
        
        cout << "Input the Length: " ;
        cin >> Length ;
        
        cout << "Input the Width: " ;
        cin >> Width ;
        
        cout << "Input the Type of Wood: " ;
        cin >> Type_of_Wood ;
        
        
             Base_Cost = 250 + 0 ;       
             Area = Length * Width ; 
             
  if (drawers >0)
              Cost_per_drawers = drawers *30; 
             
         
     else 
    
          if (Area >750 ) 
                      Total_cost =Base_Cost + 750;
                                   
        else
            if (Type_of_Wood = 'M' )
               Total_cost = Total_cost + 150;
               
            else 
                 if (Type_of_Wood = 'O')
                    Total_cost = Total_cost + 125;
                    
                    else
                        if (Type_of_Wood = 'R')
                        Total_cost = Total_cost ;
                    
        Total_cost = Total_cost + Cost_per_drawers;
        
        Total_cost_all_desks = Total_cost * unit_of_desks; 
              cout << fixed <<setprecision(2) << endl;
              cout << "Total amount:$ " << Total_cost_all_desks << endl;
              
              
system ("pause");
return 0;
  
}

Recommended Answers

All 7 Replies

Please provide more detail; Does your program compile successfully?
If not, then please post the compiler errors/warnings;
If so, what happens when you run your program, and how does this differ to what you're expecting?

Syntactically your code *appears* to be correct. You could try elaborating a bit further.

Please provide more detail; Does your program compile successfully?
If not, then please post the compiler errors/warnings;
If so, what happens when you run your program, and how does this differ to what you're expecting?

thanks for the reply ,
the program compile successfully , but it calculate the Total_cost incorrectly and Total_cost_all_desk as well. Look like the Base_cost not added .

I think the problem is coming from the way you are using your if...else statements. On line 54 you check to see if the area is greater than 750 and if it is you add your base_cost with 750 and store it into total_cost but if its not you don't add the base_cost. Try walking through your code with your debugger and see if its doing what you think it should be doing.

Please read or find code convention to follow because your code is very difficult to follow here. Also, please explain what your code is doing when you ask for help. Even better, give an example of input and expected output.

Base_Cost = 250 + 0;
Area = Length * Width;

if (drawers >0)
  Cost_per_drawers = drawers *30; 
else if (Area >750 )
  Total_cost =Base_Cost + 750;
else if (Type_of_Wood = 'M' )
  Total_cost = Total_cost + 150;
else if (Type_of_Wood = 'O')
  Total_cost = Total_cost + 125;
else if (Type_of_Wood = 'R')
  Total_cost = Total_cost ;

Total_cost = Total_cost + Cost_per_drawers;
Total_cost_all_desks = Total_cost * unit_of_desks;

This way, you should be able to find where your flaw logic is. Hope you will see it better next time. (I just saw that the thread is solved, sorry)

Umm... what does this command do :-

system("pause")

It's an obnoxious way to accomplish something relatively easy a.k.a pausing the command prompt.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.