Need help for C++ programming

Discussion in 'C++' started by ladyluck4772, Mar 15, 2010.

Joined:
Mar 15, 2010
Messages:
15
0
Trophy Points:
0
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:

Last edited by a moderator: Mar 15, 2010
2. xpi0t0sMentor

Joined:
Aug 6, 2004
Messages:
3,009
203
Trophy Points:
63
Occupation:
Senior Support Engineer
Location:
England
You've only posted half the code. Please post the entire program.

Joined:
Mar 15, 2010
Messages:
15
0
Trophy Points:
0
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```

Last edited by a moderator: Mar 16, 2010
4. xpi0t0sMentor

Joined:
Aug 6, 2004
Messages:
3,009
203
Trophy Points:
63
Occupation:
Senior Support Engineer
Location:
England
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.

Joined:
Mar 15, 2010
Messages:
15
0
Trophy Points:
0
Would it be "S && s"?

6. xpi0t0sMentor

Joined:
Aug 6, 2004
Messages:
3,009
203
Trophy Points:
63
Occupation:
Senior Support Engineer
Location:
England
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.

7. virxenActive Member

Joined:
Nov 24, 2009
Messages:
387
90
Trophy Points:
28
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')

Joined:
Mar 15, 2010
Messages:
15
0
Trophy Points:
0
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?

Joined:
Mar 15, 2010
Messages:
15
0
Trophy Points:
0
virxen,
Thank you, I think I get that part now.

10. xpi0t0sMentor

Joined:
Aug 6, 2004
Messages:
3,009
203
Trophy Points:
63
Occupation:
Senior Support Engineer
Location:
England
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.

11. xpi0t0sMentor

Joined:
Aug 6, 2004
Messages:
3,009
203
Trophy Points:
63
Occupation:
Senior Support Engineer
Location:
England
Sorry I can be a bit abrasive at times. Don't take it personally.

12. xpi0t0sMentor

Joined:
Aug 6, 2004
Messages:
3,009
203
Trophy Points:
63
Occupation:
Senior Support Engineer
Location:
England
company is a single char, not a string: do you mean that? But the program will work as long as you only enter single character company names.

Entering "foo" drops the program into an infinite loop; this will be because of the nature of formatted input. The 'f' will be read into company, but thereafter cin seems to get stuck; the next "cin>>company" doesn't change company which permanently remains at 'f', and of course 'o' isn't a valid number so that won't convert it, and maybe cin is in an error state which has to be cleared before it'll continue. Not sure if this is expected behaviour. Experienced programmers quickly drop cin and scanf in favour of more flexible inputs: fgets() reads a complete line from the user and you can then parse it yourself to extract whatever information you want (and you can still do formatted input with strstream and sscanf if you want).

subCharge undeclared identifier: because you declared it subcharge and C++ is case sensitive.

Entering a,23,b,14,s displays

Total number of registrants: 28
Total charge: 3330.00
Average charge per person: 118.93

That's wrong: registrant count should be 37. Ah:
Code:
```	registrantCount += registrantCount;
```
(By the way, when posting code, please use code blocks. It preserves the formatting and makes it easier to read.)

You need a second variable for the total number of registrants. This line simply doubles what is in registrantCount, which is why the result is 28 (=14+14). Fixed the following lines:
Code:
```	int registrantCount = 0, totalRegistrants=0;
totalRegistrants += registrantCount;
averageCharge = totalCharge/totalRegistrants;
cout << "Total number of registrants: " << totalRegistrants << endl;
```
Now the output is
Total number of registrants: 37
Total charge: 3330.00
Average charge per person: 90.00

Haven't checked total and average charges but that's probably correct now.

Joined:
Mar 15, 2010
Messages:
15
0
Trophy Points:
0
Re: Program won't run right

I actually think I understand that now. I did rewrite the program at work today. However, when I tried to execute it, the program does nothing except display "press any key to continue." I do and the window closes. I'm not sure if I did something wrong or if my Microsoft Visual Studio program is acting up. Would someone look at my program and tell me if I have done anything to keep it from running. See program below:
Code:
```
//Ch7AppE04.cpp
//Displays registration information
//Created/revised by <your name> on <current date>
#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; //accumulator
double averageCharge = 0.0;
int y = 0;     //counter
double subCharge = 0.0;
//convert variable types
static_cast <double> (registrantCount);
cout << fixed << setprecision (2);
//enter company
cout << "Enter company: ";
cin >> company;
//enter number of registrants
while (company != 'S' && company != 's')
{
cout << "Enter number of registrants: ";
cin >> registrantCount;
//choose cost per registrant
if (registrantCount > 0 && registrantCount < 4)
{
chargePerson = 150;
subCharge = registrantCount * chargePerson;
totalCharge = totalCharge + subCharge;
}
else if (registrantCount >= 4 && registrantCount <= 9)
{
chargePerson = 100;
subCharge = registrantCount * chargePerson;
totalCharge = totalCharge + subCharge;
}
else if (registrantCount >= 10)
{
chargePerson = 90;
subCharge = registrantCount * chargePerson;
totalCharge = totalCharge + subCharge;
}
else
{
cout << "Incorrect data" << endl;
y += 1;
}
//end ifs
} //end while
//display output items
cout << "Total number of registrants: " << y << endl;
cout << "Total amount charged: " << totalCharge << endl;
average = totalCharge/y;
cout << "Average charge per person: " << average << endl;
return 0;
}   //end of main function```

