My wish fountain is greedy
Mar 9, 2014 at 10:33pm Mar 9, 2014 at 10:33pm UTC
Write your question here.
Hi,I'm really new to programming and c++ is the language I chose to get started.
I'm having a great time learning this language and love to make these little programs.Right now I'm trying to practice functions and having trouble with one little programme I've made.It seemed to work fine until I realised the while loop cant end because soFar doesnt get incremented.So please someone tell me whats wrong with my code and also,what I could\should have done.Keep in mind that I'm only begining to learn.
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>
#include <string>
using namespace std;
int throwCoin();
const int nickel = 5;
const int dime = 10;
const int quarter = 25;
int soFar = 0;
int main()
{
const int wishComeTrue = 100;
string choices[3];
int possibleChange = 0;
choices[possibleChange++] = "1.Nickel" ;
choices[possibleChange++] = "2.Dime" ;
choices[possibleChange++] = "3.Quarter" ;
cout << "Please choose what coin you will put in the fountain\n\n" ;
for (int i = 0;i < 3;++i)
{
cout << choices[i] << endl;
}
throwCoin();
while (soFar < wishComeTrue)
{
cout << "Your wish will not come true for such a small price\n"
"So far you have: " << soFar << "\n"
<<"Put a little more in" << endl;
throwCoin();
}
cout << soFar << "That's pretty expensive for a wish to come true" ;
return 0;
}
int throwCoin()
{
int number;
cin >> number;
if (number = 1)
{
soFar + nickel;
}
else if (number = 2)
{
soFar + dime;
}
else if (number = 3)
{
soFar + quarter;
}
return soFar;
}
Mar 9, 2014 at 10:38pm Mar 9, 2014 at 10:38pm UTC
1 2
soFar + nickel;
soFar += nickel;
etc.
Mar 9, 2014 at 10:49pm Mar 9, 2014 at 10:49pm UTC
1 2 3 4
if (number == 1)
{
//do blah
}
not
1 2 3 4
if (number = 1)
{
//do blah
}
Note == not =
Mar 10, 2014 at 6:26pm Mar 10, 2014 at 6:26pm UTC
Ok thanks both of you.Works fine now.
Mar 10, 2014 at 6:30pm Mar 10, 2014 at 6:30pm UTC
Remember, number = 1
assigns the value 1 to the variable "number", whereas number == 1
checks if the value of "number" is equal to 1.
Topic archived. No new replies allowed.