0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Sorry I can be a bit abrasive at times. Don't take it personally.
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
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.
0
ladyluck4772's Avatar, Join Date: Mar 2010
Go4Expert Member
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 shabbir; 17Mar2010 at 09:14.. Reason: Code blocks
0
virxen's Avatar, Join Date: Nov 2009
Pro contributor
changes you must make and why
==========================

Code:
#include <iostream>
#include <iomanip>
using namespace std;//use this method its better

int main(){
 //declare variables
 char company1[80];//use this trick to avoid problems with input data
 char company=' ';
 int registrantCount = 0;  //it holds all registrants from all companies
 int registrants = 0;  //it holds the registrants for a single company
 double chargePerson = 0.0;
 double totalCharge = 0.0; //accumulator
 double averageCharge = 0.0;
 //int y = 0;     //counter you do not need it
 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
     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 >> company1;//we read a string
     company=company1[0];//and take only the first char,so if i enter company-->abc company will be a only
     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'
         cout << "Enter number of registrants: ";
         cin >> registrants;
         //choose cost per registrant
         if (registrants > 0 && registrants < 4){
              chargePerson = 150;
              registrantCount+=registrants;//with this we find the total registrants for all companies
              subCharge = registrants * chargePerson;
              totalCharge+=subCharge;
         }
         else if (registrants >= 4 && registrants<= 9){
              chargePerson = 100;
              registrantCount+=registrants;
              subCharge = registrants* chargePerson;
              totalCharge+=subCharge;
         }
         else if (registrants>= 10) {
              chargePerson = 90;
              registrantCount+=registrants;
              subCharge = registrants*chargePerson;
              totalCharge +=subCharge;
         }
         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');
         }
         //end ifs
      }
  } //end while
 //display output items
 cout << "Total number of registrants: " << registrantCount << endl;
 cout << "Total amount charged: " << totalCharge << endl;
 averageCharge = totalCharge/registrantCount;
 cout << "Average charge per person: " << averageCharge << endl;
 return 0;
}   //end of main function

Last edited by virxen; 17Mar2010 at 14:45..
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
(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.
0
ladyluck4772's Avatar, Join Date: Mar 2010
Go4Expert Member
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
  • 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'); }
I'm sorry, but I cannot figure out how to block the code.

Last edited by shabbir; 20Mar2010 at 10:01.. Reason: Code blocks
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Done the code issue for you and you can see that here
0
virxen's Avatar, Join Date: Nov 2009
Pro contributor
Quote:
Originally Posted by ladyluck4772 View Post
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
  • 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'); }
I'm sorry, but I cannot figure out how to block the code.



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
     getchar();//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.


did you learn about

Code:
string a;
for example?
0
ladyluck4772's Avatar, Join Date: Mar 2010
Go4Expert Member
No, we haven't learned about "string a;" yet.
0
itstimetojazz's Avatar, Join Date: Sep 2009
Light Poster
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 shabbir; 23Mar2010 at 12:45.. Reason: Code blocks