Problem with variables (I think!)

Pages: 12
I've started trying to learn C++ because I find it pretty interesting, and I need something to do other than working at the moment!
I've learnt about some basics so far, showing text, getting user input, boolean operators, and variables more or less.

In order to test my knowledge of variables I decided to make a program to total the change in my coin jar after I'd input the numbers of each coin I have.
The code goes like such:


#include <iostream>

using namespace std;

int main()

{
cout<<"Welcome to the piggybank counter. This program is\n"
"basically a glorified calculator! You put in the total\n"
"number of each type of coin in your piggy bank when prompted.\n"
"\n";
cout<<"Before starting, it might help if you put the coins\n"
"into different piles in order to help you count them.\n"
"\n";

int POUNDS;
int PENCE;
int A;
(PENCE=100);
cout<<"So, now you've done that, let's get started. How many one\n"
"pound coins do you have? Press ENTER after putting the number in.\n";
cin>>POUNDS;
cin.ignore();
cout<<"\n"
"Ok, that makes "<<POUNDS*PENCE<<" pence.\n"
"\n";
(POUNDS*PENCE==A);

int B;
cout<<"Braw, now put in the number of 50 pence \n"
"pieces that you've got. Mind and press ENTER!\n";
cin>>B;
cin.ignore();
cout<<"\n";

int C;
cout<<"Ok, how about 20 pence pieces?\n";
cin>>C;
cin.ignore();
cout<<"\n";

int D;
cout<<"So how many 10 pence pieces do you have?\n";
cin>>D;
cin.ignore();
cout<<"\n";

int E;
cout<<"Just the small change to go now!\n"
"How many 2 pences have you got?\n";
cin>>E;
cin.ignore();
cout<<"\n";

int F;
cout<<"And finally, how many pennies?\n";
cin>>F;
cin.ignore();
cout<<"\n";

cout<<"In your piggybank, you have "<<(A+B+C+D+E+F)<<" pence\n.";
int TPENCE;
int TPOUNDS;
((A+B+C+D+E+F==)int TPENCE)
(TPENCE/100==TPOUNDS)
cout<<"Which is "<<TPOUNDS<<" in pounds!";

cout<<"Please press ENTER to terminate the program.\n";
cin.ignore();
return 0;
}


The problem is just in the second last part there, where I tried to take all the integers and add them together. I don't think the following part works either, where I tried to divide the integer by 100 to turn the pence into pounds. You see, I didn't really know 100% how to write these parts of code, so I sort of made an educated guess.

If anyone could help sort it out, I'd much appreciate it!
Last edited on
i am very new to C++, but i see that your lines


change from
1
2
3
4
int TPENCE;
int TPOUNDS;
((A+B+C+D+E+F==)int TPENCE)
(TPENCE/100==TPOUNDS)


changed to

1
2
int TPENCE = (A+B+C+D+E+F);
int TPOUNDS = (TPENCE/100);


AND
changed from
(POUNDS*PENCE==A);

changed to
A = (POUNDS * PENCE);

The target(target being variable that value is put into will be on left side) and the value on the right side
Plus == operator is says "equal to"
whereas = operator says "put right operand into left operand"
Last edited on
haha sorry should've explained that a little better.

i'm scottish, in the united kingdom we use pounds sterling as a currency.
so, the lowest common denominator in terms of currency is a penny, and there are 100 pennies in a pound. there are also 2, 5, 10, 20, and 50 pence pieces - hence all the different requests for the user to input values for these variables. this makes me realize i've missed out a 5 pence variable.

thanks for the help, i'll give it a go, see if it compiles and let you know!
no prob, hope it works
Ok, changed the code to your suggestion and it compiled - but no matter what I put for the variable inputs it came up with the same ridiculous answer everytime, like 45835 pounds or something like that, when it should've been well under 10 pounds.

Then I tried putting in another variable, using more or less the same syntax as previously in order to factor in the 5 pence pieces that I'd forgotten originally.

