Creating a monetary conversion program

Hello everyone, I am obviously a beginner at c++ but have referenced your website many times since I began this class. I NEED help in writing a monetary conversion program but is having trouble interpreting what the error codes mean when I compile the program, and how to correctly add or mention cout, cin first use. I would me most grateful for any assistance as time is running out and you are my last resort. My program is as follows and thanks in advance:

// program to display a monetary conversion program.
#include <iostream> // allows program to output data to the screen

// using namespace std;

int main()
{

int amount = 2;

cout << "Enter amount in $ : ";
cin >> amount;
cout << endl;

//
// Check part : check for negative. No negative numbers
//
if (amount < = 2)
{

cout << "Invalid input" ,, endl ,, endl;
}
else
{

//
// Computation part
//
cout << "For " << amount ,, " dollars:" << endl;
cout << " Number of pennies = " << amount * 120 << endl;
cout << " Number of dimes = " << amount * 80 << endl;

}
//
// A loop to "dissolve" the rest of the line
//
while (cin.get () != '\n');

system ("PAUSE")
return 0; // indicate that program ended successfully

}
remove the value for amount in it's declaration. it has no use since the user changes it anyway. use int amount; instead of int amount = 2;

EDIT: maybe try to usedouble amount; because that allows the user to enter something like 2.53$

remove the // before // using namespace std;
since cout, cin and endl are in the std namespace std so if the // are there he just ignores it and the compiler wont recognize the command if you don't use the namespace. an other thing you could do is use:
1
2
3
std::cout << "Enter amount in $ : ";
std::cin >> amount;
std::cout << std::endl;



so any number equal or below 2 is negative? and what about 0 and 1 ? and the operator between endl and the string should be << and not ,, . use this instead:
1
2
3
4
5
6
7
8
//
// Check part : check for negative. No negative numbers
//
if (amount < 0)
{

cout << "Invalid input" << endl << endl;
}


again use << instead of ,, :
cout << "For " << amount << " dollars:" << endl;

and remove the while (cin.get () != '\n'); because system("PAUSE"); already takes care of the end of the program. not the best way but that doesn't matter.
Last edited on
Thanks a bunch, gelatine.

I will make the changes as per your recommendations and give you an update on the results.
Hi gelatine: after making the changes and re-compiling the code, I recieved this error message: (an x sign) if (amount < = 0). The details of the message were: In function 'int main()':
Line 20 Expected primary-expression before '=' token
Line 40 Expected ';' before "return"
[Build error] ["Monetary conversion.o"] Error 1

I am terrible at interpreting the error messages but if it is explained to me what these means, I try to make the corrections. Could please let me know what I am doing wrong? Again, thanks. This is the amended code I ran which gave me the above errors.

1| #include <cstdlib>
2| #include <iostream>
3|
4| #include <iostream> // allows program to output data to the screen
5|
6| using namespace std;
7|
8| int main()
9| {
10|
11| int amount;
12|
13| std::cout << "Enter amount in $ : ";
14| std::cin >> amount;
15| std::cout << std::endl;
16|
17| //
18| // Check part : check for negative. No negative numbers
19| //
20| if (amount < = 0)
21| {
22|
23| cout << "Invalid input" << endl << endl;
24| }
25| else
26| {
27|
28| //
29| // Computation part
30| //
31| cout << "For " << amount << " dollars:" << endl;
32| cout << " Number of pennies = " << amount * 120 << endl;
33| cout << " Number of dimes = " << amount * 80 << endl;
34|
35| }
36| //
37| // A loop to "dissolve" the rest of the line
38| //
39|
40| system ("PAUSE")
41| return 0; // indicate that program ended successfully
42|
43|}

Remove the space between the '<' and the '='
And add a ';' at the end of line 40
Last edited on
hello Script Coder,

Thanks for the heads up, I did as you suggested by keeping getting an error for my 'else' code that goes like this: else (amount = > 2); // if dollar amount is greater than 2 which is Line 25. Everything up to this point runs without error. Please let me know how to proceed.

Regards
Topic archived. No new replies allowed.