techPowerUp! Forums

Go Back   techPowerUp! Forums > Software > Programming & Webmastering

Reply
 
Thread Tools
Old Jun 27, 2008, 01:37 AM   #1
Atnevon
200 Posts
 
Atnevon's Avatar
 
Join Date: Sep 2007
Posts: 430 (0.21/day)
Thanks: 81
Thanked 29 Times in 25 Posts

System Specs

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:
Originally Posted by Code

//Lab 2 - ITCS 1214
//Created and revised by Atnevon

#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;

//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;
registrants = registrants + 1;

//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
total = registrants * price;
charge = cost / registrants;

cout << "Total Registrants: " << registrants << endl;
cout << "Average Price: " << charge << endl;

}
return 0;
} //end of main function

__________________
**||** **||**
“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
Atnevon is offline  
Reply With Quote
Old Jun 27, 2008, 01:52 AM   #2
JoJo_Whit3
25 Posts
 
Join Date: May 2008
Posts: 43 (0.02/day)
Thanks: 2
Thanked 4 Times in 4 Posts

System Specs

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
and then use that count variable to do all of your price calculations at the end
JoJo_Whit3 is offline  
Reply With Quote
The Following User Says Thank You to JoJo_Whit3 For This Useful Post:
Old Jun 27, 2008, 01:55 AM   #3
Atnevon
200 Posts
 
Atnevon's Avatar
 
Join Date: Sep 2007
Posts: 430 (0.21/day)
Thanks: 81
Thanked 29 Times in 25 Posts

System Specs

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
Atnevon is offline  
Reply With Quote
Old Jun 27, 2008, 02:00 AM   #4
Atnevon
200 Posts
 
Atnevon's Avatar
 
Join Date: Sep 2007
Posts: 430 (0.21/day)
Thanks: 81
Thanked 29 Times in 25 Posts

System Specs

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
Atnevon is offline  
Reply With Quote
Old Jun 27, 2008, 02:01 AM   #5
JoJo_Whit3
25 Posts
 
Join Date: May 2008
Posts: 43 (0.02/day)
Thanks: 2
Thanked 4 Times in 4 Posts

System Specs

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;
since registrants is -1, you will never end up in any of those if statements. you need to do the comparisons with the totalRegistrants
JoJo_Whit3 is offline  
Reply With Quote
The Following User Says Thank You to JoJo_Whit3 For This Useful Post:
Old Jun 27, 2008, 02:12 AM   #6
Atnevon
200 Posts
 
Atnevon's Avatar
 
Join Date: Sep 2007
Posts: 430 (0.21/day)
Thanks: 81
Thanked 29 Times in 25 Posts

System Specs

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.
Atnevon is offline  
Reply With Quote
Old Jun 27, 2008, 02:55 AM   #7
JoJo_Whit3
25 Posts
 
Join Date: May 2008
Posts: 43 (0.02/day)
Thanks: 2
Thanked 4 Times in 4 Posts

System Specs

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:
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.
Ok, registrants is going to be -1 because thats how the user breaks from while loop

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
then, the very next thing you do is

Code:
//make if/else for price ranges
if (registrants >=1 && registrants <= 3)
price = (registrants * 150.00);
but registrants was just set to -1 by the user. therefore, you're if/else statements will never be true.

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)
...
hopefully, this helps
JoJo_Whit3 is offline  
Reply With Quote
The Following User Says Thank You to JoJo_Whit3 For This Useful Post:
Old Jun 27, 2008, 04:39 PM   #8
Kreij
Hardcore Monkey Moderator
 
Kreij's Avatar
 
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,129 (5.27/day)
Thanks: 591
Thanked 5,494 Times in 2,938 Posts

System Specs

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
Kreij is online now  
Reply With Quote
Old Jun 27, 2008, 05:10 PM   #9
Kreij
Hardcore Monkey Moderator
 
Kreij's Avatar
 
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,129 (5.27/day)
Thanks: 591
Thanked 5,494 Times in 2,938 Posts

System Specs

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 .....
This way the Registrants variable has only one purpose, to count people.

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.
Kreij is online now  
Reply With Quote
The Following User Says Thank You to Kreij For This Useful Post:
Old Jun 27, 2008, 05:47 PM   #10
Oliver_FF
200 Posts
 
Oliver_FF's Avatar
 
Join Date: Oct 2006
Posts: 478 (0.20/day)
Thanks: 23
Thanked 66 Times in 53 Posts

System Specs

You might want a == in that while loop Kreij It'll compile and run but won't work XD
Oliver_FF is offline  
Reply With Quote
Old Jun 27, 2008, 06:29 PM   #11
Kreij
Hardcore Monkey Moderator
 
Kreij's Avatar
 
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,129 (5.27/day)
Thanks: 591
Thanked 5,494 Times in 2,938 Posts

System Specs

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
Kreij is online now  
Reply With Quote
Old Jun 28, 2008, 12:46 AM   #12
Atnevon
200 Posts
 
Atnevon's Avatar
 
Join Date: Sep 2007
Posts: 430 (0.21/day)
Thanks: 81
Thanked 29 Times in 25 Posts

System Specs

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:
Originally Posted by My Code
//Lab 2 - ITCS 1214
//Created and revised by Atnevon


#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;
double totalPrice = 0.0;

//find how many people are attending
cout << "How many company registrants will be attending?: ";
cin >> registrants;


while (registrants >= 0)
{
//make sure at least one person attends
charge = registrants * price;
totalRegistrants = totalRegistrants + registrants;

//declare if statement
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);

//get additional company registrants
cout << "...and in addition to that company? Enter -1 to stop: ";
cin >> registrants;



//make if/else for price ranges

totalPrice = totalPrice + price;

//calculate and display the average price per person
cost = registrants * price;



}//end while
charge = totalPrice / (totalRegistrants);
cout << fixed << setprecision(2) << endl;
cout << "Total Charge is: $" << totalPrice << endl;
cout << "Total Registrants: " << totalRegistrants << endl;
cout << "Average Price: $" << charge << endl;

return 0;
} //end of main function
__________________
**||** **||**
“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
Atnevon is offline  
Reply With Quote
Old Jun 28, 2008, 02:28 AM   #13
Kreij
Hardcore Monkey Moderator
 
Kreij's Avatar
 
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,129 (5.27/day)
Thanks: 591
Thanked 5,494 Times in 2,938 Posts

System Specs

Great job Atn!!

Quote:
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.
As long as you searched for an answer and got the code working, without a total copy and paste, you are learning. EVERYONE learns from the code of others.
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
Kreij is online now  
Reply With Quote
The Following User Says Thank You to Kreij For This Useful Post:
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

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


All times are GMT. The time now is 09:40 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
no new posts