Need help for C++ programming

ladyluck4772's Avatar, Join Date: Mar 2010
Go4Expert Member
Hi,
My name is Lori and I am in beginning programming and need help with an assignment. The program is C++ and the problem is listed below. I have tried everything I can think of but the program run indefinitely. Please help!

Create a program that displays the registration information for programming seminars. 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.
Code:
 
Number of registrants                Charge per person ($)
1-3                                           150
4-9                                           100
10 or more                                90
Use a while loop to allow the user to enter the number of people a company registers. The program should allow the user to enter the number registered for as many companies as desired. Be sure to use an appropriate sentinel value. The program should display the total number of people registered, the total charge, and the average charge per registrant. (For example, if one company registers 4 people and a second company registers 2 people, then the total number of people registered is 6, the total charge is $700, and the average charge per registrant is $116.67.

Here is my program (that does not work):

Code:
#include <iostream>
#include <iomanip>
 
using std::cout;
using std::cin;
using std::endl;
using std::fixed;
using std::setprecision;
 
int main()
{

 //declare variables
 char company = ' ';
 int registrantCount = 0;
 double chargePerson = 0.0;
 double totalCharge = 0.0;
 double averageCharge = 0.0;
 double subcharge = 0.0;
 
 //convert variable types
 static_cast <double> (registrantCount);
 cout << fixed << setprecision (2);
 
 //enter input data
 cout << "Enter company (S to stop): " << endl;
 cin >> company;
 
 //start loop
 while (company != 'S' || company != 's')
 {
   cout << "Enter number of registrants: " << endl;
   cin >> registrantCount;
   cout << endl << endl;  
  if (registrantCount > 0 && registrantCount <=3)
   subcharge = registrantCount * 150;
  else if (registrantCount > 3 && registrantCount < 10)
   subcharge = registrantCount * 100;
  else if (registrantCount >= 10)
   subcharge = registrantCount * 90;
  else
   cout << "Incorrect input" << endl;
  //end ifs
Thank you for your help.

Last edited by shabbir; 15Mar2010 at 11:31.. Reason: Code blocks
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
You've only posted half the code. Please post the entire program.
0
ladyluck4772's Avatar, Join Date: Mar 2010
Go4Expert Member
Whoops! That would be a problem. Also the program does not end when the user enters "S" or "s" and when I enter 3 under the number of registrants, the computer gives an error message. The program just isn't working right and I am at my wits end.

Here is the entire program:
Code:
 
int main()
{

 //declare variables
 char company = ' ';
 int registrantCount = 0;
 double chargePerson = 0.0;
 double totalCharge = 0.0;
 double averageCharge = 0.0;
 double subcharge = 0.0;
 
 //convert variable types
 static_cast <double> (registrantCount);
 cout << fixed << setprecision (2);
 
 //enter input data
 cout << "Enter company (S to stop): " << endl;
 cin >> company;
 
 //start loop
 while (company != 'S' || company != 's')
 {
   cout << "Enter number of registrants: " << endl;
   cin >> registrantCount;
   cout << endl << endl;  
  if (registrantCount > 0 && registrantCount <=3)
   subcharge = registrantCount * 150;
  else if (registrantCount > 3 && registrantCount < 10)
   subcharge = registrantCount * 100;
  else if (registrantCount >= 10)
   subcharge = registrantCount * 90;
  else
   cout << "Incorrect input" << endl;
  //end ifs
 
  registrantCount += registrantCount;
  totalCharge = totalCharge + subCharge;
  cout << "Enter next company (S to stop): " << endl;
  cin >> company;
 } //end while
 
 //calculate average
 averageCharge = totalCharge/registrantCount;
 
 //display output items
 cout << "Total number of registrants: " << registrantCount << endl;
 cout << "Total charge: " << totalCharge << endl;
 cout << "Average charge per person: " << averageCharge << endl;

 return 0;
}   //end of main function

Last edited by shabbir; 16Mar2010 at 10:20.. Reason: Code blocks
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
For what values of company do you think company != 'S' || company != 's' will evaluate FALSE? Think about it carefully. "S or s" is *not* the correct answer.
0
ladyluck4772's Avatar, Join Date: Mar 2010
Go4Expert Member
Would it be "S && s"?
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
No, it would be a list of values. For instance if you thought the answer was "a, b and c", then you would answer "a, b and c", or "a, b or c" - exact syntax doesn't matter. So "S && s" is just as wrong as "s or S".

In thinking carefully, split the equation into two parts and evaluate each part, then evaluate the overall. So if you are considering 'z' for example, first evaluate "company != 'Z'" (true), then evaluate "company != 'z'" (false), then put them together and evaluate "true or false" (true). The obvious place to start is with S and s, so what is "company != 'S'", "company != 's'", and the results of those OR-ed together.

Also lookup the word "carefully" in the dictionary. Your response was anything but a carefully thought out answer: you just assumed I was being pedantic about syntax, which I wasn't.
0
virxen's Avatar, Join Date: Nov 2009
Pro contributor
loop problem :
while (company != 'S' || company != 's'){...code...}

this mean:
while the statement (company != 'S' || company != 's') is true execute the code

company!='S' means if company is not equal with S
company != 's' means if company is not equal with s
|| means or.if any of the statements is true execute the code.


lets see what happens if i enter S for company.
1) company!='S' ---->false
2) company != 's' ---->true
3) or --->true,continue executing the code.

solution
=========
&& means and.if both statements are true execute the code.
so from the above we get
while (company != 'S' && company != 's')
0
ladyluck4772's Avatar, Join Date: Mar 2010
Go4Expert Member
xpi0t0s,

I truly meant no disrespect and did not mean to leave the impression that I am not trying to understand all of this. The fact is, is that I have been going over this same program for over a week with very little help from my teacher. It is all starting to look the same and my head starts swimming everytime I look at it. Maybe I just need to delete it all and start over. I appreciate you trying to help and explain things, but I didn't really understand the way that you went about it. Thank you for your help. Would you like to help me start it from the beginning again?
0
ladyluck4772's Avatar, Join Date: Mar 2010
Go4Expert Member
virxen,
Thank you, I think I get that part now.
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
No, I don't think you should start it from the beginning again. I was just trying to help you think through the logic: whenever you are using negative logic it gets confusing quickly, particularly because English works the way you did it, so for example "it's not a donkey or a sheep" is actually expressed as "it's not a donkey AND it's not a sheep". Every animal, including donkeys and sheep, is "not a donkey OR not a sheep":

cat: not a donkey=TRUE; not a sheep=TRUE; TRUE+TRUE=TRUE (+=OR)
donkey: not a donkey=FALSE; not a sheep=TRUE; FALSE+TRUE=TRUE
sheep: not a donkey=TRUE; not a sheep=FALSE; TRUE+FALSE=TRUE
shonkey: not a donkey=FALSE; not a sheep=FALSE; FALSE+FALSE=FALSE

so the expression only evaluates FALSE for an animal that is both a donkey and a sheep, i.e. "not a donkey" and "not a sheep" are both FALSE.

So that is why your loop would never exit. Every character you entered was not S or not s, including both S and s.