Last edited by a moderator: Mar 17, 2010
14. virxenActive Member

Joined:
Nov 24, 2009
Messages:
387
90
Trophy Points:
28
changes you must make and why
==========================

Code:
```#include <iostream>
#include <iomanip>
[COLOR=Red]using namespace std;//use this method its better[/COLOR]

int main(){
//declare variables
[COLOR=Red]char company1[80];//use this trick to avoid problems with input data
char company=' ';[/COLOR]
int registrantCount = 0;  [COLOR=Red]//it holds all registrants from all companies[/COLOR]
[COLOR=Red]int registrants = 0;  //it holds the registrants for a single company[/COLOR]
double chargePerson = 0.0;
double totalCharge = 0.0; //accumulator
double averageCharge = 0.0;
[COLOR=Red]//int y = 0;     //counter you do not need it[/COLOR]
double subCharge = 0.0;

//convert variable types
static_cast <double> (registrantCount);
cout << fixed << setprecision (2);

//enter number of registrants
while (company != 'S' && company != 's'){
//enter company
[COLOR=Red]     cout << "Enter company: ";//inside the while we must read again the company name otherwise there is no chance the program to exit some time
[/COLOR][COLOR=Red] cin >> company1;//we read a string[/COLOR]
[COLOR=Red]     company=company1[0];//and take only the first char,so if i enter company-->abc company will be a only[/COLOR]
[COLOR=Red]     if (company != 'S' && company != 's'){//we use it again in order not to proccess the code below since the program must stop if company='S' or 's'[/COLOR]
cout << "Enter number of registrants: ";
cin >> registrants;
//choose cost per registrant
if ([COLOR=Red]registrants[/COLOR] > 0 && [COLOR=Red]registrants[/COLOR] < 4){
chargePerson = 150;
[COLOR=Red]registrantCount+=registrants;//with this we find the total registrants for all companies
subCharge = registrants * chargePerson;[/COLOR]
totalCharge+=subCharge;
}
else if ([COLOR=Red]registrants[/COLOR] >= 4 && [COLOR=Red]registrants[/COLOR]<= 9){
chargePerson = 100;
[COLOR=Red]registrantCount+=registrants;[/COLOR]
subCharge = [COLOR=Red]registrants[/COLOR]* chargePerson;
totalCharge+=subCharge;
}
else if (registrants>= 10) {
chargePerson = 90;
[COLOR=Red]registrantCount+=registrants;[/COLOR]
[COLOR=Black]subCharge = [COLOR=Red]registrants[/COLOR]*chargePerson;[/COLOR]
totalCharge +=subCharge;
}
[COLOR="Red"]         else if (cin.fail()){//with this you trap errors related with bad input,string instead of number
cerr << "Incorrect data!!!" << endl;
cin.clear();
cin.ignore(numeric_limits<int>::max(),'\n');
}[/COLOR]
//end ifs
}
} //end while
//display output items
cout << "Total number of registrants: " << [COLOR=Red]registrantCount[/COLOR] << endl;
cout << "Total amount charged: " << totalCharge << endl;
[COLOR=Red]averageCharge [/COLOR]= totalCharge/[COLOR=Red]registrantCount[/COLOR];
cout << "Average charge per person: " << [COLOR=Red]averageCharge [/COLOR]<< endl;
return 0;
}   //end of main function

```

