![]() |
|
|
#1 | |
![]() |
Need some help with entry C++
Well, the title is simple. I have an intro C++ assignment that for some reason, will not work. I am just not sure why it isn't.
Here is a background: I have to build a program (win32) to show a list of people attending a conference. Here is the price: 1-3: 150.00 per person 4-9: 100.00 per person 10+: 90.00 per person Now, i need the user to keep inputing people (from different companies) untill they tell it to stop. (I am required to use a while loop). The program is suppost to calculate the total number of people entered, and display the average cost per person. What I keep getting is the number of people to equal -1, and cost of 0. I'm about to stop my foot and swear some more, but not sure how that would help. If anyone can give me any tips on how to get this to work. I would be most grateful. Just as a note. I'm not asking you do it for me, I wont learn anything that way. I want to understand the whos and why, otherwise I'd just be a cheating P.O.S. Thanks alot TPU, -Andrew I have to do this in Visual Studio C++ Express 2005. And here is my code so far: Quote:
__________________
**||** “I'm not angry. Just tired of the yuppie schmucks blogging every time they take a dump on their mackbooks. And acting like they are 1337 cause they have a Mac.” -Steevo
"Design is the only field where Science and Art merge"- Lucille Tenazas |
|
|
|
|
|
|
#2 |
![]() |
you're getting -1 each time because you are setting the registrants variable to -1 each time when you break from the while loop. You need another int that keeps a count of the registrants.
Then in your loop you'd do something like Code:
count += registrants |
|
|
|
| The Following User Says Thank You to JoJo_Whit3 For This Useful Post: |
|
|
#3 |
![]() |
Thanks. I'll give that a try
EDIT: Ok, I tried this and got the total people. It is a start. [QUOTE = changed this} //Lab 2 - ITCS 1214 //Created and revised by Andrew Lichtenhan #include <iostream> #include <iomanip> using std::cout; using std::cin; using std::endl; using std::fixed; using std::setprecision; int main() { //declare variables int registrants = 0; double price = 0.0; double charge = 0.0; double cost = 0.0; double totalRegistrants = 0.0; //find how many people are attending cout << "How many company registrants will be attending? (Enter -1 to stop): "; cin >> registrants; while (registrants >= 0) { //make sure at least one person attends charge = registrants * price; totalRegistrants = totalRegistrants + registrants; //get remaining temperatures cout << "...and in addition to that company?: "; cin >> registrants; } //end while //make if/else for price ranges if (registrants >=1 && registrants <= 3) price = (registrants * 150.00); else if (registrants >=4 && registrants <= 9) price = (registrants * 100.00); else if (registrants >=10) price = (registrants * 90.00); { //calculate and display the average price per person cost = registrants * price; charge = cost / totalRegistrants; cout << "Total Registrants: " << totalRegistrants << endl; cout << "Average Price: " << charge << endl; } return 0; } //end of main function [/QUOTE]
__________________
**||** “I'm not angry. Just tired of the yuppie schmucks blogging every time they take a dump on their mackbooks. And acting like they are 1337 cause they have a Mac.” -Steevo
"Design is the only field where Science and Art merge"- Lucille Tenazas |
|
|
|
|
|
#4 |
![]() |
crap. I revised code. Newest code is at the top post now. Will reply correctly next time.
Anyway. I got the correct number to count with my change, now I just need the adverage price to display. Im getting -0 now.
__________________
**||** “I'm not angry. Just tired of the yuppie schmucks blogging every time they take a dump on their mackbooks. And acting like they are 1337 cause they have a Mac.” -Steevo
"Design is the only field where Science and Art merge"- Lucille Tenazas |
|
|
|
|
|
#5 |
![]() |
you're almost there...you just need to edit this code
Code:
//make if/else for price ranges
if (registrants >=1 && registrants <= 3)
price = (registrants * 150.00);
else if (registrants >=4 && registrants <= 9)
price = (registrants * 100.00);
else if (registrants >=10)
price = (registrants * 90.00);
{
//calculate and display the average price per person
cost = registrants * price;
charge = cost / totalRegistrants;
|
|
|
|
| The Following User Says Thank You to JoJo_Whit3 For This Useful Post: |
|
|
#6 |
![]() |
Holy crap. This is where my mind falls apart. I know I'm staring right at it, and it is flowing well in my head, but not to the compy. grr.
You mentioned I need to do a comparison with the totalRegisters. Used -1 just because it is an easy negitive numer to end my loop. This is what I attribute to my lack of math skills. Hence I hope to do shader graphics someday, and chartactor modeling. Because art and programming are 2 totally different sides of the brain. I'll update when I think I found something...or get frustrated as hell. EDIT: You mentioned registrants coming out to -1. Is this due to the code, which you posted, coming up with that, or is it due to the way I initialed my variables to begin with. I feel i just hit the brick wall and am stuck looking into it.
__________________
**||** “I'm not angry. Just tired of the yuppie schmucks blogging every time they take a dump on their mackbooks. And acting like they are 1337 cause they have a Mac.” -Steevo
"Design is the only field where Science and Art merge"- Lucille Tenazas Last edited by Atnevon; Jun 27, 2008 at 02:31 AM. |
|
|
|
|
|
#7 | |
![]() |
Ok...first off since you're using Visual Studio, you should learn to set breakpoints and step through code. This will help you out a lot when debugging in the future. If you don't know how to do this, do a quick google search and you should be able to find a tutorial.
Quote:
Code:
while (registrants >= 0)
{
//make sure at least one person attends
charge = registrants * price;
totalRegistrants = totalRegistrants + registrants; // accumulated total
//get remaining temperatures
cout << "...and in addition to that company?: ";
cin >> registrants; // *** registrants gets set to -1 here by the user to break from while loop
} //end while
Code:
//make if/else for price ranges if (registrants >=1 && registrants <= 3) price = (registrants * 150.00); you want to do the comparisons with the totalRegistrants entered...not just the last value entered by the user to do this, the code would look like Code:
if (totalRegistrants >=1 && totalRegistrants <= 3) ... |
|
|
|
|
| The Following User Says Thank You to JoJo_Whit3 For This Useful Post: |
|
|
#8 |
|
Hardcore Monkey Moderator
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,129 (5.27/day)
Thanks: 591
Thanked 5,494 Times in 2,938 Posts
|
Atn, why are you doing "charge = registrants * price" in your while loop?
When the while loop executes, price = 0, so charge will always be zero. It looks like you don't need that line at all.
__________________
Cloud (noun, singular): A dynamic arrangement of multiple potential single points of failure, with a user at one end and their data at the other. Get more tech news on a wide variety of topics at NextPowerUp
|
|
|
|
|
|
#9 |
|
Hardcore Monkey Moderator
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,129 (5.27/day)
Thanks: 591
Thanked 5,494 Times in 2,938 Posts
|
Here's another little tip that may help you out in the future.
Don't use variables to perform multiple functions. For instance you are using the registrants variable to count people and also to determine the end of input. You should try to avoid this as it complicates things. Expecially when your code gets more complex. I would create a char variable called something like "Completed". Then do a while loop with that variable... Code:
Char Completed = 'N';
Int Registrants = 0;
Int TotalRegs = 0;
while (Completed == 'N')
{
cout << "How many to register?"
cin >> registrants;
TotalRegs = TotalRegs + Registrants;
cout << "Are you done? (Y or N)"
cin >> Completed;
}
// continue program .....
I write almost exclusively in C#, so forgive me if my C++ syntax is a little off base
__________________
Cloud (noun, singular): A dynamic arrangement of multiple potential single points of failure, with a user at one end and their data at the other. Get more tech news on a wide variety of topics at NextPowerUp
Last edited by Kreij; Jun 27, 2008 at 06:29 PM. |
|
|
|
| The Following User Says Thank You to Kreij For This Useful Post: |
|
|
#10 |
![]() |
You might want a == in that while loop Kreij
It'll compile and run but won't work XD
|
|
|
|
|
|
#11 |
|
Hardcore Monkey Moderator
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,129 (5.27/day)
Thanks: 591
Thanked 5,494 Times in 2,938 Posts
|
LOL .. thanks Oliver, I always used it in C# but was not sure if C++ used the same syntax.
Too lazy to try it. FIXED !!! Like I said, it's been awhile since I've done any C++ programming.
__________________
Cloud (noun, singular): A dynamic arrangement of multiple potential single points of failure, with a user at one end and their data at the other. Get more tech news on a wide variety of topics at NextPowerUp
|
|
|
|
|
|
#12 | |
![]() |
Well, holy hell. I finally, by some miracle got it to work. I am going to study it in a week or so when I have the time to really sit and ponder. This is the last week of class, and have to crank another program out. As one of my coworkers suggested:
"If it ghostly magically works, don;t F$%K with it. Wait a bit, and look later. This way you can go full speed in your next project without your last one bugging you." I like the while loop you suggested Kreij. It makes more since to me just looking at it. However, my textbook has not really touched anything like a confirmation series to continue loops, so my instructor might get suspicious that someone did it for me. But I will keep that little set in my mind for when a more appropriate time should come. Here is my code, just to share it with you all. I essential moved and changed some variables, and moved my if statements so they did their job correctly. Thanks agian TPU, -Andrew Quote:
__________________
**||** “I'm not angry. Just tired of the yuppie schmucks blogging every time they take a dump on their mackbooks. And acting like they are 1337 cause they have a Mac.” -Steevo
"Design is the only field where Science and Art merge"- Lucille Tenazas |
|
|
|
|
|
|
#13 | |
|
Hardcore Monkey Moderator
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,129 (5.27/day)
Thanks: 591
Thanked 5,494 Times in 2,938 Posts
|
Great job Atn!!
Quote:
That is what programming is all about. If your instructor has a problem with that, tell him to make an account here on TPU and take us on. We love a challenge
__________________
Cloud (noun, singular): A dynamic arrangement of multiple potential single points of failure, with a user at one end and their data at the other. Get more tech news on a wide variety of topics at NextPowerUp
|
|
|
|
|
| The Following User Says Thank You to Kreij For This Useful Post: |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Thermaltake ProWater PW850i Entry Level Water Cooling Kit | malware | News | 7 | Apr 17, 2008 03:21 PM |
| please help me figure out what this registry entry means... | acousticlemur | General Software | 6 | Jan 21, 2008 10:19 PM |
| Intel Adds New Entry Level Chipsets | malware | News | 5 | Jun 13, 2007 02:07 AM |
| Sapphire Ships New Entry Level Video Cards | malware | News | 10 | Jan 22, 2007 04:07 AM |