|
Mentor
|
![]() |
| 16Mar2010,15:43 | #11 |
|
Sorry I can be a bit abrasive at times. Don't take it personally.
|
|
Mentor
|
![]() |
| 16Mar2010,16:08 | #12 |
|
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; 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; 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. |
|
Go4Expert Member
|
|
| 17Mar2010,09:03 | #13 |
|
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
|
|
Pro contributor
|
![]() |
| 17Mar2010,14:35 | #14 |
|
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.. |
|
Mentor
|
![]() |
| 17Mar2010,18:14 | #15 |
|
(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; |
|
Go4Expert Member
|
|
| 20Mar2010,07:43 | #16 |
|
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
|
|
Pro contributor
|
![]() |
| 22Mar2010,03:43 | #18 |
|
Quote:
Originally Posted by ladyluck4772 1) change this to Code:
using std::cout; using std::cin; using std::endl; using std::fixed; using std::setprecision; 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
.................
did you learn about Code:
string a; |
|
Go4Expert Member
|
|
| 22Mar2010,07:48 | #19 |
|
No, we haven't learned about "string a;" yet.
|
|
Light Poster
|
|
| 22Mar2010,15:26 | #20 |
|
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;
}
|