Last edited: Mar 17, 2010
15. xpi0t0sMentor

Joined:
Aug 6, 2004
Messages:
3,009
203
Trophy Points:
63
Occupation:
Senior Support Engineer
Location:
England
(1) Would you please use CODE BLOCKS when posting code. Shabbir is nicely doing that for you but please do it yourself. You have read the posting guidelines, haven't you? If not, do it now; they are described there.

(2) The code as presented doesn't compile so I'm not sure what you think you're running. average is not defined so
Code:
```average = totalCharge/y;
```
throws an error.

Joined:
Mar 15, 2010
Messages:
15
0
Trophy Points:
0
My apologies for not using code blocks. Sometimes I do skip over the rules. I will try to do better. Many thanks to Shabbir for doing what I should have been doing. OK, as per previous posts, the company is supposed to be on character, namely a number.

Dear Virxen,
You have been very helpful. However, alot of the changes you have made, we have not covered yet in class. I am a beginning programmer and I'm not sure how my teacher would react to alot of the terms you have used. The farthest we have gotten is using "if, switch, else if, while, and do while." Is there a simpler way to do this? I do not know what the things listed in the block are (if I do the block right, lol). And yes, average is a variable that I forgot to declare. Thank you for your help.

Lori

Code:
```//Code Block
[list]

[*] Item 1 using namespace std;

[*] Item 2 char company1[80];

[*] Item 3 else if (cin.fail()){//with this you trap errors related with bad input,string instead of number
cerr << "Incorrect data!!!" << endl;
cin.clear();
cin.ignore(numeric_limits<int>::max(),'\n');
}

[/list]
```
I'm sorry, but I cannot figure out how to block the code.

Last edited by a moderator: Mar 20, 2010

Joined:
Jul 12, 2004
Messages:
15,369
386
Trophy Points:
83
Done the code issue for you and you can see that here

18. virxenActive Member

Joined:
Nov 24, 2009
Messages:
387
90
Trophy Points:
28

1) change this to
Code:
```using std::cout;
using std::cin;
using std::endl;
using std::fixed;
using std::setprecision;
```
2) remove it and change code like this

Code:
```char company=' ';
.................
//enter number of registrants
while (company != 'S' && company != 's'){
//enter company
registrants=0;
cout << "Enter company: ";//inside the while we must read again the company name otherwise there is no chance the program to exit some time
cin >> company;//we read a string
[COLOR=Red]getchar();[/COLOR]//if you don't want this also, then no solution
.................
```
3) no other solution --->if you press char instead of number then you can not avoid errors.

Code:
```string a;
```
for example?

Joined:
Mar 15, 2010
Messages:
15
0
Trophy Points:
0
No, we haven't learned about "string a;" yet.

20. itstimetojazzNew Member

Joined:
Sep 9, 2009
Messages:
8
0
Trophy Points:
0
Hi

Here is the program for you
enjoy!!!
Code:
```#include "stdafx.h"
#include <iostream.h>

int main(int argc, char* argv[])
{
int numofcompanies;
int i=0;

int totalamount=0;
int totalregistrants=0;

cout<<"Enter the number of companies: ";
cin>>numofcompanies;
while(i<numofcompanies)
{
i++;
int amount1=0;
int amount2=0;
int amount3=0;
int numofreg=0;

cout<<"Enter the number of people suppose to register in Company "<<i<< endl;
cin>>numofreg;
if(numofreg>=1 && numofreg<=3)
{
amount1= numofreg * 150;
}
else if(numofreg>=4 && numofreg <=9)
{
amount2=numofreg * 100;
}
else if(numofreg>=10)
{
amount3=numofreg * 90;
}

totalamount=totalamount+ amount1 + amount2 + amount3; //400
totalregistrants=totalregistrants + numofreg; //4

}

if (i==numofcompanies)
{
cout<<"Total number of registrants are "<<totalregistrants<<endl;
cout<<"Total Amount is "<<totalamount<<"\$"<<endl;
float averageamount= totalamount / totalregistrants;
cout<<"Average amount is "<<averageamount<<"\$" <<endl;

}

return 0;

}```

Last edited by a moderator: Mar 23, 2010