Stuck on a program creation assignment utilising Visual C++, - c++

I am Brand New to programming, Ienter code here have an assignment to do, based on this image and question here, " A local electronics store allows items to be purchased on hire purchase. The hire purchase option
requires a deposit of one fifth of the cash price. There is an interest charge of 10% on the
balance.
Consider the following example: A customer wishes to purchase a Dell Inspiron 15-inch laptop,
with an i5 processor and 8 GB RAM, for $3500. The item code is 12345 and the customer
chooses to pay via two years of hire purchase.
The program operates as follows:
Write a program, HirePurchase, which prints the invoice for an item on hire purchase as
detailed above. The program prompts for the item code, cash price and the number of years on hire
purchase. enter image description here
I have begun but every time I try to run the program only a piece of it presents itself on screen. I'm very sorry if this seems basic but I'm interested in programming.
#include <iostream>
using namespace std;
int main () {
int NumberMonthHirePurchase,ItemCode,NumberYearHirePurchase;
double CashPrice,Deposit,MonthlyInstall,NewbalanceOne, NewBalanceTwo;
cout<<"Please enter the Item Code:2768N3496YHG";
cin>>ItemCode;
cout<<"Please enter the cash price of the item:6,264";
cin>>CashPrice;
cout<<"Please enter the Number of Years Hire Purchase:1";
cin>>NumberYearHirePurchase;
cout<<endl;
cout<<"item details"<<endl;
cout<<"Item code:2768N3496YHG"<<ItemCode<<endl;
return 0;
}
this is what I have so far but only "please enter the item code" is appearing and nothing else. I don't know if it's because I am doing something wrong, can someone please assist, would be greatly appreciated.

The variable ItemCode is an int. That means cin>>ItemCode will read only 2768 from the input. The rest of the input, N3496YHG, will be left in the input buffer for the remaining input to read.
However, since the next input is to read a floating point value, and N3496YHG is not a valid floating point value, nothing will be read and an error flag will be set on the input stream.
Since the error flag is now set on the stream, the third attempt to read something (the integer for NumberYearHirePurchase) will not do anything at all.
To solve this, the ItemCode should be a string, using std::string.
Also note that input of floating-point values doesn't have to use the comma , as decimal separator. It depends on the settings of your computer.
Of course, the above is only valid if you actually try to input something. If you don't input anything, the program will appear to stand still as it patiently waits for your input.

Related

How can I create a loop that terminates at any desired year (C++)?

I was faced with creating a program that can tell a user how much interest will be added to a product after any arbitrary number of years. I had to use a loop to estimate the new price, among other things.
The problem I ran into was in the creation of the loop. I didn't know how to make it stop at any specific year that a user chose. What I did is made a for loop that outputted the loop for the next 99 years. This was my loop:
for (time>0; time<=99; time++) where time was the number of years from today.
This is obviously not ideal. I wrote this in frustration from not knowing how to create a loop that can end wherever the user wants. How can I create a loop that terminates at any desired year?
for (time>0; time<=99; time++)
This will stop at a predefined number, i.e. 99. To make it stop at a number given by user you need to:
Get the number from the user and save it in a variable
Replace 99 with the variable in your loop

Allowing a test to run again C++

I'm new to Stack Overflow and would just like to thank all the people that have unknowingly helped me so far. Anyhow, I'm currently writing the pseudocode for a program in which multiplication problems are generated for the user to answer. My professor wants this program to run until the user enters a sentinel value that would stop asking the user questions, and then display a report of how many questions they correctly answered and so forth.
All of that makes sense to me, and I'd probably be able to code most of that right now without struggling much. However, at the end of the direction sheet, it says "Your program should then repeat the process for another test taker". I'm not entirely sure how I'd go about doing that, since I have a 'while loop' running until the user terminates the program. The pseudocode is as follows:
BEGIN Lab 4 - Multiplication Program
Mult_Num_1 = Random positive single digit number
Mult_Num_2 = Random positive single digit number
Calculate the product
Display the multiplication problem, and sentinel value to quit
User inputs an answer
WHILE ( Answer != QUIT )
IF ( Answer == Product )
Inform user they are correct
Add 1 to Correct_Counter
Add 1 to Question_Counter
ELSE
Inform the user they are incorrect
Display the correct answer
Add 1 to Question_Counter
END IF
Calculate percentage of correct questions
Clear screen
Mult_Num_1 = Random positive single digit number
Mult_Num_2 = Random positive single digit number
Calculate the product
Display the multiplication problem, and sentinel value to quit
User inputs an answer
END WHILE
Display test report
END Lab 4 - Multiplication Program
I'd assume something needs to go after displaying the test report? I'm not entirely sure, but any help would greatly be appreciated. Thank you for taking your time to look over this!

Detecting Enter key for input meant for int type

I am trying to detect and output an error when the user inputs just the enter key when cin prompts for the score. I was thinking
if(score.size()==0)
cout<<"not a score";
However this says I need to declare the datatype for score, which is already declared. I feel like there is a simple and elegant solution. I am looking for a beginners solution with a simple condition to check, or any advice to get there.
void get_scores(vector<int> &v)
{
int score, count=1;
cout<<"Enter scores, enter -1 to stop"<<endl;
cin>>score;
while (score!=-1 && count<=100)
{
v.push_back(score);
count++;
cin>>score;
}
}
It sounds like you are new to programming. It's important to learn how to break your problems down into smaller steps.
The first step would be reading a string from the user (instead of an integer). The second step would be to see if the string is empty. The third step would be to convert the string to an integer.
You can find good information about each of these three individual steps on this site. Once you know how to do those three steps, you should be able to put them all together in a program.
I realize this isn't a complete answer but I hope it helps you understand how to approach this. With enough practice, breaking your problems into smaller pieces like this should become an automatic thing.

