Please help me get started!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • 05l8kr
    New Member
    • Sep 2006
    • 35

    Please help me get started!

    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
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Start with creating a while loop that lets you enter numbers and stops when you enter a -1 (or may be any minus value)

    Comment

    • 05l8kr
      New Member
      • Sep 2006
      • 35

      #3
      Code:
      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

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Well you are going to need to use cin, can you write a single line that allows the user to input a number.

        Comment

        • 05l8kr
          New Member
          • Sep 2006
          • 35

          #5
          Originally posted by Banfa
          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?

          Comment

          • gonzo
            New Member
            • Nov 2006
            • 9

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

            Comment

            • 05l8kr
              New Member
              • Sep 2006
              • 35

              #7
              Code:
              {
              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?

              Comment

              • gonzo
                New Member
                • Nov 2006
                • 9

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

                Code:
                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.

                Comment

                • gonzo
                  New Member
                  • Nov 2006
                  • 9

                  #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)...

                  Comment

                  • 05l8kr
                    New Member
                    • Sep 2006
                    • 35

                    #10
                    Originally posted by gonzo
                    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?

                    Comment

                    • gonzo
                      New Member
                      • Nov 2006
                      • 9

                      #11
                      Originally posted by karlracki
                      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.


                      Code:
                      #include <iostream>
                      
                      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()

                      Comment

                      • sonic
                        New Member
                        • Nov 2006
                        • 40

                        #12
                        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:

                        Code:
                        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:

                        Code:
                        else if (age >= 18 && age < 64)
                        Hope this helps!

                        Comment

                        Working...