LOOP trouble

All help is appreciated,

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.

Thank you
- 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
Last edited on
I think there's a formula for that

like in binary

2^0 + 2^1 + 2^2 + ... + 2^31 or simply (2^32 - 1) if I'm not mistaken
Thank you Disch...
one more question should i use a while loop?

and yes the binary did work out correctly.
Last edited on
any loop will do.
you can use any loop, yes.

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.
So following Zhuge:
1
2
3
for(double i=0.0;i<31.0;i++)
double sum+=pow(2.0,i);
cout<<"Sum donated= "<<sum<<endl;
Last edited on
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.
@buffbill
1
2
3
for(double i=0.0;i<31.0;i++)
double sum+=pow(2.0,i);
cout<<"Sum donated= "<<sum<<endl


or simply

 
double sum = pow(2.0, i+1) - 1;


no need to use for loop.
Last edited on
Zhuge....no the for loop remains active for the 31 iterations and...yes you have declared the size of sum+= comprising the 31 iterated values
One more solution
#include "stdafx.h"
#include <iostream.h>
#include <conio.h>
#define NO_OF_DAYS 31

int main(int argc, char* argv[])
{
int sum = 0 , i = 0 , num = 1;

do
{

sum += (num);
num = num + num;
i++;

}while(i < NO_OF_DAYS);

cout<<"\nThe total sum is = "<< sum<<endl;




return 0;
}
num = num + num;//ugly

you can simply write
1
2
3
num = 2*num; //for short
//or even
num *= 2;


and you include "stdafx.h"?? for what reason?
Last edited on
there is no need for "stdafx.h" .. it should be removed .

you need the "stdafx.h" if your using visual c++ 2008 express edition, it's what i'm using and wont run without it, otherwise you don't need it
Last edited on
Hello - i am having difficulty with a loop.

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//

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>

using namespace std ;
int main()


{

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;




return (0); // terminate with success.
}
1
2
3
4
{
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. 
Thank you so much for your help olredixsis.
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 .
Topic archived. No new replies allowed.