Finding Means with arrays

I got the assignment and the numbers to create a program which asks
You are to write a C++ program that will prompt for and re ad in the
Math and Verbal SAT scores for a sample population of students and
then determine the mean Math SAT score, the mean Verbal SAT score, the
standard deviation for the Math SAT scores and the standard deviation
for the Verbal SAT scores respectively.
The part that is confusing me is I am not sure if the program is asking me to input the scores given right into the code or to make a function or whatever to input them in as a user.
This another part of the assignment which may be pertinent.
Read the Math and Verbal SAT scores from the keyboard into a two
dimensional array for storage. The array should have 10rows and two
columns one for the math SAT score and the second for the verbal SAT
score.
Sorry for asking such an easy question my brain is fried and I am in over my head. I am going in for help tomorrow but I need to have more of a code together because the project is due for Wednesday.
write a C++ program that will prompt for and re ad in the Math and Verbal SAT scores
You're expected to take them as input from the user.
i.e. don't hardcode them, take them as keyboard input.

Trouble calculating correct decimal digits

I am trying to create a program that will do some simple calculations, but am having trouble with the program not doing the correct math, or placing the decimal correctly, or something. Some other people I asked cannot figure it out either.
Here is the code: http://pastie.org/887352
When you enter the following data:
Weekly Wage: 500
Raise: 3
Years Employed: 8
It outputs the following data:
Year Annual Salary
1 $26000.00
2 $26780.00
3 $27560.00
4 $28340.00
5 $29120.00
6 $29900.00
7 $30680.00
8 $31460.00
And it should be outputting:
Year Annual Salary
1 $26000.00
2 $26780.00
3 $27583.40
4 $28410.90
5 $29263.23
6 $30141.13
7 $31045.36
8 $31976.72
Here is the full description of the task:
8.17 ( Pay Raise Calculator Application) Develop an application that computes the amount of money an employee makes each year over a user- specified number of years. Assume the employee receives a pay raise once every year. The user specifies in the application the initial weekly salary, the amount of the raise (in percent per year) and the number of years for which the amounts earned will be calculated. The application should run as shown in Fig. 8.22. in your text. (fig 8.22 is the output i posted above as what my program should be posting)
Opening the template source code file. Open the PayRaise.cpp file in your text editor or IDE.
Defining variables and prompting the user for input. To store the raise percentage and years of employment that the user inputs, define int variables rate and years, in main after line 12. Also define double variable wage to store the user’s annual wage. Then, insert statements that prompt the user for the raise percentage, years of employment and starting weekly wage. Store the values typed at the keyboard in the rate, years and wage variables, respectively. To find the annual wage, multiply the new wage by 52 (the number of weeks per year) and store the result in wage.
Displaying a table header and formatting output. Use the left and setw stream manipulators to display a table header as shown in Fig. 8.22 in your text. The first column should be six characters wide. Then use the fixed and setprecision stream manipulators to format floating- point values with two positions to the left of the decimal point.
Writing a for statement header. Insert a for statement. Before the first semicolon in the for statement header, define and initialize the variable counter to 1. Before the second semicolon, enter a loop- continuation condition that will cause the for statement to loop until counter has reached the number of years entered. After the second semicolon, enter the increment of counter so that the for statement executes once for each number of years.
Calculating the pay raise. In the body of the for statement, display the value of counter in the first column and the value of wage in the second column. Then calculate the new weekly wage for the following year, and store the resulting value in the wage variable. To do this, add 1 to the percentage increase (be sure to divide the percentage by 100.0 ) and multiply the result by the current value in wage.
Save, compile and run the application. Input a raise percentage and a number of years for the wage increase. View the results to ensure that the correct years are displayed and that the future wage results are correct.
Close the Command Prompt window.
We can not figure it out! Any help would be greatly appreciated, thanks!
Do not store money as floating point. This will end only in tears. Store money as an integral number of cents.
The reason for this is that floating point math on a computer is necessarily inexact. You know that 0.40 / 2 = 0.20, but it's entirely possible that the computer will say it is 0.19999999999999, and that is not an error. The internal representation of floating point numbers makes it impossible for a computer to exactly represent some fractions, much like you cannot write out an exact decimal representation of 1/3 (without an infinite amount of paper).
When you are dealing with numbers that have fractional parts and for which inexactness is not acceptable (e.g. money), you must compute using fixed-point math. In general, you might use a fixed point library, but for an assignment like this, if you're not allowed to do so, an int that stores a number of pennies will do just fine, so long as you understand how integer division works. You will have to write more math code and account for the rounding yourself, though. But that's what you want. You want absolute control over rounding.
I changed your for loop to this:
cout << (i+1) << " $" << wage*52 << "\n";
wage = wage * (1+(raise/100.0));
And it did worked!. I see you didn't understand the language of the problem.
I think that the intention is to receive a 3% raise each year, but you are actually only adding 3% of the starting salary ($780 in this case) each year. You may want to explore modifying the wage value on each pass in the loop (I won't present a solution as I suspect that this is a homework problem, yes?).
The best way to catch this sort of problem is to run it in a debugger and step through each line looking for when the results don't match your expectations. It's usually pretty easy at that point to figure out where your logic went astray.
Your problem is that your program ignores compounding. You are calculating the dollar value of the raise once, and using that for each increase. Once you get your first raise, the value of your second raise needs to be calculated based on your new wage, not your original wage.