#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    4
    Rep Power
    0

    C++ Widget Problem help!!


    A well-regarded manufacturer of widgets has been losing 4% of its sales each year. The company's annual profit is 10% of sales. This year, the company had %10 million in sales and a profit of $1 million. Determine the expected sales and profits for the next 10 years. Your program should display in the following form:
    Sales and Profit Projection
    ----------------------------------

    year Expected sales Projected Profit
    --------------------- --------------------------- ---------------------
    1 $10000000.00 $1000000.00
    2 $9600000.00 $960000.00
    3
    4
    5
    6
    7
    8
    9
    10

    please help me he gave 3 homework problems and this is the only one I can not figure out how to do please help me thank and write the program so I will know how next time I have one like this thank you and god bless! Biggest thing I can't do is figure out how to make the table and do the manips please help thanks,
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,392
    Rep Power
    1871
    > Biggest thing I can't do is figure out how to make the table and do the manips please help thanks,
    So what can you do?
    Do you know how to subtract 4% of a number? Can you do it in a loop?
    Do you know how to calculate 10% of a number? Can you do it in a loop?

    I mean, if you can do
    Code:
    for ( year = 1 ; year <= 10 ; year++ )
    and print all the information (without formatting), then we'll know exactly where to focus the help.

    Similarly, if you CAN'T do that, there is no point explaining the finer points of manipulators when you're still stuck on the basic maths of the problem.
    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
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    4
    Rep Power
    0
    Yeah I know that done got that, I just can't get a table like that cant figure out manipulatord
    Originally Posted by salem
    > Biggest thing I can't do is figure out how to make the table and do the manips please help thanks,
    So what can you do?
    Do you know how to subtract 4% of a number? Can you do it in a loop?
    Do you know how to calculate 10% of a number? Can you do it in a loop?

    I mean, if you can do
    Code:
    for ( year = 1 ; year <= 10 ; year++ )
    and print all the information (without formatting), then we'll know exactly where to focus the help.

    Similarly, if you CAN'T do that, there is no point explaining the finer points of manipulators when you're still stuck on the basic maths of the problem.
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,171
    Rep Power
    2222
    Originally Posted by djohn2010
    Yeah I know that done got that,...
    That does not even parse. In English, please.

    You print out the table by first printing the table header and then you enter a loop to print out each line. Simple as that. You already say that you know how to write a loop (or at least I think you did), so is it that you don't know how to print to the screen? Funny, that should have been covered in "Hello, world."

    Since you say this is C++, I assume that you are not allowed to use printf, but instead must use iostreams' cout. What part of cout do you not understand?

    Do what you can. When we can see where you're having problems then we will be able to help.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    4
    Rep Power
    0
    here is what i have but this is not working. Please help.
    #include <iostream>
    #include <iomanip>
    using namespace std;
    int main()
    {
    int year, totsales,totprof;
    double sales, profit;

    sales=10,000,000.00;
    profit=1,000,000;
    totsales=0;
    totprof=0;

    for(year=1;year<=10;year++)
    sales=sales-(sales*.04*(1));
    profit= sales*.1;
    cout<<setw(42)<<"SALES AND PROFIT PROJECTION"<<endl;
    cout<<setw(42)<<"---------------------------"<<endl;
    cout<<setw(10)<<"year"<<endl;
    cout<<setw(10)<<"----"<<endl;
    cout<<setw(20)<<" sales"<<endl;
    cout<<setw(20)<<"--------------"<<endl;
    cout<<setw(21)<<"profit"<<endl;
    cout<<setw(21)<<"----------------"<<endl;
    cout<<setw(50)<<"totsales"<<endl;
    cout<<setw(50)<<"--------------------------------------------------"<<endl;
    cout<<setw(50)<<"totprof"<<endl;
    cout<<setw(50)<<"--------------------------------------------------"<<endl;
    return 0;
    }
  10. #6
  11. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,392
    Rep Power
    1871
    > sales=10,000,000.00;
    > profit=1,000,000;
    Comma is not a grouping operator in C or C++.
    So you respectively end up with 10 and 1 assigned.

    Then you need to put braces around the part of the code you want to loop.
    Like so.
    Code:
    for(year=1;year<=10;year++) {
        sales=sales-(sales*.04*(1));
        profit= sales*.1;
        cout << sales << " " << profit << endl;
    }
    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
  12. #7
  13. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,171
    Rep Power
    2222
    Originally Posted by djohn2010
    here is what i have but this is not working.
    What is "not working" supposed to mean? Whatever makes you think that it's "not working"?

    That is a very serious question. When you have a problem and are seeking help to fix it, you need to describe just exactly what that problem is. Forcing free help to play time-consuming and tedious guessing games with you will prove to be counter-productive.

    1. Does it compile? If not, then what exactly are the error messages?

    2. Does it compile with warnings? If so, then again what exactly are the warnings?

    Always have your compiler display warnings. Never ignore warnings. Warnings are far more important and useful than error messages are.

    3. Does it compile without warnings, but when you try to run it does it:
    a. crash? At about what point does it crash (ie, immediately, after a while, when you enter a certain value at a particular prompt)?

    b. not accept input when it should?

    c. seem to freeze? Does it stop producing output or dump tons of the same output to the screen?

    d. appear to run, but produces the wrong output. Please define "wrong output". In particular, tell us what output you expected and what output you got instead. Copy-and-paste whenever possible. If the wrong output is in response to input that you provide to the program, then also include test input values, what output it should produce in response to those inputs, and what you got instead -- this will give us a test case to run through a debugger, if our investigation has to go that far.

    So you see, I hope, that "not working" tells us nothing. Give us a description of a specific problem and you will receive prompt and meaningful help. Withhold that description and you will just waste our time and yours.

    Also, for several problems it helps us to know what operating system and development system (AKA "compiler") you're using. Though in this case that information is not necessary, but remember that for future questions.

    And it helps immensely if your code is readable, which means that it needs to be formatted. Since HTML strips out indenting, you need to use code tags to preserve your code's formatting; eg:

    Code:
    Please help.
    #include <iostream>
    #include <iomanip>
    using namespace std;
    int main()
    {
    	int year, totsales,totprof;
    	double sales, profit;
    
    	sales=10,000,000.00;
    	profit=1,000,000;
    	totsales=0;
    	totprof=0;
    
    	for(year=1;year<=10;year++) 
    	sales=sales-(sales*.04*(1));
    	profit= sales*.1;
    	       cout<<setw(42)<<"SALES AND PROFIT PROJECTION"<<endl;
    		   cout<<setw(42)<<"---------------------------"<<endl;
    		   cout<<setw(10)<<"year"<<endl;
    		   cout<<setw(10)<<"----"<<endl;
    		   cout<<setw(20)<<" sales"<<endl;
    			cout<<setw(20)<<"--------------"<<endl;
    		   cout<<setw(21)<<"profit"<<endl;
    			cout<<setw(21)<<"----------------"<<endl;
    		   cout<<setw(50)<<"totsales"<<endl;
    			cout<<setw(50)<<"--------------------------------------------------"<<endl;
    			cout<<setw(50)<<"totprof"<<endl;
    				cout<<setw(50)<<"--------------------------------------------------"<<endl;
    			return 0;
    }
    OK, that is kind of crappy, though we've seen far worse. Try setting your tabs to a more reasonable value, like 4, and set your editor to insert spaces instead of tab characters so that your tab settings will travel well. Eg:
    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    int main()
    {
        int year, totsales,totprof;
        double sales, profit;
    
        sales=10,000,000.00;
        profit=1,000,000;
        totsales=0;
        totprof=0;
    
        for(year=1;year<=10;year++) 
            sales=sales-(sales*.04*(1));
        
        profit= sales*.1;
        
        cout<<setw(42)<<"SALES AND PROFIT PROJECTION"<<endl;
        cout<<setw(42)<<"---------------------------"<<endl;
        cout<<setw(10)<<"year"<<endl;
        cout<<setw(10)<<"----"<<endl;
        cout<<setw(20)<<" sales"<<endl;
        cout<<setw(20)<<"--------------"<<endl;
        cout<<setw(21)<<"profit"<<endl;
        cout<<setw(21)<<"----------------"<<endl;
        cout<<setw(50)<<"totsales"<<endl;
        cout<<setw(50)<<"--------------------------------------------------"<<endl;
        cout<<setw(50)<<"totprof"<<endl;
        cout<<setw(50)<<"--------------------------------------------------"<<endl;
        
        return 0;
    }
    Now, when I compile your program with MinGW g++, I get this (please note the -Wall option, which tells g++ to display all warnings):
    C:TEST>g++ -Wall sales.cpp
    sales.cpp: In function `int main()':
    sales.cpp:11: warning: left-hand operand of comma expression has no effect
    sales.cpp:12: warning: left-hand operand of comma expression has no effect

    C:TEST>g++ -Wall sales.cpp
    Here are lines 11 and 12:
    Code:
        sales=10,000,000.00;
        profit=1,000,000;
    The problem is that that is incorrect syntax for integer or floating-point literals. The comma splits an expression into multiple expressions, most of which have a side-effect and the last of which provides the evaluated value of the entire expression. We normally only see comma expressions in for-statements. Never put a comma inside a number, because that means something entirely different to the compiler.

    At this point, I will re-emphasize the importance of not ignoring warnings. When those two lines are executed, what values are sales and profit set to? 10000000.0 and 1000000.0, respectively? No, they are set to 10.0 and 1.0, respectively. If you ignore those warnings, then you will have initialized your variables to the wrong values, which would give you wrong results. It is most important to not ignore warnings.

    Those two lines should be (also making both doubles):
    Code:
        sales=10000000.0;
        profit=1000000.0;
    Now my compiler compiles it with no warnings. When I run it, I get this output (using code tags here to preserve indentation):
    Code:
                   SALES AND PROFIT PROJECTION
                   ---------------------------
          year
          ----
                   sales
          --------------
                   profit
         ----------------
                                              totsales
    --------------------------------------------------
                                               totprof
    --------------------------------------------------
    Now, what output did you expect it to give you?
    Last edited by dwise1_aol; April 11th, 2013 at 01:27 PM.

IMN logo majestic logo threadwatch logo seochat tools logo