Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,069 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,609 people online right now. Registration is fast and FREE... Join Now!




Please Help with my code

 
Reply to this topicStart new topic

Please Help with my code, Cant figure out where i went wrong

magic0198
14 Apr, 2008 - 12:44 PM
Post #1

New D.I.C Head
*

Joined: 14 Apr, 2008
Posts: 7

I have to display how many days it will take to reach 4,843,378,958.00 by starting out with one penny and doubleing every day. The code I wrote doesnt work. When i execute my program it crashes.

This is what i have:

CODE
#include <iostream>

using namespace std;

/*      Nick Mullins
        4-14-08
        Extra Credit2
        CS115-J001

        This program will display the number of days it will take
        starting with one pennie doubleing every day to get
        4,843,378,958.00
*/
         int main()
{

        int numday;
        float daypay;
        float total; //lcv
      
        numday=0;
        daypay=0.01;
        total=0.00;

        while(total<4,843,378,958.00){
        numday++;
        total=total+daypay;
        daypay=daypay*2.0;

        cout << "It took ";
        cout << numday;
        cout << " days to even 4,843,378,958.00;
        cout << endl;
        }
}


*Mod Edit: added code tags: code.gif
User is offlineProfile CardPM
+Quote Post

LoganLooker
RE: Please Help With My Code
14 Apr, 2008 - 12:57 PM
Post #2

New D.I.C Head
*

Joined: 1 Feb, 2007
Posts: 38


My Contributions
QUOTE(magic0198 @ 14 Apr, 2008 - 01:44 PM) *

I have to display how many days it will take to reach 4,843,378,958.00 by starting out with one penny and doubleing every day. The code I wrote doesnt work. When i execute my program it crashes.

This is what i have:

CODE
#include <iostream>

using namespace std;

/*      Nick Mullins
        4-14-08
        Extra Credit2
        CS115-J001

        This program will display the number of days it will take
        starting with one pennie doubleing every day to get
        4,843,378,958.00
*/
         int main()
{

        int numday;
        float daypay;
        float total; //lcv
      
        numday=0;
        daypay=0.01;
        total=0.00;

        while(total<4,843,378,958.00){
        numday++;
        total=total+daypay;
        daypay=daypay*2.0;

        cout << "It took ";
        cout << numday;
        cout << " days to even 4,843,378,958.00";
        cout << endl;
        }
}



Try adding a,

System("PAUSE");

at the end of your program like this....

and also add a " to your

CODE
cout << " days to even 4,843,378,958.00;


like this

CODE
cout << " days to even 4,843,378,958.00";



CODE
#include <iostream>

using namespace std;

/*      Nick Mullins
        4-14-08
        Extra Credit2
        CS115-J001

        This program will display the number of days it will take
        starting with one pennie doubleing every day to get
        4,843,378,958.00
*/
         int main()
{

        int numday;
        float daypay;
        float total; //lcv
      
        numday=0;
        daypay=0.01;
        total=0.00;

        while(total<4,843,378,958.00){
        numday++;
        total=total+daypay;
        daypay=daypay*2.0;

        cout << "It took ";
        cout << numday;
        cout << " days to even 4,843,378,958.00;
        cout << endl;
        }

    system("PAUSE");
    return EXIT_SUCCESS;

}



ALL WORKS! I TESTED smile.gif

This post has been edited by LoganLooker: 14 Apr, 2008 - 01:10 PM
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Please Help With My Code
14 Apr, 2008 - 01:05 PM
Post #3

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,198



Thanked: 213 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
You had a few errors including putting your totals in the loop, using commas in your number (computers don't use commas, people do) and forgetting to close one of your strings with double quotes.

cpp

#include <iostream>

using namespace std;

/* Nick Mullins
4-14-08
Extra Credit2
CS115-J001

This program will display the number of days it will take
starting with one penny doubling every day to get
4,843,378,958.00
*/
int main()
{

int numday;
float daypay;
float total; //lcv

numday=0;

// Specify these are floats with "f"
daypay=0.01f;
total=0.00f;

// Remove the commas, computers don't use them here
while(total < 4843378958.00){
numday++;
total = total + daypay;

// Again use "f" to let the computer know you are using a float.
daypay = daypay * 2.0f;

// Show the pay for that day
cout << daypay << endl;
}

// Show results AFTER the loop.
cout << "It took ";
cout << numday;
cout << " days to even 4,843,378,958.00";
cout << endl;

return 0;
}


Play with that and you will see the pay for each day and how many days it took you to go over the amount. smile.gif

Be sure to put in a pause or something so you can see the output too.

Enjoy!

"At DIC we be coin doubling code ninjas... after X days we grow in 2^X strength!" decap.gif

This post has been edited by Martyr2: 14 Apr, 2008 - 01:07 PM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 06:58PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month