Sunday, August 2, 2009

Can someone help me w/ this program in C++, it is for school.?

Lab 6 Seminar Registration





Create a C++ program that displays the registration information for a programming seminar. The price per person depends on the number of people a company registers. (For example, if a company registers four people, then the amount owed by that company is $400). The following chart shows the charges per registrant.





Number of Registrants Charge per person ($)





1 – 3 150


4 – 9 100


10 or more 90





The program should allow the user to enter the number registered for as many companies as desired. The program should display the total number of people registered, the total charge, and the average charge per registrant.





Sample run:





Enter the number of people registered: 4


Enter the next number of people registered: 2





Output:





The total number of people registered is 6.


The total charge is $700.


The average charge per registrant is $116.67

Can someone help me w/ this program in C++, it is for school.?
#include %26lt;iostream%26gt;


#include %26lt;iomanip%26gt;


#include %26lt;string%26gt;


using namespace std;





int main()


{


int attendees = 0, totalPrice = 0, classSize = 0;


float aveCharge = 0.0;


string str;





while(true)


{


cout %26lt;%26lt; "Enter the number of people registered from company or 'done' to stop: ";


getline(cin, str);





if (str == "done")


break;


else


attendees = atoi(str.c_str());





classSize += attendees;





if (attendees %26lt;= 3)


{


totalPrice += (attendees * 150);


}


else if (attendees %26lt;= 9)


{


totalPrice += (attendees * 100);


}


else


{


totalPrice += (attendees * 90);


}


}





aveCharge = (float) totalPrice / classSize;





cout %26lt;%26lt; "The total number of people registered is " %26lt;%26lt; classSize %26lt;%26lt; endl;


cout %26lt;%26lt; "The total charge is " %26lt;%26lt; totalPrice %26lt;%26lt; endl;


cout %26lt;%26lt; "The average charge per registrant is " %26lt;%26lt; fixed %26lt;%26lt; setprecision(2) %26lt;%26lt; aveCharge %26lt;%26lt; endl;





return(0);


}
Reply:/* I don't know how you were supposed to solve this (ie, what you already know) but here is a simple solution I used. I took a vector (basically acts like an expandable array) and filled it with values. I then loop thru each of the elements in the vector (the vector%26lt;T%26gt;.at() function accesses the element at that position) and I test for the total charge based on that number. */





#include %26lt;iostream%26gt;


#include %26lt;vector%26gt;





int main()


{


std::vector%26lt;int%26gt; peopleRegd; // vector of people registered


int people; // temp input variable





int totalCharge=0;


int totalPeopleRegd=0;





std::cout %26lt;%26lt; "Enter the number of people registered, and enter 0 to quit." %26lt;%26lt; std::endl;





do


{


std::cout %26lt;%26lt; "Enter the number of people registered:";


std::cin %26gt;%26gt; people;


peopleRegd.push_back(people);


} while (people != 0);





// loop thru each registered person


for(int i=0;i %26lt; peopleRegd.size(); i++)


{


totalPeopleRegd += peopleRegd.at(i); // add to total person count





if(peopleRegd.at(i) %26gt;= 1 %26amp;%26amp; peopleRegd.at(i) %26lt;= 3)


totalCharge += 150*peopleRegd.at(i);


else if(peopleRegd.at(i) %26gt;= 4 %26amp;%26amp; peopleRegd.at(i) %26lt;= 9)


totalCharge += 100*peopleRegd.at(i);


else


totalCharge += 90*peopleRegd.at(i);


}





std::cout %26lt;%26lt; "The total number of people registered is " %26lt;%26lt; totalPeopleRegd %26lt;%26lt; std::endl;


std::cout %26lt;%26lt; "The total charge is $" %26lt;%26lt; totalCharge %26lt;%26lt; std::endl;


std::cout %26lt;%26lt; "The average charge per registrant is $" %26lt;%26lt; ((float)totalCharge)/((float)totalPeople... %26lt;%26lt; std::endl;





return 0;


}


No comments:

Post a Comment