some loop help

So I am trying to write a program where you sell tickets. For every 10 tickets you sell the price decreases 50 cents. Right now I have a good start but my loop is not working out correctly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int count, minPass, passengers;
    float tickPrice;
    cout << " Enter minimum number of passengers ";
    cin >> minPass;
    cout << " Enter number of passengers ";
    cin >> passengers;
    for(count = minPass; count <= passengers; count += 10)
        
    for(tickPrice = 30.00; tickPrice >= 1; tickPrice -= 0.5)

    cout << "\n" <<count << "\t\t" << tickPrice;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}


You can see my output decreases for the whole span and not just every ten tickets sold.

I need it to look something like this

Number Ticket
Passengers Price
300 $20.25
310 $19.75
320 $19.25

and not

Number Ticket
Passengers Price
300 $20.25
300 $19.75
300 $19.25

Any help is extremely appreciated. TIA
Last edited on
Don't do this:
tickPrice -= 0.5,
Instead, do this:
tickPrice - 0.5

Also, for this:
1
2
3
4
5
for(count = minPass; count <= passengers; count += 10)
        
    for(tickPrice = 30.00; tickPrice >= 1; tickPrice -= 0.5)

    cout << "\n" <<count << "\t\t" << tickPrice;


The for loops don't know what to do, put some braces on there.
Don't do this:
tickPrice -= 0.5,
Instead, do this:
tickPrice - 0.5


Err.. that's not right. Using - instead of -= here would cause the loop to deadlock (infinite loop)

And yes, the braces (which technically not required) would certainly help in this case.


The real problem here is that you have a nested loop. Why? You only need one loop.
Last edited on
Line 16 is the issue. When it hits line 16, it enters a loop that decreases the price as long as it's greater then 1, so it just goes straight down to 1.
1
2
3
for(count = minPass; count <= passengers; count += 10)
        
    for(tickPrice = 30.00; tickPrice >= 1; tickPrice -= 0.5)


Try putting it in one for loop instead...

for(count=minPass,tickPrice=30.00;count<=passengers,tickPrice>=1;count+=10,tickPrice -=.5)

I don't have access to my compiler currently so I can confirm if this will work or not.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int count, minPass, passengers;
    float tickPrice, tickStart;
    cout << " Enter minimum number of passengers ";
    cin >> minPass;
    cout << " Enter number of passengers ";
    cin >> passengers;
    cout << " Enter the starting ticket price ";
    cin >> tickStart;
    cout << "# Passengers       Ticket Price        Proft"; 
    for(count = minPass, tickPrice = tickStart ; count <= passengers; count += 10, tickPrice -= 0.5)
            
    cout << "\n" << count <<"\t\t\t"<<"$"<< tickPrice << "\t\t" <<"$"<< count * tickPrice - 2500;
   
    system("PAUSE");
    return EXIT_SUCCESS;
}


Looks like it is working pretty well so far, what do you guys think? There is still a couple more elements I need to add so there is a good chance I will be back for more help. Thanks again

Edit: So I am already having a hard time. I need to output the highest profit and the ticket price that generates that profit. But I cant seem to figure out how to output a specific value from the loop. Any help is greatly appreciated.
Last edited on
I think my last post was unclear. I can figure out by looking at the output what the highest profit is but I can figure out how to display that particular output. I have searched around but cant seem to find any info that will help me.
Create a function that finds the max. I'll get you started...

float max(float a, float b){
if a>b //statement
else //statement}
Topic archived. No new replies allowed.