Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Need help for C++ programming (http://www.go4expert.com/forums/help-cpp-programming-t21360/)

ladyluck4772 15Mar2010 09:44

Need help for C++ programming
 
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. :wacky:

xpi0t0s 15Mar2010 15:31

Re: Need help for C++ programming
 
You've only posted half the code. Please post the entire program.

ladyluck4772 15Mar2010 18:59

Re: Need help for C++ programming
 
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. :wacky:

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


xpi0t0s 15Mar2010 23:28

Re: Need help for C++ programming
 
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.

ladyluck4772 16Mar2010 00:18

Re: Need help for C++ programming
 
Would it be "S && s"?

xpi0t0s 16Mar2010 01:40

Re: Need help for C++ programming
 
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.

virxen 16Mar2010 02:16

Re: Need help for C++ programming
 
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')

ladyluck4772 16Mar2010 04:20

Re: Need help for C++ programming
 
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?

ladyluck4772 16Mar2010 04:21

Re: Need help for C++ programming
 
virxen,
Thank you, I think I get that part now.

xpi0t0s 16Mar2010 15:42

Re: Need help for C++ programming
 
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.


All times are GMT +5.5. The time now is 09:42.