Jump to content

help with c++ lab please

Go to solution Solved by Cruiseshipfan,

Ok I know your issue.

 do{            cout << "\nWhat is the cost of the book? $";    cin >> Cost;    }while(Cost<0);        cout <<"Invalid entry must be greater than 0. ";

You are looping while the value is correct. You should have it loop while it is incorrect and have the error statement in an if. So when ever the input value is correct you are causing it to go back to the top of the loop, but when you input the incorrect value it is leaving the loop. So reverse the conditions and add an if.

 

As for it starting on the same line. You need to tell it to go to the next line in the error message.

 

For example

do{   //GET INFORMATION   if (cost < 2)   {     cout << "Value is too small\n";   }} while(cost < 2)

so im stuck and the teacher refuses to give assistance when i ask her for help she just say im not giving you the answers. wot even stear me in the right direction

 

 

so this is my code and out put

 

/Lab 6
//Buying and selling Books

#include <iostream>
#include <iomanip>
using namespace std;

int main ()   
{
    double Cost;
    double Markup;
    double Sales_Tax;

    double Tax;
    double Markup_Rate;
    double Total_Cost;
    double Markup_Percent;
    double Sales_Tax_Percent;
    
    cout << "My name\n\n\n" << endl;    
    

    do{    
    
    cout << "\nWhat is the cost of the book? $";
    cin >> Cost;
    }while(Cost<0);
    
    cout <<"Invalid entry must be greater than 0. ";
    
    do{    

    cout << "What is the markup percentage? ";   
    cin >> Markup_Rate;
    }while((Markup_Rate<0) && (Markup_Rate<=100));
    
    cout <<"Invalid entry must be between 1 and 100";



    do{    
        
    cout << "What is the sales tax rate? ";
    cin >> Sales_Tax;
    }while((Sales_Tax<0)&&(Sales_Tax<=0.15));
    
    cout <<"Invalid entry must be Greater than 0 but less than .15";
    
    
   
    Markup= Cost*Markup_Rate;
    Tax= (Markup+Cost)*Sales_Tax;
    Total_Cost= Markup+Tax+Cost;
    Markup_Percent = Markup_Rate*100;
    Sales_Tax_Percent = Sales_Tax*100;
    
    cout << setw(2)<< fixed << setprecision(2) << "\nThe Wholesale cost is $" <<Cost<< endl;
    cout << setw(2)<< fixed << setprecision(0) <<"The markup Rate is " << Markup_Percent << "%" << endl;
    cout << setw(2)<< fixed << setprecision(0) << " The sales tax rate is " << Sales_Tax_Percent<< "%" << endl;

    cout << setw(2) << fixed << setprecision(2) << "\nMarkup Amount: $" << Markup << endl;
    cout << setw(2) << fixed << setprecision(2) << "Sales Tax Amount: $" << Tax << endl;
    cout << setw(2) << fixed << setprecision(2) <<"Total Cost of Book to customer: $" << Total_Cost << endl;
  return 0;
}       

post-20713-0-10398000-1445135556.jpg

 

 

so basicaly this is what im supposed to do

 

Modify your code to check for valid input values. Specifically:

The Wholesale Cost must be positive. { I.e. not zero or negative }

The Mark-up Percentage must be positive but less than or equal to 100%.

The Sales Tax Rate ( Percentage ) must be positive but strictly less than 15%.

 

Use the most appropriate LOOP construct to get user input until a good value is given.

 

Be sure your code tells the user that the value entered was wrong, when it is invalid.

If a good value is given, do NOT say anything to the user. I.e. when a good value is given – move on.

 

part b of the lab will be to ask user to run the code again(without having to re launch program) but  i should be able to do that on my own but would apreciate being pointed  in the right directionor give me any helpfull tips with out giving me the answer 

 

 

 

 

my code does what it supposed to do as far as validating wether the person enetered valid data

i just cant seem to get it to format correctly the lines keep running into eah other how do i get it to format correctly on the command prompt

 

 

 

thank you in advance for any help you may provide

 

 

Link to comment
https://linustechtips.com/topic/469073-help-with-c-lab-please/
Share on other sites

Link to post
Share on other sites

Using \n and keeping lines of code under 80 characters long helps with formatting.\

 

EDIT: \n is a line feed by the way. I usually put one after every single line of text that is printed to the display. 

[Out-of-date] Want to learn how to make your own custom Windows 10 image?

 

Desktop: AMD R9 3900X | ASUS ROG Strix X570-F | Radeon RX 5700 XT | EVGA GTX 1080 SC | 32GB Trident Z Neo 3600MHz | 1TB 970 EVO | 256GB 840 EVO | 960GB Corsair Force LE | EVGA G2 850W | Phanteks P400S

Laptop: Intel M-5Y10c | Intel HD Graphics | 8GB RAM | 250GB Micron SSD | Asus UX305FA

Server 01: Intel Xeon D 1541 | ASRock Rack D1541D4I-2L2T | 32GB Hynix ECC DDR4 | 4x8TB Western Digital HDDs | 32TB Raw 16TB Usable

Server 02: Intel i7 7700K | Gigabye Z170N Gaming5 | 16GB Trident Z 3200MHz

Link to comment
https://linustechtips.com/topic/469073-help-with-c-lab-please/#findComment-6291254
Share on other sites

Link to post
Share on other sites

i tred that it does solve the formating isu but the error staement still comes out after the correct value is inputed

Shouldn't the conditions for a while loop be indented or within {}?

 

Such as this:

while(Cost<0);

{

    cout <<"Invalid entry must be greater than 0. ";

}

 

or this:

while(Cost<0);

    cout <<"Invalid entry must be greater than 0. ";

[Out-of-date] Want to learn how to make your own custom Windows 10 image?

 

Desktop: AMD R9 3900X | ASUS ROG Strix X570-F | Radeon RX 5700 XT | EVGA GTX 1080 SC | 32GB Trident Z Neo 3600MHz | 1TB 970 EVO | 256GB 840 EVO | 960GB Corsair Force LE | EVGA G2 850W | Phanteks P400S

Laptop: Intel M-5Y10c | Intel HD Graphics | 8GB RAM | 250GB Micron SSD | Asus UX305FA

Server 01: Intel Xeon D 1541 | ASRock Rack D1541D4I-2L2T | 32GB Hynix ECC DDR4 | 4x8TB Western Digital HDDs | 32TB Raw 16TB Usable

Server 02: Intel i7 7700K | Gigabye Z170N Gaming5 | 16GB Trident Z 3200MHz

Link to comment
https://linustechtips.com/topic/469073-help-with-c-lab-please/#findComment-6291294
Share on other sites

Link to post
Share on other sites

Just a tip, using Allman style formatting is so much nicer.

[Out-of-date] Want to learn how to make your own custom Windows 10 image?

 

Desktop: AMD R9 3900X | ASUS ROG Strix X570-F | Radeon RX 5700 XT | EVGA GTX 1080 SC | 32GB Trident Z Neo 3600MHz | 1TB 970 EVO | 256GB 840 EVO | 960GB Corsair Force LE | EVGA G2 850W | Phanteks P400S

Laptop: Intel M-5Y10c | Intel HD Graphics | 8GB RAM | 250GB Micron SSD | Asus UX305FA

Server 01: Intel Xeon D 1541 | ASRock Rack D1541D4I-2L2T | 32GB Hynix ECC DDR4 | 4x8TB Western Digital HDDs | 32TB Raw 16TB Usable

Server 02: Intel i7 7700K | Gigabye Z170N Gaming5 | 16GB Trident Z 3200MHz

Link to comment
https://linustechtips.com/topic/469073-help-with-c-lab-please/#findComment-6291301
Share on other sites

Link to post
Share on other sites

Shouldn't the conditions for a while loop be indented or within {}?

 

Such as this:

while(Cost<0);

{

    cout <<"Invalid entry must be greater than 0. ";

}

 

or this:

while(Cost<0);

    cout <<"Invalid entry must be greater than 0. ";

didnt work thanks any ways

Link to comment
https://linustechtips.com/topic/469073-help-with-c-lab-please/#findComment-6291395
Share on other sites

Link to post
Share on other sites

What is wrong with your code so far? Person above already mentioned using \n to add a new  line. Are you having any other problems?

Yes If i I type in an invalid value it's supposed to look like this

 

 

Enter number

(You enter a number)  <---- DOESN'T PRINT JUST SAYING WHAT HAPPENS HERE

You have entered wrong number 

(You type another number)

 

 

But instead is

 

Enter a number

 

Enter a number

 

Now when u finally enter a correct number it now says wrong number enter number between

 

Then it starts new question on same line

 

 

 

Edited stupid auto correct changed a word to a f word sorry wast supposed to be there my phone is messed up.

Link to comment
https://linustechtips.com/topic/469073-help-with-c-lab-please/#findComment-6291723
Share on other sites

Link to post
Share on other sites

Ok I know your issue.

 do{            cout << "\nWhat is the cost of the book? $";    cin >> Cost;    }while(Cost<0);        cout <<"Invalid entry must be greater than 0. ";

You are looping while the value is correct. You should have it loop while it is incorrect and have the error statement in an if. So when ever the input value is correct you are causing it to go back to the top of the loop, but when you input the incorrect value it is leaving the loop. So reverse the conditions and add an if.

 

As for it starting on the same line. You need to tell it to go to the next line in the error message.

 

For example

do{   //GET INFORMATION   if (cost < 2)   {     cout << "Value is too small\n";   }} while(cost < 2)

My PC:

Case: NZXT Phantom 420 | Motherboard: Gigabyte GA-Z77X-UD3H-WB WIFI | PSU: Corsair TX650M | CPU: Intel 3570k | CPU Cooler: Corsair H60 with Corsair SP120s in push pull | GPU: AMD 7970 GV-R797OC-3GD | RAM: 16GB Corsair Vengeance 4x4GB | SSD: Samsung 830 256GB | HDD: Seagate Barracuda 2TB
Link to comment
https://linustechtips.com/topic/469073-help-with-c-lab-please/#findComment-6291792
Share on other sites

Link to post
Share on other sites

 

Ok I know your issue.

 do{            cout << "\nWhat is the cost of the book? $";    cin >> Cost;    }while(Cost<0);        cout <<"Invalid entry must be greater than 0. ";

You are looping while the value is correct. You should have it loop while it is incorrect and have the error statement in an if. So when ever the input value is correct you are causing it to go back to the top of the loop, but when you input the incorrect value it is leaving the loop. So reverse the conditions and add an if.

 

As for it starting on the same line. You need to tell it to go to the next line in the error message.

 

For example

do{   //GET INFORMATION   if (cost < 2)   {     cout << "Value is too small\n";   }} while(cost < 2)

thank you it worked

 

this is what it ended up as

 

#include <iostream>

#include <iomanip>

using namespace std;

int main ()   

{

    double Cost;

    double Markup;

    double Sales_Tax;

    double Tax;

    double Markup_Rate;

    double Total_Cost;

    double Markup_Percent;

    double Sales_Tax_Percent;

    

    cout << "\n\nMichael Watkins" << endl;    

    cout << "Lab 6_A\n\n\n"<<endl;

do

{    

    

        cout << "What is wholesale the cost of the book? $";

        cin >> Cost;

    if(Cost<1)

    {

        cout << "Invalid entry must be greater than 0."<< endl;

        cout << "Please Try Again.\n" << endl;    

    }

}while(Cost<1);

    cout<<"\n";    

do

{    

    

        cout << "What is the markup percentage in decimal form?";   

        cin >> Markup_Rate;

    if((Markup_Rate<0) || (Markup_Rate > 1))

    {

        cout <<"\nInvalid entry must be a percentage"<<endl;

        cout <<"in decimal form between 0 and 1"<<endl;

        cout <<"Please Try Again.\n" << endl;

    }

}while((Markup_Rate<0) || (Markup_Rate > 1));

        cout<<"\n";    

do

{    

        cout << "What is the sales tax rate in decimal form? "<< endl;        

        cin >> Sales_Tax;

    if((Sales_Tax<=0)||(Sales_Tax>=0.15))

    {

        cout <<"\nInvalid entry must be a percentage in decimal"<<endl;

        cout <<"form that is more than 0 but less than .15"<< endl;

        cout <<"Please Try Again.\n" << endl;

    }

}while((Sales_Tax<=0) || (Sales_Tax>=0.15));

    cout<<"\n";

    

    Markup= Cost*Markup_Rate;

    Tax= (Markup+Cost)*Sales_Tax;

    Total_Cost= Markup+Tax+Cost;

    Markup_Percent = Markup_Rate*100;

    Sales_Tax_Percent = Sales_Tax*100;

    

    cout << setw(2)<< fixed << setprecision(2) << "\nThe Wholesale cost is $" <<Cost<< endl;

    cout << setw(2)<< fixed << setprecision(0) <<"The markup Rate is " << Markup_Percent << "%" << endl;

    cout << setw(2)<< fixed << setprecision(0) << " The sales tax rate is " << Sales_Tax_Percent<< "%" << endl;

    cout << setw(2) << fixed << setprecision(2) << "\nMarkup Amount: $" << Markup << endl;

    cout << setw(2) << fixed << setprecision(2) << "Sales Tax Amount: $" << Tax << endl;

    cout << setw(2) << fixed << setprecision(2) <<"Total Cost of Book to customer: $" << Total_Cost << endl;

  return 0;

}       

 

 

 

now it took me 2 hrs after i got out of work but it fimaly does what i need

 

next step to figure out

is adding code for this line

cout  << Do you want to enter new values << endl;

i got 1-2 hours beween tomorow and tuesday afternoon to figure it out

any quick tips that could point me in right direction i tried a do while but it just looped infinitely and wouldnt stop wether i hit y or n

Link to comment
https://linustechtips.com/topic/469073-help-with-c-lab-please/#findComment-6297741
Share on other sites

Link to post
Share on other sites

next step to figure out

is adding code for this line

cout  << Do you want to enter new values << endl;

i got 1-2 hours beween tomorow and tuesday afternoon to figure it out

any quick tips that could point me in right direction i tried a do while but it just looped infinitely and wouldnt stop wether i hit y or n

 

You would get an input until the user inputs a correct input. 

//Print instructionwhile(/*Not y or n*/){   //Get char input}

To get it to enter new value you would put the whole program in a loop that loops while the input is not 'y'.

 

NOTE: Make sure when you are checking the input that you are doing so correctly. If you are inputting a string you will need to do a string compare, as the C comparison operators do not work on strings, since they are actually pointers and you will be comparing the addresses rather then the strings. 

My PC:

Case: NZXT Phantom 420 | Motherboard: Gigabyte GA-Z77X-UD3H-WB WIFI | PSU: Corsair TX650M | CPU: Intel 3570k | CPU Cooler: Corsair H60 with Corsair SP120s in push pull | GPU: AMD 7970 GV-R797OC-3GD | RAM: 16GB Corsair Vengeance 4x4GB | SSD: Samsung 830 256GB | HDD: Seagate Barracuda 2TB
Link to comment
https://linustechtips.com/topic/469073-help-with-c-lab-please/#findComment-6311122
Share on other sites

Link to post
Share on other sites

@madknight3 @Cruiseshipfan thank you I apreciate it and will remember that for next lab. Unfortunately it had to be submitted an hour ago. I ended up wrapping the whole program in a  do while statement. It's probably not the way  the professor wanted it to be done. It's messy code but it does output the way she wanted it to output.

Link to comment
https://linustechtips.com/topic/469073-help-with-c-lab-please/#findComment-6311294
Share on other sites

Link to post
Share on other sites

thank you I apreciate it and will remember that for next lab. Unfortunately it had to be submitted an hour ago. I ended up wrapping the whole program in a  do while statement. It's probably not the way  the professor wanted it to be done. It's messy code but it does output the way she wanted it to output.

 

Sometimes you have to rush to make deadlines. If this was code that you needed to continue working on, it would be worth cleaning it up. Since it's an assignment for school you probably wont be needing it again. Still, it may be worth trying to make it better for extra practice if you have the time. If not, just work to do better on the next assignment.

Link to comment
https://linustechtips.com/topic/469073-help-with-c-lab-please/#findComment-6313158
Share on other sites

Link to post
Share on other sites

Sometimes you have to rush to make deadlines. If this was code that you needed to continue working on, it would be worth cleaning it up. Since it's an assignment for school you probably wont be needing it again. Still, it may be worth trying to make it better for extra practice if you have the time. If not, just work to do better on the next assignment.

Actually I think we're supposed to build on it as the weeks go by. Most of my lab grades are 50s-high 60s, and 90% of the deductions are formating errors in indents or mistyping. Because in expected to do the lavs at home and google doesnt always help. she expects perfection and wants it done exactly if she wrote the code yet won't answer questions expects other students to help and or dosent give us any lab time time in class. it's supposed to be one day lecture one day lab but she uses 70% of the time lecturing 2% inclass exercises 8-10% going over homework and labs and the rest is usually the weekly quiz 

Some times we get a prod  30 min lab time. I wish I knew about the rate my professor site I wouldn't have picked her class. 

 

 

Only good thing is she drops at least on  homework grades and I think one lab grade(she's deating on upping these. And she gives fairness points (her way of saying curve).

Link to comment
https://linustechtips.com/topic/469073-help-with-c-lab-please/#findComment-6313308
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×