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
remove the value for amount in it's declaration. it has no use since the user changes it anyway. useint 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:
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.
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|}
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.