| | |
Quick maths problem...
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
Greetings,
I have my first assignment for my programming class to do, but first, I will say that I am aware of those people who post entire problems requesting members to do their homework for them. This is not really what my post is about.
I'm having a little problem with getting my head around the maths part. Let me explain...
Below is the assignment in full:
I'm unsure how to have it display how many tens' of thousands, 'hundreads, cents etc. are in the number that was put in. Any ideas? Just some advice would be much appreciated, perhaps a link to an appropriate tutorial.
I have most of the code written. I will post it if you wish to see it....
Salutations....
I have my first assignment for my programming class to do, but first, I will say that I am aware of those people who post entire problems requesting members to do their homework for them. This is not really what my post is about.
I'm having a little problem with getting my head around the maths part. Let me explain...
Below is the assignment in full:
•
•
•
•
A small company is writing its cheques by hand and wants to change
over to computer generated cheques.
Your boss has asked you, the new programmer, to produce a prototype
cheque writer that will accept the payees name and the amount payable
and display a cheque on the screen. The amount should be broken up
into ten thousand, thousand, hundreds and pounds. If the amount in any
of the categories is 0 then the word "zero" is put in that place.
The name of the company is MODCON Ltd.
Input the payee name: Joe
Input the amount payable: EUR 12045.67
*******************************************************
MODCON
Promises to pay Joe EUR 12045.67
1 ten_thousand
2 thousand
zero hundred
45 euro
67 cent
*******************************************************
Note that only one name is used. We cannot yet input first and
second names together.
Use only what you have learned to date Use any compiler you like to
test your code.
Try to get the cheque in the middle of the screen using <iomanip>
Identify the Inputs Outputs and constants.
Identify the data objects. Note that cheque in NOT an object as far as
we are concerned but is a collection of smaller objects that can be
seen on the screen in the example.
List the actions to be performed on those objects.
Write the algorithm consisting of the list of actions ordered
correctly and any control structures used.
Translate the algorithm into C++ code.
You need only test with positive numbers. Your code need not take
negative numbers into account.
I have most of the code written. I will post it if you wish to see it....
Salutations....
OK I see nobody has responded to my topic. I have recieved an answer to the maths problem using fmod to complete the part. Apparently there is a way to do this without fmod using division and subtraction only. Any ideas???
Please respond....
Answer using fmod;
Please respond....
Answer using fmod;
•
•
•
•
#include <iostream.h>
#include <math.h> /* You need this to use the fmod dunction */
int main()
{
double amt,amt_over;
double field_val10k,field_val1k,field_valhun,field_val10,field_val1,field_valcent;
cout << "Enter amount to be paid:"<<endl;
cin >> amt;
/*Work out the Ten thousands field */
amt_over=fmod(amt,10000);
field_val10k=(amt-amt_over)/10000;
/*Work out the thousands field */
amt_over=fmod(amt,1000);
field_val1k=((amt-amt_over)/1000)-(field_val10k*10);
/*Work out the hundreds field */
amt_over=fmod(amt,100);
field_valhun=((amt-amt_over)/100)-((field_val10k*100)+(field_val1k*10));
/*Work out the tens field */
amt_over=fmod(amt,10);
field_val10=((amt-amt_over)/10)-((field_val10k*1000)+(field_val1k*100)+(field_valhun*10));
/*Work out the single unit field */
amt_over=fmod(amt,1);
field_val1=(amt-amt_over)-((field_val10k*10000)+(field_val1k*1000)+(field_valhun*100)+(field_val10*10));
/*Cent field is the easiest!!*/
field_valcent=(fmod(amt,1)*100);
cout << "#######################################"<<endl;
cout << "Ten thousands:\t" <<field_val10k<<endl;
cout << "Thousands:\t" <<field_val1k<<endl;
cout << "Hundreds:\t" <<field_valhun<<endl;
cout << "Tens:\t\t" <<field_val10<<endl;
cout << "Ones:\t\t" <<field_val1<<endl;
cout << "Cents:\t\t"<<field_valcent<<endl;
cout << "#######################################"<<endl;
return 0;
}
•
•
•
•
Originally Posted by Narue
>however would this way return something like '4.5678?'
No, integers can't hold a precision, so everything past the radix is truncated. You would have 4. But you would also have known this if you had bothered to try it before asking this question.
Now that I've tried it, I can't seem to actually get it working....This is what I have come up with so far but I cannot determine what's wrong with it. Can someone see anything wrong with it?
•
•
•
•
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
int main()
{
string name;
double amount;
string prompt;
prompt = "Input payee name here like: ";
cout << prompt;
cin >> name;
cin.get();
prompt = "Input amount payable: ";
cout << prompt;
cin >> amount;
cin.get();
int ten_thousand;
int thousand;
int hundread;
int euro;
double cent;
cout << endl;
cout << endl;
// THE CHEQUE
cout << "*******************************************";
cout << endl << "\t"<< setw(20) << "MODCON" << endl;
cout << "Promises to pay " << "\t" << name << setw(20) << "EUR" << amount <<endl;
//Calculate amount of thousands.
int thousand = (int) (amount / 1000);
cout << "Thousands " << setw(20) << thousand;
cout << endl << "**********************************************";
cin.get();
return 0;
}
Update** I have got it to give me the amount of ten_thousands and thousands. The next one seems a bit tricky.
However it's the next part I cannot seem to get, the 'hundread'...
•
•
•
•
e.g.... lets say the amount is 35857.56
ten_thousand = (int) (amount / 10000);
ten_thousand comes up as 3.
thousand = (int) (amount / 1000) - (ten_thousand * 10);
thousand comes up as 5
![]() |
Similar Threads
- Start button and Quick Launch problem on boot (Windows 95 / 98 / Me)
Other Threads in the C++ Forum
- Previous Thread: Help-overloading operators
- Next Thread: never to old
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count database delete deploy desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelp homeworkhelper iamthwee ifstream input int integer lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






