I am currently in a jam here with writting a program, it states that A person will donate 1 penny on day one then double the donation each day for 31 days.
In other words on day 2 he donates two cents, day 3 he donates 4 cents ( at the end of day 3 he has donated a total of 7 cents.)
I need to figure out how much he donated at the end of the month.
I am open to any suggestions on how to get this program started.
- you'll need to loop 31 times (once for each day)
- you'll need to keep track of the total money donated
- you'll need to keep track of how much the person donated today
EDIT: to further clarify... "keep track" = make a variable to represent that
Although a for loop makes the most sense in this situation. Whenever you're counting up to something, that's generally a sign that you should use a for loop.
Um, heres the code i got for it, but i'm not sure if it's right, but here it is
--------------------------------------------------------------------------------------------------
#include <iostream>
using namespace std;
int main()
{
int day, donated;
day=1;
donated=0;
if (day=1)
{donated= 1;
day=day+1;}
while (day!=31)
{
donated= donated*2;
day= day+1;}
cout << "Days past " << day<< endl;
cout << "Total donated " << donated;
system ("PAUSE");
return 0;
}
That's looking good, except for a small logic error. You aren't simply doubling the amount he has donated, you need to add the amount already donated to the doubled amount.
In that case, your sum is local to the for loop, which means it's gone after the loop exits, and reset each iteration, which isn't what you want. Besides this, what you are trying to do looks okay but I haven't throughly tested it to make sure.
I am writing a program for a number game. I have the random number and seed function working fine. What I am asked to do is count the number of guesses it takes to guess the random number, and write a statement for three different conditions; guess random number in <= 10, == 10 or >10 trys.
I am so stuck.
I am not certain how to count the guesses properly and gather the correct total # of guesses.
// Number Guessing Game//
// Applies the rand, srand() functions and loops//
srand(time(0));
int randomNumber = rand() % 1000 + 1; // generates random number between 1 and 1000.
int guess = 0;
int total = 0;
int numTrys = 0;
cout << randomNumber << endl ;
cout << " Welcome to Guess-the-Number Game." << endl ; // Title of program.
cout << "" << endl ;
cout << " I have a secret number between 1 - 1000." << endl ;
cout << " Can you guess my number ?." << endl ;
cout << "" << endl ;
do
{
cin >> guess ;
cout << "" << endl ;
if (guess < randomNumber) // If guess is lower than the random number.
cout << " Too low. Try again. " << endl ;
if (guess > randomNumber)
cout << " Too high. Try again." << endl; // If guess is higher than the random number.
}while (guess != randomNumber); // While guess is not equal to the random number.
// count number of guesses.
{
for (int numTrys=1; numTrys<= 100; numTrys++)
(total = total + numTrys);
}
if (numTrys <= 10) // If numer of trys is 10 or less.
cout << " Either you know the secret or you got lucky!." << endl ;
else if (numTrys == 10)
cout << " Ahah! you know the secret!." << endl;
else if (numTrys >10)
cout << " You should be able to do better! " << endl;
cout << " " << endl ;
cout << "The total number of guesses you made was " << total << endl;
{
for (int numTrys=1; numTrys<= 100; numTrys++)
(total = total + numTrys);
}
you misplaced the open curly brace.
1 2 3
for (int numTrys=1; numTrys<= 100; numTrys++){
(total = total + numTrys);
}
Well you increment the numTrys in your do-while loop
1 2 3 4 5 6 7 8 9 10
do
{
cin >> guess ;
++numTrys;//here
cout << "" << endl ;
if (guess < randomNumber) // If guess is lower than the random number.
cout << " Too low. Try again. " << endl ;
if (guess > randomNumber)
cout << " Too high. Try again." << endl; // If guess is higher than the random number.
}while (guess != randomNumber); // While guess is not equal to the random number.
As according to
we will not this part of the code .
// count number of guesses.
for (int numTrys=1; numTrys<= 100; numTrys++)
{
(total = total + numTrys);
}
because i suspect that numTrys will be equal to total number .