You are here:

# C++/C++ newbie need help with fixing code

Question
I have a homework problem using floating decimal and I'm just learning C++, this is my second class and I feel lost. I will paste the question and my code. Any help you can give me will be appreciated.
Thank You!

Lucy likes to jog in the morning.
As she jogs, she counts the number of strides, she makes during the first minute and then again during the last minute of her jogging.
She then averages these two numbers together and calls this the average the number of strides she makes during a minute.

Write a program that asks for the number of strides made during the first minute and the number of strides made during the last minute (each an integer) and then asks for the  total number of hours and minutes that Lucy jogs. (each an integer)
Assume Lucy’s stride length is 2.5 feet. Using the fact that there are 5280 feet in a mile, have your program display the average number of strides Lucy makes in a minute (a float) and the distance Lucy jogged in miles (a float)

Below is a sample run….your code should work for any input I choose .

What was the number of strides you made during the first minute?
128
What was the number of strides you made during the last minute?
123
How many hours did you jog?
1
How many minutes did you jog?
15
Your average number of strides per minute is: 125.5
The distance you jogged was: 4.46 miles

HINT:
The magic code to print a float to 2 places is :
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);

MY CODE:

#include  <iostream>
using namespace std;

int main()
{
int number_of_feet, feet_per_minutes, feet_per_miles, miles_hour;
float numMinutes, numMiles
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout << " What was the number of feet made during the first minute: ";
cin >> numFeet;
cout << " What was the number of feet made during the last minute: ";
cin >> numFeet;
cout << “What is the average number of feet:”;
cin >> “numAvfeet;
cout<<” How many hours jogged:”;
cin >> numHours;
cout << “How many minutes jogged:”;
cin >> numMinutes;
cout << “How many jogged miles per hour:";
cout<< numMiles / numHours << '\n';

return 0;
}

OK as you will know from my instructions to questioners I do not do your homework for you. However here are a few general points.

1/ Don't panic.

3/ Make notes as to what the question is asking you to do so as to make sure what is required - there is no point wasting time and effort doing the wrong thing.

4/ Don't panic.

5/ Read the question carefully. That's right read it again, and again, ... if necessary to make sure you understand what is required.

6/ Make sure you know how to perform the required operations manually (i.e. using pencil or pen and paper rather than a computer and a programming language) to produce the results from the inputs.

7/ When you understand what is required and know how to produce all the required output values then and only then start thinking how to implement the task as a program (in C++ in this case).

8/ Don't panic.

9/ Do not try to get your program all working at once - you can do it one piece at a time, checking each small addition or change works - that is build an executable without errors and that the executable does what is expected up to this point in the proceedings.

10/ Do not be afraid of errors from the build tools - this is normal we _all_ (all writers of code) get them! It is a natural part of the development process. Read carefully what they say - which may not mean much to you at the moment. The most useful piece of information will probably be the line number in the source file that the compiler is complaining about. Note that sometimes the error occurred before the line number the compiler became confused.

With specific regard to your posted question and code:

1/ Does your code ask for exactly the stated required values in the question? If not then it is not doing what was requested. And yes I _do_ mean go back and list what the question states you should ask the user for and note what type these inputs are and while you are there also list what the required outputs are and their types. And yes I do mean once you have done this _look_ at your code and compare what you are requested to ask the user for and what you actually are asking the user for and what you are asked to calculate and output as results from these inputs and what your program actually does calculate and output.

2/ Is your code internally consistent? For example are names of the variables used in a consistent manner? (example: what is numFeet? Oh and as a side issue: Why is a value read into numFeet then another value read in to numFeet again overwriting the first read value before it is used for anything? Do you think this is useful?)

3/ If you started again using the incremental development approach mentioned previously then in this case maybe try these steps (note: if at any stage there is a problem then spend time fixing it before moving on):

a) A do nothing skeleton program:

int main()
{
}

Build it and make sure the resultant executable runs, produces no output
and terminates without producing any errors.

This stage tests your build process - that you can build your program and
produce a viable executable. Once you can do this you can easily repeat
the process to build and run the program as it develops. Getting the build
process to work should not be something that detracts too much from your
and getting it working .

b) Add in enough code to ask the user for the first piece of information

#include  <iostream>

using namespace std;

int main()
{
cout << "What was the number of strides made during the first minute? ";
}

Note that I copied and pasted the text after "What was the " directly from
the assignment question.

Again build the program and make sure it runs as expected.
The expected behaviour now is that the program displays:

What was the number of strides made during the first minute?

On the console without a newline and then terminates.

c) Next add in code to read in the value of first minutes strides:

int main()
{
cout << "What was the number of strides made during the first minute? ";
int number_of_strides_in_first_minute(-1);
cin >> number_of_strides_in_first_minute;

clog << "TRACE: number_of_strides_in_first_minute="
<< number_of_strides_in_first_minute
<< endl;
}

Notice that I only define a variable just before I first need it.
This is considered good C++ practice.

Second note that the variable name reflects the _specific_ input and
that I have set it to an initial invalid value of -1. This could be
used to help detect some failures to read from cin correctly.

Third notice that I have added a temporary statement writing some trace
output to the clog stream (which by default is also written to the console).
This statement is purposely not indented correctly to make it and any other
trace, test or debug  easy to remove later. However they will be useful
while you are still developing the program to allow you to see what values
variables have.

Again build the program and make sure it runs as expected.
The expected behaviour now is that the program displays:

What was the number of strides made during the first minute?

On the console and waits for a value to be entered and then displays:

TRACE: number_of_strides_in_first_minute=XXXXX

followed by a newline and terminates without errors, where XXXXX
should be the value you entered.

When it is running correctly try running the program several times to see
what happens if you enter bad values: -1234 or rubbish or even 123.345.

As this is only your second lesson I do not expect that you need to
actually check for such things just yet - but in real code we do have
to worry and cater for bad input from users, over the Internet, from
files etc. and cases where operations fail in some way.

d) Now add in code to read in the values for the other required input values.
Hint: they follow a similar form to that you now have for the initial
input request. The differences are: the prompt text output to the user (again
see if you can use text directly from the assignment question) and the
name of the variable used to store the value input by the user.

Build and test run the program - preferably after each time you have added
code for another user input request.

e) You should now have a program that asks and reads into separate variables each
of the pieces of information specified in the assignment question. Now you
have to go back and see what you have to do with all these values. Again tackle
one required output at a time. Make sure you know which input values you require
for each required output value and how to use these input values to produce the
output. Take note of the types needed for the output values. Again build and test
producing and outputting each result value.

f) When the program is functioning as required go back as remove the temporary TRACE output statement that write to clog.

Remember: Don't panic!!

Hope this helps.
Questioner's Rating
 Rating(1-10) Knowledgeability = 10 Clarity of Response = 10 Politeness = 10 Comment Thank You for taking the time to explain the logic. I will go through it closely and resubmit my assignment. My Professor wants me to redo it. Have a Nice Day!

C++

Volunteer

#### Ralph McArdell

##### Expertise

I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.

##### Experience

My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.

Education/Credentials