Help - Search - Members - Calendar
Full Version: extreme Noob needs help with homework
AC Tools Everything Macro > General Discussion > C++/Delphi/VB Development
patch
This program is suposed to take in up to a hundred sales persons sales for a week and not figure out their salary but to see how many sales people make between certain figures in example 1 sales person made 399 in sales he gets 9% of his sales plus 200 dollars. the question is out of one hundred sales people how many made between 200-299
300-399
400-499
500-599
600-699
700-799
800-899
900-999
and over 1000

I used an aray becuase its part of the assignment and I used if else statements to find the variable array position. ok here is my code please be gental I am just starting and need all the help I can get so thanks in advance.

#include <iostream>
#include <iomanip>

using std::cin;
using std::cout;
using std::setw;
using std::endl;






int main()
{ //start main

int x=0;
int c = 1;
int cs(int);
const int arraysize = 9;
int frequency[arraysize] = {0};


cout <<"Enter sales persons total sales for this week.\n"; // get first salary
cin >> x;
while ( c <=100 && x != -1 ) {



cout <<"Enter sales persons total sales for this week.\n";
cin >> x;
if (x != -1){
x = cs(x);
++c; // increment counter
} //end if

if (x > 199 && x < 300)
++frequency[1];
else
if (x > 299 && x < 400)
++frequency[2];
else
if (x > 399 && x < 500)
++frequency[3];
else
if (x > 499 && x < 600)
++frequency[4];
else
if (x > 599 && x < 700)
++frequency[5];
else
if (x > 699 && x < 800)
++frequency[6];
else
if (x > 799 && x < 900)
++frequency[7];
else
if (x > 899 && x < 1000)
++frequency[8];
else
if (x > 1000)
++frequency[9];

} //end


cout << "Number of sales people" << setw(24) << "made between \n";
cout << setw(6) << frequency[1] << setw(38) << "$200 - $299\n"
<< setw(6) << frequency[2] << setw(38) << "$300 - $399\n"
<< setw(6) << frequency[3] << setw(38) << "$400 - $499\n"
<< setw(6) << frequency[4] << setw(38) << "$500 - $599\n"
<< setw(6) << frequency[5] << setw(38) << "$600 - $699\n"
<< setw(6) << frequency[6] << setw(38) << "$700 - $799\n"
<< setw(6) << frequency[7] << setw(38) << "$800 - $899\n"
<< setw(6) << frequency[8] << setw(38) << "$900 - $999\n"
<< setw(6) << frequency[9] << setw(39) << "Over $1000 !\n" << endl;

return 0;

} // end main


int cs(int x) // function calculate salary
{
int y = 0;
y = (x * .09 + 200);


return y;
} // end function
Gatre
ok, the code is a little different than I'm seeing in school myself but this is what is bugging me:

cout <<"Enter sales persons total sales for this week.\n"; // get first salary
cin >> x;
while ( c <=100 && x != -1 ) {


then you say

cout <<"Enter sales persons total sales for this week.\n";
cin >> x;

Your asking for the total sales twice.

Also I'm not familiar with the
using std::setw;

Can you be more specific with whats not working....amounts...numbers??

patch
I will try to the numbers are all corect this program prints out in a tubular fashion

number of sales people made between
1 $200-$299
4 $300-$399
0 $400-$499
1 $500-$599
0 $600-$699
0 $700-$799
0 $800-$899
0 $900-$999
1 Over $1000
when I enter 200
299
301
305
307
1000
the math is good sales * .09 + 200
the problem is my if else statement are not picking up the corect answers and adding them to my arry (int frequency(arraysize))

instead I am getting

number of sales people made between
5 $200-$299
0 $300-$399
0 $400-$499
0 $500-$599
0 $600 $699
0 $700-$799
0 $800-$899
0 $900-$999
9 Over $1000
I cant figure were this garbage variable is comming from (9)
patch
oh ya the c < 100 is so it will auto calculate when one hundred sales people are inputted -1 is the sentinal value.
Gatre
I think I see the problem. It is in your If else section. The way its written it is nesting everything or something. Try this in place of the if else:

if (x > 199 && x < 300) {
++frequency[1];
}

if (x > 299 && x < 400) {
++frequency[2];
}

and so on down the line. Or another option would be to use a switch command.
patch
thx so much I will try it and let you know
patch
I was still getting incorect calculations for some reason the last data I put in calculated twice (or just added two to corasponding frequency)I slimmed it down like you sugested by taking out the second request for salary and even padded on to the aray to try and catch any values that met no criteria by using an else statement at the end and putting garbage into frequency[0] I had to hand it in last night I asked the professor for help solving the error if ya wana no what it was I will post it here. ty for you help
Gatre
Sure thing, how'd you fix it. Always good to see solutions to problems presented on the boards.
patch
CODE
// CISED242wk5LKinney
// Calculating Salary
// this program works as far as storing frequencies in an aray but I am getting
// incorect data for sales of 10,000 for some reason this is adding two to frequency[9]
// please help me understand what is wrong I know you dont always have time for every student but
// I have gone over this program hundreds of times and cant find the error my e-mail address is
// lesbone2003@yahoo.com please help I realy want to be a better programmer
#include <iostream>
#include <iomanip>

using std::cin;
using std::cout;
using std::setw;
using std::endl;






int main()
{   //start main
int y;
int c = 0;
int x=0;
const int arraysize = 11;
int frequency[arraysize] = {0,0,0,0,0,0,0,0,0,0,0};

while ( x != -1 ) { //allows 100 sales people

 cout <<"Enter sales persons total sales for this week.\n";  // get first salary
 cin >> x;
 
 
 
if (x != -1){
y = (x * .09) + 200;

}  //end if

if (y > 199 && y < 300){
 ++frequency[1];
}//end if
if (y > 299 && y < 400){
  ++frequency[2];
}//end if
if (y > 399 && y < 500){
   ++frequency[3];
}//end if
if (y > 499 && y < 600){
    ++frequency[4];
}//end if
if (y > 599 && y < 700){
    ++frequency[5];
}//end if
if (y > 699 && y < 800){
     ++frequency[6];
}//end if
if (y > 799 && y < 900){
     ++frequency[7];
}//end if
if (y > 899 && y < 1000)
    ++frequency[8];
if (y > 1000 || y == 1000)
                   ++frequency[9];
else
++frequency[0]; // this is an attempt to catch the garbage value that doesn't fit any of the criteria
// such as -1 that would give a value under 200 // didn't work




}// while

cout << "Number of sales people" << setw(24) << "made between \n";
cout << setw(6) << frequency[1] << setw(38) << "$200 - $299\n"
 << setw(6) << frequency[2] << setw(38) << "$300 - $399\n"
 << setw(6) << frequency[3] << setw(38) << "$400 - $499\n"
 << setw(6) << frequency[4] << setw(38) << "$500 - $599\n"
 << setw(6) << frequency[5] << setw(38) << "$600 - $699\n"
 << setw(6) << frequency[6] << setw(38) << "$700 - $799\n"
 << setw(6) << frequency[7] << setw(38) << "$800 - $899\n"
 << setw(6) << frequency[8] << setw(38) << "$900 - $999\n"
 << setw(6) << frequency[9] << setw(39) << "Over $1000 !\n" << endl;

return 0;

}  // end main
this is as close as I got still had some problem got full credit though rolleyes.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2010 Invision Power Services, Inc.