[HELP] Integer Adding

Ok, so please bare with me this is only my second day with C++ and I have ben trying my hardest and I have made some progress and I’m proud of that at least, so the program I have to make is basically this:

Open Me

Enter the number of coins you have for each denomination.
_____Pennies? 23
_____Nickels? 17
_____Dimes? 14
_____Quarters? 7
_____Half dollars? 3
You have 23 pennies.
You have 17 nickels.
You have 14 dimes.
You have 7 quarters.
You have 3 half dollars.
The value of all your coins is 573 cents.

*Underscores are there to show positioning if it even matters.

Ok, I’ve gotten pretty far I believe the only thing that I get wrong or need assistance with is how do I get it so I can type numbers right next to where it ask you to type in your value i.e Pennies? XX

Also, how do I get it to add the variables and outputs it all as cents not just grouping them. Here is my code and a picture of how it runs.

Open Me
#include <bjarne/std_lib_facilities.h>

int main()
{
    cout << "Enter the number of coins you have for each denomination.
";
    cout << "Pennies?
"; string Pennies;
    cout << "Nickels?
"; string Nickels;
    cout << "Dimes?
";   string Dimes;
    cout << "Quarters?
";  string Quarters;
    cout << "Half Dollars?
";  string Half_Dollars;

    cin >> Pennies >> Nickels >> Dimes >> Quarters >> Half_Dollars;
    string values= Pennies+ Nickels+ Dimes+ Quarters+ Half_Dollars;

    cout << "The value of all your coins is "  << values << " cents. 
";

}

Image

Open Me

EDIT: Here is how the program is suppose to run according to my professor.

Open Me

Open Me
#include <bjarne/std_lib_facilities.h>

using namespace std;

int main() {
	int p, n, d, q,hd;
	int tot[4];
	int temp;	
	cout << "Enter the number of coins you have for each denomination." << endl;
	cout << "Pennies";
	cin >> p;
	cout << "
";
	cout << "Nickels";
	cin >> n;
	cout << "
";
	cout << "Dimes";
	cin >>d;
	cout << "
" ;
	cout << "Quarters" ;
	cin >> q;
	cout << "
";
	cout << "Half dollars";
	cin >> hd;
	cout << "
";
	tot[0] = p;
	tot[1] = (n * 5);
	tot[2] = (d * 10);
	tot[3] = (q * 25);
	tot[4] = (hd * 50);
	temp = tot[0] + tot[1] + tot[2] +tot[3] + tot[4];
	cout << "The total value of your coins is " << temp << " cents." << endl;

		


		return 0;


}

That right?

Might be sloppy, but I did do it really quick so.

Hey thanks so much for taking the time to reply. I just want to know a little bit more so on some other forum I got this

Open Me
cout << "Enter the amount of coins you have for each denomination" << endl;
cout << "Pennies:";
cin >> Pennies;
cout << "Nickels:";
cin >> Nickels;
cout << "Dimes:";
cin >> Dimes;
cout << "Quarters:";
cin >> Quarters;

and when I try compiling it I get that the Pennies in cin is not in scope. Just out of curiosity why is that. Also, I got advice to use float and doubles and I don’t even know how to use that I just need a little explanation if you don’t mind.

declare pennies and such as int values, not strings.

Precision is the amount of decimal values. Others may describe is differently, but this is how I think of it.

INT only holds whole values. So 15 is a int. If I tried to set 15.2 to an int, only 15 would register.

You could use double and then set each penny as .01 of a dollar, and do it the long math way. My way was the really lazy way that was a quick way to type.

^
I know someone will criticize the work, but I’m not the best teacher / person to explain this.

So to give something a value I would just type in “int” and then right after with a number or letter.
Also, after running the project I noticed it doesn’t read back what you have for each how do I go about doing that?

For example:
Enter # of denominations etc
Pennies
etc.

You have XX Pennies
You have XX Nickels
etc

Your total value of coins is etc

I really dont think you have a basic understand of c++ enough to do this project.

Might wanna try reading some basic info about it.

To output a variable in c++ you would write
cout << varname << endl;
it would display the variable’s contents on a line.

So to display pennies it owuld be

cout << You have " << pennies" << " pennies." << endl;
it would look like’

You have 320 pennies.

Int can only hold numbers, no letters.

I know I feel like that and I have been really trying it’s that there was a snow storm in NYC so my classes were canceled and we had to do this program by ourselves on the first day in an intro to cs class.

They wanted you to start off with a program in C++?

Yes the class is in C++. Sorry for being such a noob guys.

Um I encountered another problem, everything is working fine it’s just that now I added my own code so it can read back the imputed variables but it’s like it just skips those lines of code and doesn’t do anything.

cout << "You have " << p << “pennies.
”;
cout << "You have " << n << “nickels.
”;
cout << "You have " << d << “dimes.
”;
cout << "You have " << q << “quarters.
”;
cout << "You have " << hd << “half dollars.
”;

You need to post all of your code. On something this small it doesn’t help just to post the cout stuff.

Sorry about that. Here’s my full code the only problem is that it doesn’t read back the input data near the end.

Open Me

#include <bjarne/std_lib_facilities.h> 

using namespace std; 

int main() { 
    int p, n, d, q,hd; 
    int tot[4]; 
    int temp;     
    cout << "Enter the number of coins you have for each denomination." << endl;
    cout << "Pennies"; 
    cin >> p; 
    cout << "
"; 
    cout << "Nickels"; 
    cin >> n; 
    cout << "
"; 
    cout << "Dimes"; 
    cin >>d; 
    cout << "
" ; 
    cout << "Quarters" ; 
    cin >> q; 
    cout << "
"; 
    cout << "Half dollars"; 
    cin >> hd; 
    cout << "
";

cout << "You have " << p << "pennies. 
";
    cout << "You have " << n << "nickels. 
";
    cout << "You have " << d << "dimes. 
";
    cout << "You have " << q << "quarters. 
";
    cout << "You have " << hd << "half dollars. 
";

  tot[0] = p; 
    tot[1] = (n * 5); 
    tot[2] = (d * 10); 
    tot[3] = (q * 25); 
    tot[4] = (hd * 50); 
    temp = tot[0] + tot[1] + tot[2] +tot[3] + tot[4]; 
    cout << "The total value of your coins is " << temp << " cents." << endl;

      return 0; 

}  

You have an array with 4 elements which means you can only use 0-3. Change it to 5 and everything will work. There is really no need for the array though, it is just doubling up where it isn’t needed.