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
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)
-
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
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.
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?
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);
- {
-
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?
I'm not sure what you've learned so far, but here's how I'd go about it... -
-
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.
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)...
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?
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. -
#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()
-
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: -
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: -
else if (age >= 18 && age < 64)
-
Hope this helps!
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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:...
|
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?
...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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,...
|
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...
| |