C++ help please.

Status
Not open for further replies.

derekjr_1

Smack-Fu Master, in training
67
Probably a basic question, but I need to be able to do calculate a problem in c++ but cant figure this out. Here goes... I need to be able to calc the first 75000 * .10, the next 25000 + .05 and anything higher after that + .01. I tried and if statement to like this:

if ( numEntered > 75000 < 100000 )

but that allways creates a warning. I need to be able to specify a range, without using functions and classes. This is my first basic project but I just cant figure this out. Here is my assignment to help you get a better understanding:

Problem

Write a program to calculate and display an IRS informant's reward for recovering monies owed the government.

Specifications

1. IRS informants are paid cash rewards on the value of the money recovered. If the information was specific enough to lead to a recovery, the informant receives 10% of the first $75,000, 5% of the next $25,000, and 1% of the remainder, up to a maximum award of $50,000.

2. Write a program that requests the amount of the recovery as an input and displays the reward. Assume that if the informant enters an amount, the information was specific enough to receive the award.

3. Follow this sequence in program execution:

Prompt user to enter an appropriate value of money recovered.
Loop until a positive number has been entered, prompting each time you loop.
Calculate the person's award
Display the person's award in a nice neat manner
4. Format the award amount so it is displayed with a $ and two decimal places.



5. Test your code with numbers such as -5, $10,000, $125,000, and $10,000,000. Be sure you get the correct award amounts for each.



6. Be sure to use good programming practices and the C++ language constructs.

Here is the code I have so far, compiles with not errors but I cant figure the range thing out. Any help is much apprecaited, thanks as I am a newbie.

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main()
{
double numEntered = 0;

cout << "Enter the value of money recovered" << endl;
cin >> numEntered;

while ( numEntered < 0 ) {
cout << "Enter a valid number" << endl;
cin >> numEntered;
}

if ( numEntered <= 75000 )
cout << numEntered * .10 << endl;
if ( numEntered > 75000 )
cout << numEntered * .15 << endl;
if ( numEntered > 100000 )
cout << numEntered * .16 << endl;

return 0;
}null
 

Richard Berg

Ars Legatus Legionis
43,037
Subscriptor
code:<HR>
Code:
#pragma once<P>...<P>inline bool between(int x, lhs, rhs) { return lhs<x && x<rhs; };
inline bool betweenInclusive(int x, lhs, rhs) { return lhs<=x && x<=rhs; };
inline bool between(double x, lhs, rhs) { return lhs<x && x<rhs; };
inline bool betweenInclusive(double x, lhs, rhs) { return lhs<=x && x<=rhs; };<P>...
This is one of the things I encounter so often that it gets shoved into my "useful stuff" header.
 

earl

Well-known member
3,066
You should make those template functions... or is there a reason you can't? Although, I don't think you can inline template functions... (actually, at least under gcc, this seems to compile.)

code:<HR>
Code:
template<class T> inline bool between(T val, T lhs, T rhs)
{
   return (lhs<val && rhs>val);
};

-earl-

[This message was edited by earl on February 05, 2002 at 13:40.]
 
They're very powerful, but care is needed not to abuse them. My only real gripe about them is that you have to include the source in the headers, so they're not really an option for lots of large functions in a large project. They're better off for low-level objects, wrappers, STL containers and algorithms, etc. They really do make for some nifty programming :)
 

hiner112

Smack-Fu Master, in training
98
I had only one problem with templates so far. I was using a c++ program to get information from a stats file at the great internet prime search. (what exponents they had, how many computers, how many exponents were overdue, etc) and there were some alot of objects that were being kept track of and stored (Dates, exponents, etc. ) but I couldn't template the output to file. I wanted to do:

code:<HR>
Code:
template<class datatype>
Log_Out::Output(datatype data)
{
     this->filestream << data;
}

But it wasn't supported by the compiler (I don't think) so I has to write a lot of functions that would have just been the instantiations of the one above. If anyone knows if there's a compiler that supports templated member functions let me know.
 
Status
Not open for further replies.