473,406 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Please help me get started!

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 1904
Banfa
9,065 Expert Mod 8TB
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
05l8kr
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
9,065 Expert Mod 8TB
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
05l8kr
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
gonzo
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
05l8kr
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
gonzo
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
gonzo
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
05l8kr
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
gonzo
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
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

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

Similar topics

6
by: Lochness | last post by:
I'm hoping someone can help me with this. I've seen and tried various solutions I've seen on the net, but nothing works. Of course it works perfectly on localhost, but when I upload it to the...
3
by: morty | last post by:
Heloo I'm starting project.First stage is for my school work, but i want to develop it to the end. I want to create a ERP based on php for small company's I want to use as many as possible...
4
by: Lloyd Sheen | last post by:
So MS where is the error that is reported on last line, says 1 failed. No indication as to what failed. VS 2003, (reinstalled more times than should have been). ------ Build started: Project:...
5
by: bojiokia | last post by:
I have started school for just 3 weeks,and totally new to c++,what i learn is just cin n cout and i received this project, omg, i am totally lost in what the question wanted me to input? ...
36
by: Cap'n Ahab | last post by:
I have used VB3 - VB6, so learning all this OO stuff is reasonably new to me (although I looked at Java a few years ago). Anyway, I thought I would write a small class to begin with, with a...
6
by: lennon1 | last post by:
Hi, I have already started learning .NET and I have a question. If I want to do anything - Display Data, Navigate, Update - with database (SQL Server) in Visual Studio 2005, do I have to use all...
1
by: albert_reade | last post by:
Hello I was wondering if someone could please help me understand what I need to do in order to get this project to work. I just need some hints or a push in the right direction to get this to work,...
5
by: Y2J | last post by:
I am working through this book on C++ programming, the author is speaking of using linked lists. He gave and example which I found confusing to say the least. So I rewrote the example in a way that...
9
by: devranger | last post by:
I am using the below CURL Function and can not figure out why it is not retruning the results from the post. Can anyone take a look and tell me what I may be doing wrong? I am just not seeing...
0
by: sa6113 | last post by:
I want to connect to a Windows machine in my network , using ssh, I use paramiko but I have problem in authentication, would you please help me? 1- I have installed freeSSHD in server machine? Is...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.