446,188 Members | 1,024 Online
Need help? Post your question and get tips & solutions from a community of 446,188 IT Pros & Developers. It's quick & easy.

 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
11 Replies

 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 apstring name="subway"; i = 0;            //adult while (i< name.length( ))     {       cout<< name[ i ] << endl;         i++;                             cout << "There are " << i << " characters.\n";   I'm completely LOST Nov 2 '06 #3

 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 { cout << "Enter your age: "; cin >> age; } while (age != -1); apstring name="subway"; i = 0;            //adult while (i< name.length( ))     {       cout<< name[ i ] << endl;         i++;                             cout << "There are " << i << " characters.\n";   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   int age; int count1 = 0; int count2 = 0; int count3 = 0;   do {    cout << "Enter an age: ";    cin >> age;      if ( age <= 17 )    {          count1++;    }      else if ( age < 65 )     {          count2++;     }       else if ( age >= 65 )     {         count3++;     }   } while ( age != -1 );   //  the rest of the code should be easy to figure out. Just display the counts, (age groups)...     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 #include    using namespace std;   void main() {    int age;    int count1 = 0;    int count2 = 0;    int count3 = 0;      do    {        cout << "Enter an age: ";        cin >> age;          if ( age <= 17 )        {            count1++;        }         else if ( age < 65 )       {            count2++;       }          else if ( age >= 65 )        {             count3++;        }       } while ( age != -1 );        count1--; // subtract one off of count1 to compensate for the -1         cout << "There were " << count1 << " people in the first age group, " <<        endl <<  count2 << " people in the second age group and " << count3 <<       endl <<  " in the third age group. " << endl << endl;   } // end main()    Nov 3 '06 #11

 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 cout << "Age category breakdown of transit use: \n\n"; cout << "17 and under: " << count1 << "\n\n"; cout << "18 - 64: " << count2 << "\n\n"; cout << "65 and above: " << count3 << "\n\n";       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 else if (age >= 18 && age < 64)   Hope this helps! Nov 8 '06 #12