By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,188 Members | 1,024 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,188 IT Pros & Developers. It's quick & easy.

Please help me get started!

P: 35
I have to write this from scratch, what do I do to start? I'm suppose to write a program that counts how many of each fee classification rode the subway on a given day (this is a set of if statements). The subway has three fee classifications: students (17 years old or less) adult, and senior (65 years old or more). I have to assume that the conductor has a laptop and enters the age of each rider. Your instructions tell the conductor to enter a -1 at the end of the day to terminate the data entry process ( a loop will cause the program to continue until a -1 is entered).

After the -1 is entered to stop the data entry process, your program must display the number of customers in each fee category.

Where to start? Any help would be greatly appreciated
Nov 1 '06 #1
Share this Question
Share on Google+
11 Replies


Banfa
Expert Mod 5K+
P: 8,916
Start with creating a while loop that lets you enter numbers and stops when you enter a -1 (or may be any minus value)
Nov 2 '06 #2

P: 35
Expand|Select|Wrap|Line Numbers
  1. apstring name="subway";
  2. i = 0;            //adult
  3. while (i< name.length( ))    
  4. {
  5.       cout<< name[ i ] << endl;  
  6.       i++;                            
  7. cout << "There are " << i << " characters.\n";
  8.  
I'm completely LOST
Nov 2 '06 #3

Banfa
Expert Mod 5K+
P: 8,916
Well you are going to need to use cin, can you write a single line that allows the user to input a number.
Nov 2 '06 #4

P: 35
Well you are going to need to use cin, can you write a single line that allows the user to input a number.
I'm lost so anyone want to help me get started?
Nov 2 '06 #5

P: 9
I would make a while (or do-while) loop to keep taking ages until -1 is entered. I'd have 3 seperate counters for each age group. Make some If statements in the loop so each time an age is entered, 1 is added to that count. Hope that helps, and goodluck.

do
{
cout << "Enter your age: ";
cin >> age;
.... and then all the other code
} while (age != -1);
Nov 2 '06 #6

P: 35
Expand|Select|Wrap|Line Numbers
  1. {
  2. cout << "Enter your age: ";
  3. cin >> age;
  4. } while (age != -1);
  5. apstring name="subway";
  6. i = 0;            //adult
  7. while (i< name.length( ))    
  8. {
  9.       cout<< name[ i ] << endl;  
  10.       i++;                            
  11. cout << "There are " << i << " characters.\n";
  12.  
This is what I have so far, I'm lost anyone have aim or yahoo to talk to with?
Nov 2 '06 #7

P: 9
I'm not sure what you've learned so far, but here's how I'd go about it...

Expand|Select|Wrap|Line Numbers
  1.  
  2. int age;
  3. int count1 = 0;
  4. int count2 = 0;
  5. int count3 = 0;
  6.  
  7. do
  8. {
  9.    cout << "Enter an age: ";
  10.    cin >> age;
  11.  
  12.    if ( age <= 17 )
  13.    {
  14.          count1++;
  15.    }
  16.  
  17.    else if ( age < 65 )
  18.     {
  19.          count2++;
  20.     }
  21.  
  22.     else if ( age >= 65 )
  23.     {
  24.  
  25.       count3++;
  26.     }
  27.  
  28. } while ( age != -1 );
  29.  
  30. //  the rest of the code should be easy to figure out. Just display the counts, (age groups)...
  31.  
  32.  

The idea is you're running a loop ( a do-while in this case) which will keep taking values from the user until -1 (the sentinel value) is entered. At that point, you want it to display the #'s of each age group, (your 3 counts) and end. I'm also new at this, but I've always learned best through examples. So hopefully this makes sense to you!! I do have AIM if you want to talk about it later... good luck.
Nov 3 '06 #8

P: 9
I forgot to add this, but It should be obvious once you get your program together and run it. You're going to want to add a " count1--; " after the loop to compensate for the -1 sentinel. Otherwise, it's going to say you have 1 extra person in age group one (17 and under)...
Nov 3 '06 #9

P: 35
I forgot to add this, but It should be obvious once you get your program together and run it. You're going to want to add a " count1--; " after the loop to compensate for the -1 sentinel. Otherwise, it's going to say you have 1 extra person in age group one (17 and under)...
What do you mean by display the counts?
Nov 3 '06 #10

P: 9
What do you mean by display the counts?
You just want to stick those 3 counts at the end in a cout statement so the program does what it's supposed to. This program below should compile and execute fine, but I suggest you look everything over so that it makes sense to you.


Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. void main()
  6. {
  7.    int age;
  8.    int count1 = 0;
  9.    int count2 = 0;
  10.    int count3 = 0;
  11.  
  12.    do
  13.    {
  14.        cout << "Enter an age: ";
  15.        cin >> age;
  16.  
  17.        if ( age <= 17 )
  18.        {
  19.            count1++;
  20.        }
  21.  
  22.       else if ( age < 65 )
  23.       {
  24.            count2++;
  25.       }
  26.  
  27.        else if ( age >= 65 )
  28.        {
  29.  
  30.           count3++;
  31.        } 
  32.  
  33.    } while ( age != -1 );
  34.  
  35.      count1--; // subtract one off of count1 to compensate for the -1
  36.  
  37.       cout << "There were " << count1 << " people in the first age group, " << 
  38.       endl <<  count2 << " people in the second age group and " << count3 <<
  39.       endl <<  " in the third age group. " << endl << endl;
  40.  
  41. } // end main() 
  42.  
Nov 3 '06 #11

sonic
P: 40
He means that after the heart and guts of your program (the code he posted) calculates what you're trying to keep track of that you need a way to display it; otherwise the program would be useless as it would never return to you the information you need. You might try something like:

Expand|Select|Wrap|Line Numbers
  1. cout << "Age category breakdown of transit use: \n\n";
  2. cout << "17 and under: " << count1 << "\n\n";
  3. cout << "18 - 64: " << count2 << "\n\n";
  4. cout << "65 and above: " << count3 << "\n\n";
  5.  
  6.  
  7.  

Also, you may want to look at your count2 code. I think there may be occasions where both count1 and count2 may be incremented. You may want to try something like:

Expand|Select|Wrap|Line Numbers
  1. else if (age >= 18 && age < 64)
  2.  
Hope this helps!
Nov 8 '06 #12

Post your reply

Sign in to post your reply or Sign up for a free account.