d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need Help With Basic Program! > I Donate Fg, If U Explain Good
Add Reply New Topic New Poll
Member
Posts: 5,913
Joined: May 6 2009
Gold: 0.00
Sep 20 2013 01:41pm
Yes.. I need help with a basic exercise in C++

This is my exercise;




This is my code I currently got for the exercise, but I just get "Matrix" :wallbash: please tell me whats wrong with my code.
Code

#include <iostream>
#include <iomanip>

using namespace std;

int main()

{
int tax;
int taxperunit;
int first_price;
double pricewithtax;
int last_price = 0;
int price_step = 0;
int tax_percentage = 0;


cout << setw(5) << "Price without tax" << setw(15) << "Tax" << setw (15) << "Price with tax" << endl;
cout << setw(50) << setfill('=') << "=" << endl;
cout << setfill(' '); // fill spaces in the next write operations

cout << "Enter first price= " << endl;
cin >> first_price;
cout << "Enter last price= " << endl;
cin >> last_price;
cout << "Price step= " << endl;
cin >> price_step;
cout << "Tax percentage= " << endl;
cin >> tax_percentage;

for(int first_price = 0; first_price <= last_price; first_price += price_step )
{

cout << setw(6) << first_price;

first_price =+ price_step;
taxperunit = (tax / 100) * first_price;
pricewithtax = taxperunit + first_price;


cout << setw(20) << first_price << endl;
cout << setw(20) << taxperunit << endl;
cout << setw(20) << pricewithtax << endl;

}

return 0;
}


I use codeblocks if that makes any difference.

Thanks
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Sep 20 2013 01:57pm
The problem was tax wasn't initialized and you were declaring a local variable "first_price" for the for loop and as an int, which adding a decimal (first_place += price_step) will do nothing, causing infinite loop.
Code
#include <iostream>
#include <iomanip>

using namespace std;

int main()

{
double tax;
double taxperunit;
double first_price;
double pricewithtax;
double last_price = 0;
double price_step = 0;
double tax_percentage = 0;


cout << setw(5) << "Price without tax" << setw(15) << "Tax" << setw (15) << "Price with tax" << endl;
cout << setw(50) << setfill('=') << "=" << endl;
cout << setfill(' '); // fill spaces in the next write operations

cout << "Enter first price= " << endl;
cin >> first_price;
cout << "Enter last price= " << endl;
cin >> last_price;
cout << "Price step= " << endl;
cin >> price_step;
cout << "Tax percentage= " << endl;
cin >> tax_percentage;


for(double i = first_price; i<= last_price;i+=price_step){
tax = i * (tax_percentage /100);
cout << i << "\t" << tax << "\t" << tax+first_price <<endl;

}

return 0;
}


This post was edited by labatymo on Sep 20 2013 02:01pm
Member
Posts: 6,192
Joined: Dec 13 2010
Gold: 6,669.99
Sep 20 2013 02:12pm
Quote (labatymo @ Sep 20 2013 04:57pm)
The problem was tax wasn't initialized and you were declaring a local variable "first_price" for the for loop and as an int, which adding a decimal (first_place += price_step) will do nothing, causing infinite loop.
Code
#include <iostream>
#include <iomanip>

using namespace std;

int main()

{
double tax;
double taxperunit;
double first_price;
double pricewithtax;
double last_price = 0;
double price_step = 0;
double tax_percentage = 0;


cout << setw(5) << "Price without tax" << setw(15) << "Tax" << setw (15) << "Price with tax" << endl;
cout << setw(50) << setfill('=') << "=" << endl;
cout <<  setfill(' '); // fill spaces in the next write operations

cout << "Enter first price= " << endl;
cin >> first_price;
cout << "Enter last price= " << endl;
cin >> last_price;
cout << "Price step= " << endl;
cin >> price_step;
cout << "Tax percentage= " << endl;
cin >> tax_percentage;


for(double i = first_price; i<= last_price;i+=price_step){
  tax =  i * (tax_percentage /100);
  cout << i << "\t" << tax << "\t" << tax+first_price <<endl;

}

return 0;
}


It should actually be this:

for(double i = first_price; i<= last_price;i+=price_step){
tax = i * (tax_percentage /100);
cout << i << "\t" << tax << "\t" << tax+i<<endl;

}

But other than that, it should work

This post was edited by ShadowFiend on Sep 20 2013 02:15pm
Member
Posts: 5,913
Joined: May 6 2009
Gold: 0.00
Sep 20 2013 02:18pm
Labatymo, your awesome! Thanks for the help, I might check in here later if I get into more trouble.

Quote (ShadowFiend @ Sep 20 2013 09:12pm)
It should actually be this:

for(double i = first_price; i<= last_price;i+=price_step){
tax = i * (tax_percentage /100);
cout << i << "\t" << tax << "\t" << tax+i<<endl;

}

But other than that, it should work


Yes I changed that myself :), after all its my assignment. Thanks again for the help everyone.


This post was edited by bomben on Sep 20 2013 02:20pm
Member
Posts: 4
Joined: Jun 2 2013
Gold: 0.00
Sep 20 2013 03:09pm
I'm working with this exercise atm

[IMG]http://i41.tinypic.com/25irzhj.jpg[/IMG]

Sadly i dont really understand what to do and how to start... I would be really happy if someone could help me to start how maybe give me a few suggestions on how to do this exercise.

This post was edited by MartinLindman on Sep 20 2013 03:11pm
Member
Posts: 5,913
Joined: May 6 2009
Gold: 0.00
Sep 20 2013 03:14pm
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll