• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

Need some help with entry C++

Joined
Sep 5, 2007
Messages
512 (0.08/day)
System Name HAL_9017
Processor Intel i9 10850k
Motherboard Asus Prime z490-A
Cooling Corsair H115i
Memory GSkill 32GB DDR4-3000 Trident-Z RGB
Video Card(s) NVIDIA 1080GTX FE w/ EVGA Hybrid Water Cooler
Storage Samsung EVO 960 M.2 SSD 500Gb
Display(s) Asus XG279
Case In Win 805i
Audio Device(s) EVGA NuAudio
Power Supply CORSAIR RM750
Mouse Logitech Master 2s
Keyboard Keychron K4
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:

Code said:
//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
 
Joined
May 1, 2008
Messages
47 (0.01/day)
Processor Intel E8400 4GHz @ 1.34v
Motherboard Asus P5Q WS
Cooling Xigmatek S1283
Memory G.Skill Black Pi 2x2GB @ 894MHz 4-4-4-8
Video Card(s) 2 x HD4850 @ 730/1100 Crossfire
Storage Samsung 320GB, 2 x Fujitsu 15K 73GB SCSI RAID 0
Display(s) Soyo 21.6" Widescreen
Case Antec 300
Audio Device(s) Audigy 2 ZS
Power Supply PC P&C Silencer 610W
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
 
Joined
Sep 5, 2007
Messages
512 (0.08/day)
System Name HAL_9017
Processor Intel i9 10850k
Motherboard Asus Prime z490-A
Cooling Corsair H115i
Memory GSkill 32GB DDR4-3000 Trident-Z RGB
Video Card(s) NVIDIA 1080GTX FE w/ EVGA Hybrid Water Cooler
Storage Samsung EVO 960 M.2 SSD 500Gb
Display(s) Asus XG279
Case In Win 805i
Audio Device(s) EVGA NuAudio
Power Supply CORSAIR RM750
Mouse Logitech Master 2s
Keyboard Keychron K4
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]
 
Joined
Sep 5, 2007
Messages
512 (0.08/day)
System Name HAL_9017
Processor Intel i9 10850k
Motherboard Asus Prime z490-A
Cooling Corsair H115i
Memory GSkill 32GB DDR4-3000 Trident-Z RGB
Video Card(s) NVIDIA 1080GTX FE w/ EVGA Hybrid Water Cooler
Storage Samsung EVO 960 M.2 SSD 500Gb
Display(s) Asus XG279
Case In Win 805i
Audio Device(s) EVGA NuAudio
Power Supply CORSAIR RM750
Mouse Logitech Master 2s
Keyboard Keychron K4
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.
 
Joined
May 1, 2008
Messages
47 (0.01/day)
Processor Intel E8400 4GHz @ 1.34v
Motherboard Asus P5Q WS
Cooling Xigmatek S1283
Memory G.Skill Black Pi 2x2GB @ 894MHz 4-4-4-8
Video Card(s) 2 x HD4850 @ 730/1100 Crossfire
Storage Samsung 320GB, 2 x Fujitsu 15K 73GB SCSI RAID 0
Display(s) Soyo 21.6" Widescreen
Case Antec 300
Audio Device(s) Audigy 2 ZS
Power Supply PC P&C Silencer 610W
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
 
Joined
Sep 5, 2007
Messages
512 (0.08/day)
System Name HAL_9017
Processor Intel i9 10850k
Motherboard Asus Prime z490-A
Cooling Corsair H115i
Memory GSkill 32GB DDR4-3000 Trident-Z RGB
Video Card(s) NVIDIA 1080GTX FE w/ EVGA Hybrid Water Cooler
Storage Samsung EVO 960 M.2 SSD 500Gb
Display(s) Asus XG279
Case In Win 805i
Audio Device(s) EVGA NuAudio
Power Supply CORSAIR RM750
Mouse Logitech Master 2s
Keyboard Keychron K4
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.
 
Last edited:
Joined
May 1, 2008
Messages
47 (0.01/day)
Processor Intel E8400 4GHz @ 1.34v
Motherboard Asus P5Q WS
Cooling Xigmatek S1283
Memory G.Skill Black Pi 2x2GB @ 894MHz 4-4-4-8
Video Card(s) 2 x HD4850 @ 730/1100 Crossfire
Storage Samsung 320GB, 2 x Fujitsu 15K 73GB SCSI RAID 0
Display(s) Soyo 21.6" Widescreen
Case Antec 300
Audio Device(s) Audigy 2 ZS
Power Supply PC P&C Silencer 610W
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.

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
 

Kreij

Senior Monkey Moderator
Joined
Feb 6, 2007
Messages
13,817 (2.20/day)
Location
Cheeseland (Wisconsin, USA)
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.
 

Kreij

Senior Monkey Moderator
Joined
Feb 6, 2007
Messages
13,817 (2.20/day)
Location
Cheeseland (Wisconsin, USA)
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 :D
 
Last edited:

Oliver_FF

New Member
Joined
Oct 15, 2006
Messages
544 (0.09/day)
Processor Intel q9400 @ stock
Motherboard Lanparty P45-T2RS
Cooling Zalman CNPS-9500
Memory 8GB OCZ PC2-6400
Video Card(s) BFG Nvidia GTX285 OC
Storage 1TB, 500GB, 500GB
Display(s) 20" Samsung T200HD
Case Antec Mini P180
Audio Device(s) Sound Blaster X-Fi Elite Pro
Power Supply 700w Hiper
Software Ubuntu x64 virtualising Vista
You might want a == in that while loop Kreij ;) It'll compile and run but won't work XD
 

Kreij

Senior Monkey Moderator
Joined
Feb 6, 2007
Messages
13,817 (2.20/day)
Location
Cheeseland (Wisconsin, USA)
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.
 
Joined
Sep 5, 2007
Messages
512 (0.08/day)
System Name HAL_9017
Processor Intel i9 10850k
Motherboard Asus Prime z490-A
Cooling Corsair H115i
Memory GSkill 32GB DDR4-3000 Trident-Z RGB
Video Card(s) NVIDIA 1080GTX FE w/ EVGA Hybrid Water Cooler
Storage Samsung EVO 960 M.2 SSD 500Gb
Display(s) Asus XG279
Case In Win 805i
Audio Device(s) EVGA NuAudio
Power Supply CORSAIR RM750
Mouse Logitech Master 2s
Keyboard Keychron K4
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

My Code said:
//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
 

Kreij

Senior Monkey Moderator
Joined
Feb 6, 2007
Messages
13,817 (2.20/day)
Location
Cheeseland (Wisconsin, USA)
Great job Atn!!

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 :toast:
 
Top