When I tried to compile it came up with a problem on line 28 (waaaay up in the code, that I hadn't even touched and that had worked before) around the "<<POUNDS*PENCE<<" part.

Anyways, here's the rewritten code:


#include <iostream>

using namespace std;

int main()

{
//Explanation of the program's function to the user.
cout<<"Welcome to the piggybank counter. This program is\n"
"basically a glorified calculator! You put in the total\n"
"number of each type of coin in your piggy bank when prompted.\n"
"\n";
//Preparing the user for using the program.
cout<<"Before starting, it might help if you put the coins\n"
"into different piles in order to help you count them.\n"
"\n";

//First 'set' in the program. Finding out the number of
//pound coins the user has and changing it into pence.2
int POUNDS;
int PENCE;
int A;
(PENCE=100);
cout<<"So, now you've done that, let's get started. How many one\n"
"pound coins do you have? Press ENTER after putting the number in.\n";
cin>>POUNDS;
cin.ignore();
cout<<"\n"
"Ok, that makes "<<POUNDS*PENCE<<" pence.\n"
"\n";
(POUNDS*PENCE==A);

//Second 'set' in the program. Finding out the number
//of 50 pence pieces they've got.
int B;
cout<<"Braw, now put in the number of 50 pence \n"
"pieces that you've got. Mind and press ENTER!\n";
cin>>B;
cin.ignore();
cout<<"\n";

//Third 'set'. Now, twenty pence pieces.
int C;
cout<<"Ok, how about 20 pence pieces?\n";
cin>>C;
cin.ignore();
cout<<"\n";

//Fourth 'set'. Ten pence pieces.
int D;
cout<<"So how many 10 pence pieces do you have?\n";
cin>>D;
cin.ignore();
cout<<"\n";

//Fifth 'set'. 2 pence pieces.
int E;
cout<<"Just the small change to go now!\n"
"How many 2 pences have you got?\n";
cin>>E;
cin.ignore();
cout<<"\n";

//Sixth 'set'. 1 penny peices.
int F;
cout<<"And finally, how many pennies?\n";
cin>>F;
cin.ignore();
cout<<"\n";

//The final output of the program - to display the
//user's piggybank's total. Multiply all variables
//A through F together to give the value in pence,
//and divide it by 100 to return the value in pounds.
cout<<"In your piggybank, you have "<<(A+B+C+D+E+F)<<" pence\n.";
int TPENCE;
int TPOUNDS;
((A+B+C+D+E+F==)int TPENCE)
(TPENCE/100==TPOUNDS)
cout<<"Which is "<<TPOUNDS<<" in pounds!";


//To ensure the program doesn't close!
cout<<"Please press ENTER to terminate the program.\n";
cin.ignore();
return 0;
}


Here I included my annotations, which I just deleted before pasting in the original question.

Again, thanks for any help.
this code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>

using namespace std;

int main()

{
cout<<"Welcome to the piggybank counter. This program is\n"
"basically a glorified calculator! You put in the total\n"
"number of each type of coin in your piggy bank when prompted.\n"
"\n";
cout<<"Before starting, it might help if you put the coins\n"
"into different piles in order to help you count them.\n"
"\n";

int POUNDS;
int PENCE;
int A;
(PENCE=100);
cout<<"So, now you've done that, let's get started. How many one\n"
"pound coins do you have? Press ENTER after putting the number in.\n";
cin>>POUNDS;
cin.ignore();
cout<<"\n"
"Ok, that makes "<<POUNDS*PENCE<<" pence.\n"
"\n";

A = (POUNDS * PENCE);

int B;
cout<<"Braw, now put in the number of 50 pence \n"
"pieces that you've got. Mind and press ENTER!\n";
cin>>B;
cin.ignore();
cout<<"\n";

int C;
cout<<"Ok, how about 20 pence pieces?\n";
cin>>C;
cin.ignore();
cout<<"\n";

int D;
cout<<"So how many 10 pence pieces do you have?\n";
cin>>D;
cin.ignore();
cout<<"\n";

int E;
cout<<"Just the small change to go now!\n"
"How many 2 pences have you got?\n";
cin>>E;
cin.ignore();
cout<<"\n";

int F;
cout<<"And finally, how many pennies?\n";
cin>>F;
cin.ignore();
cout<<"\n";

cout<<"In your piggybank, you have "<<(A+B+C+D+E+F)<<" pence\n.";
int TPENCE = (A+B+C+D+E+F);
int TPOUNDS = (TPENCE/100);


cout<<"Which is "<<TPOUNDS<<" in pounds!";

cout<<"Please press ENTER to terminate the program.\n";
cin.ignore();
return 0;
}

gave out this output with these values

Welcome to the piggybank counter. This program is
basically a glorified calculator! You put in the total
number of each type of coin in your piggy bank when prompted.

Before starting, it might help if you put the coins
into different piles in order to help you count them.

So, now you've done that, let's get started. How many one
pound coins do you have? Press ENTER after putting the number in.
5

Ok, that makes 500 pence.

Braw, now put in the number of 50 pence
pieces that you've got. Mind and press ENTER!
2

Ok, how about 20 pence pieces?
5



So how many 10 pence pieces do you have?
10

Just the small change to go now!
How many 2 pences have you got?
50

And finally, how many pennies?
100

In your piggybank, you have 667 pence
.Which is 6 in pounds!Please press ENTER to terminate the program.


this is what values i gave in that program, but what is the answer suppose to be??
int A will always be (pounds * 100)
not knowing this currency, i have no idea how to get the outcome proper
Last edited on
Ok figured out that wee problem there, somehow the cout<<"" had been deleted from line 28. Just me being stupid I guess.

The answer to the values you put in should be 1000 pence, or 10 pounds (£10) exactly.
This is kinda frustrating.
not knowing this currency, i have no idea how to get the outcome proper


at what point does that math go wrong?
No clue man, I just can't see it.

There's the pounds transferred into pence, and set as integer A.
Then there's all of the other coins set as integers too.
And at the end all the integers are added together.

Seems simple.

EDIT: OH MY JESUS. Just realized that it's taking all the integers as the numbers that the user puts in, whereas for each one it should multiply the input number by the value of each coin. Not sure if that fixes things, but still, *FACEPALM*

RE-EDIT: Nope, definitely doesn't. See any major problems with this?
#include <iostream>

using namespace std;

int main()

{
//Explanation of the program's function to the user.
cout<<"Welcome to the piggybank counter. This program is\n"
"basically a glorified calculator! You put in the total\n"
"number of each type of coin in your piggy bank when prompted.\n"
"\n";
//Preparing the user for using the program.
cout<<"Before starting, it might help if you put the coins\n"
"into different piles in order to help you count them.\n"
"\n";

//First 'set' in the program. Finding out the number of
//pound coins the user has and changing it into pence.2
int POUNDS;
int PENCE;
int A;
(PENCE=100);
cout<<"So, now you've done that, let's get started. How many one\n"
"pound coins do you have? Press ENTER after putting the number in.\n";
cin>>POUNDS;
cin.ignore();
cout<<"Ok, that makes "<<POUNDS*PENCE<<" pence.\n"
"\n";
(POUNDS*PENCE==A);

//Second 'set' in the program. Finding out the number
//of 50 pence pieces they've got.
int ONE;
int B;
cout<<"Braw, now put in the number of 50 pence \n"
"pieces that you've got. Mind and press ENTER!\n";
cin>>ONE;
cin.ignore();
cout<<"\n";
(ONE*50==B);


//Third 'set'. Now, twenty pence pieces.
int TWO;
int C;
cout<<"Ok, how about 20 pence pieces?\n";
cin>>TWO;
cin.ignore();
cout<<"\n";
(TWO*20==C);

//Fourth 'set'. Ten pence pieces.
int THREE;
int D;
cout<<"So how many 10 pence pieces do you have?\n";
cin>>THREE;
cin.ignore();
cout<<"\n";
(THREE*10==D);

//Fifth 'set'. 5 pence pieces.
int FOUR;
int E;
cout<<"What about 5 pence pieces?\n";
cin>>FOUR;
cin.ignore();
cout<<"\n";
(FOUR*5==E);

//Sixth 'set'. 2 pence pieces.
int FIVE;
int F;
cout<<"Just the small change to go now!\n"
"How many 2 pences have you got?\n";
cin>>FIVE;
cin.ignore();
cout<<"\n";
(FIVE*2==F);

//Seventh 'set'. 1 penny peices.
int SIX;
int G;
cout<<"And finally, how many pennies?\n";
cin>>SIX;
cin.ignore();
cout<<"\n";
(SIX==G);

//The final output of the program - to display the
//user's piggybank's total. Multiply all variables
//A through F together to give the value in pence,
//and divide it by 100 to return the value in pounds.
cout<<"In your piggybank, you have "<<(A+B+C+D+E+F)<<" pence\n";
int TPENCE = (A+B+C+D+E+F+G);
int TPOUNDS = (TPENCE/100);
cout<<"Which is "<<TPOUNDS<<" in pounds!";

//To ensure the program doesn't close!
cout<<"Please press ENTER to terminate the program.\n";
cin.ignore();
return 0;
}
Last edited on
All the integers are added together, I also don't use the currency but heres what happens in the output posted by metulburr:
A = 500,
B = 2,
C = 5,
D = 10,
E = 50,
F = 100

added together that would be 500+100+50+10+5+2 or 667.
I am going to assume that B-E need to be multiplied by a proper value, for example multiplying B by 50 would mean that 2 50 pence pieces equals 100 pence.
thanks for the reply warnis.

i've tried to fix that (see above) after realizing that mistake you just pointed out, but it still doesn't want to work. currently it's returning a negative number for the total.
wow i feel retarted now lol
why didnt i think of that
Ah, I see, well, you see there is a difference between == and =,
== is the equality operator, = is the assignment operator and the one you want, so change all of the == to =
ah. fantastic thanks, thought it might've been something simple!
will change the code and get back to you.

tell me about it metullburr - i think i should retire as a human being.

EDIT: 'error: lvalue required as left operand of assignment' any clues man?
Last edited on
The problem you got is solved > A+B+C+D...== tpence< you forgot semicolons. And use lower case letters for variabe names. C++ is case sensitive...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>

using namespace std;

int main()

{
cout<<"Welcome to the piggybank counter. This program is\n"
"basically a glorified calculator! You put in the total\n"
"number of each type of coin in your piggy bank when prompted.\n"
"\n";
cout<<"Before starting, it might help if you put the coins\n"
"into different piles in order to help you count them.\n"
"\n";

int POUNDS;
int PENCE;
int A;
(PENCE=100);
cout<<"So, now you've done that, let's get started. How many one\n"
"pound coins do you have? Press ENTER after putting the number in.\n";
cin>>POUNDS;
cin.ignore();
cout<<"\n"
"Ok, that makes "<<POUNDS*PENCE<<" pence.\n"
"\n";
(POUNDS*PENCE==A);

int B;
cout<<"Braw, now put in the number of 50 pence \n"
"pieces that you've got. Mind and press ENTER!\n";
cin>>B;
cin.ignore();
cout<<"\n";

int C;
cout<<"Ok, how about 20 pence pieces?\n";
cin>>C;
cin.ignore();
cout<<"\n";

int D;
cout<<"So how many 10 pence pieces do you have?\n";
cin>>D;
cin.ignore();
cout<<"\n";

int E;
cout<<"Just the small change to go now!\n"
"How many 2 pences have you got?\n";
cin>>E;
cin.ignore();
cout<<"\n";

int F;
cout<<"And finally, how many pennies?\n";
cin>>F;
cin.ignore();
cout<<"\n";

cout<<"In your piggybank, you have "<<(A+B+C+D+E+F)<<" pence\n.";
int TPENCE;
int TPOUNDS;
( A+B+C+D+E+F == TPENCE);
(TPENCE/100==TPOUNDS);
cout<<"Which is "<<TPOUNDS<<" in pounds!";

cout<<"Please press ENTER to terminate the program.\n";
cin.ignore();
return 0;
}
Last edited on
cheers happykiller will edit all that.

any clue what 'error: lvalue required as left operand of assignment' means?
Are you trying to do some assignment like A+B = C?
i made one for american currency.

first i converted all coins into pennies and then divided it by 100
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>

using namespace std;

int main()

{
    cout << "Piggy bank" << endl;
    cout << "This program will add up all your change and give you the total." <<endl <<endl;

        float pennies, nickels, dimes, quarters, hdollars, dollars, total;


    cout << "How many pennies do you have: ";
    cin >> pennies;

         cout << "How many nickels do you have: ";
    cin >> nickels;

    nickels = (nickels * 5);

     cout << "How many dimes do you have: ";
    cin >> dimes;

    dimes *= 10;
     cout << "How many quarters do you have: ";
    cin >> quarters;

    quarters *= 25;

     cout << "How many half-dollars do you have: ";
    cin >> hdollars;

    hdollars *= 50;

     cout << "How many dollars do you have: ";
    cin >> dollars;

    dollars *= 100;

    total = (pennies + nickels + dimes + quarters + hdollars
             + dollars);
    total = (total / 100);
    cout << "You have $" << total;


}
Last edited on
yeah warnis - just got it to compile, had (FOUR*5=E) when it should've been (E=FOUR*5), when FOUR is the integer and 5's obviously just a numerical value to times the variable by.
whilst it compiles, it still doesn't work.

metullburr - your's is awesome, a lot shorter and to the point than mine i think. i'm possibly totally over complicating things.

any ideas why this is STILL returning a negative value?

#include <iostream>

using namespace std;

int main()

{
cout<<"Welcome to the piggybank counter. This program is\n"
"basically a glorified calculator! You put in the total\n"
"number of each type of coin in your piggy bank when prompted.\n"
"\n";
cout<<"Before starting, it might help if you put the coins\n"
"into different piles in order to help you count them.\n"
"\n";

int POUNDS;
int PENCE;
int A;
(PENCE=100);
cout<<"So, now you've done that, let's get started. How many one\n"
"pound coins do you have? Press ENTER after putting the number in.\n";
cin>>POUNDS;
cin.ignore();
cout<<"Ok, that makes "<<POUNDS*PENCE<<" pence.\n"
"\n";
(A=POUNDS*PENCE);

int ONE;
int B;
cout<<"Braw, now put in the number of 50 pence \n"
"pieces that you've got. Mind and press ENTER!\n";
cin>>ONE;
cin.ignore();
cout<<"\n";
(B=ONE*B);

int TWO;
int C;
cout<<"Ok, how about 20 pence pieces?\n";
cin>>TWO;
cin.ignore();
cout<<"\n";
(C=TWO*20);

int THREE;
int D;
cout<<"So how many 10 pence pieces do you have?\n";
cin>>THREE;
cin.ignore();
cout<<"\n";
(D=THREE*10);

int FOUR;
int E;
cout<<"What about 5 pence pieces?\n";
cin>>FOUR;
cin.ignore();
cout<<"\n";
(E=FOUR*5);

int FIVE;
int F;
cout<<"Just the small change to go now!\n"
"How many 2 pences have you got?\n";
cin>>FIVE;
cin.ignore();
cout<<"\n";
(F=FIVE*2);

int SIX;
int G;
cout<<"And finally, how many pennies?\n";
cin>>SIX;
cin.ignore();
cout<<"\n";
(G=SIX*1);


cout<<"In your piggybank, you have "<<(A+B+C+D+E+F+G)<<" pence\n";
int TPENCE = (A+B+C+D+E+F+G);
int TPOUNDS = (TPENCE/100);
cout<<"Which is "<<TPOUNDS<<" in pounds!\n"
"\n";

cout<<"Please press ENTER to terminate the program.\n";
cin.ignore();
return 0;
(B=ONE*B);
Right there, you're using B whilst it's still unassigned, and you probably want to replace it with 50 anyways
Pages: 12