C++ in college, need help with first assignment

Overview

Write a program to calculate the number of singles and total number of bases for a baseball player using information supplied by the player.

If you're not familiar with baseball, keep reading. Otherwise, move ahead to the section titled "Basic Program Logic." A lot of baseball terms are pretty self-explanatory: a single is a 1 base hit, a double is a 2 base hit, and a triple is a 3 base hit. The one that may not be familiar is a home run, which is a 4 base hit.

Basic Program Logic

prompt the player for the total number of hits. This is an integer value that should be stored in an integer variable.

prompt the player for the number of doubles they have hit. This is an integer value that should be stored in an integer variable.

prompt the player for the number of triples they have hit. This is an integer value that should be stored in an integer variable.

prompt the player for the number of home runs they have hit. This is an integer value that should be stored in an integer variable.

calculate the number of singles and the total number of bases the player has earned:

number of singles = total number of hits - number of doubles - number of triples - number of home runs

total number of bases = number of singles + (number of doubles * 2) + (number of triples * 3) + (number of home runs * 4)
display the number of each type of hit (single, double, triple, and home run) with an appropriate label

display the total number of bases with an appropriate label

Here is what I have so far, I know it isn't much but I am very unsure of what I am doing, if someone could explain to me that would be fantastic!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <iomanip>

using namespace std;

int main ()
{
int s, d, t, h, x, sums;

s = 1;
d = 2;
t = 3;
h = 4;
x = 1;

sums = s + x; 
}
Last edited on
You already have the formulas you need. You can use cin to get values from the user. After that just use those formulas with the input you got from the user and store the result in some other variable.
1
2
3
4
5
6
...
cin >> doubles;
...

int singles = ...
...
How exactly would I go about doing that and compiling it correctly? I have it setup correctly I just am unsure how to follow up and finish it with the proper math, I of course don't want it done for me I just want to make sure I properly understand it and do my assignment correctly.
So you can do
1
2
3
int doubles = 0;
cout << "Enter doubles: ";
cin >> doubles;


doubles now holds the value of whatever the user entered (assuming the user didn't enter something that wasn't a number)

You can do the same thing for triples, home runs, hits to prompt the user for input and store the value they entered in some variable.

 
int singles = hits - doubles... //Finish the formula, Combine/use the values you got earlier to calculate this 

You have the formulas already written in your original post. Once you decided the names you'll use for the variables in the program it's more of just sticking the formula in there with the right variable names in the right place (ie doubles wherever you need the # of doubles they hit, etc).

When you're doing the math, you can just do it almost exactly how it looks in your post singles + (doubles * 2) will do exactly what it looks like it will do. You can just write the whole formula in one expression.

eg Perimeter = (2 * length) + (2 * width):

1
2
int length = 4, width = 5;
int perimeter = (2 * length) + (2 * width);

Now perimeter will hold whatever the value is that came out of that formula for the input I plugged into it.
Cool, I will try that tomorrow after classes and will come back with any questions if need be, either way I appreciate it! :)
This name is attributed to Rick Mascitti (mid-1983) and was first time manage in December 1983. parior, during the development age, the manufacturing language had been named as "new C", then "C with Classes". In computer science C++ is still named as a superoperations of C. The last name was given from C's "++" operator and a well known name style of using "+" to show an advance computer program. C++ is designed for the following purposes: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-096-introduction-to-c-january-iap-2011/,it is made to be a statically typed, to work normal purpose language that is as fast and easy to move as C. C++ is designed to forward and completely help more then one programming ways as like http://yourhomeworkhelp.org/do-my-programming-homework/ (step by step programming, piece of data, object-oriented programming, and basic programming). C++ is made to provide the programmer select, even if this makes it probable for the programmer to select not correctly. C++ is made to be as reliable with C as possible, therefore facilitating a smooth transition from C. C++ prevent the views that are platform definite or not normal purpose C++ does not incur forward for views that are not in programming C++ is made to work without a sophisticated programming condition
Last edited on
Topic archived. No new replies allowed.