#setprecision question

1 messages · Page 1 of 1 (latest)

sly crest
#

ok I am trying to use setprecision to set a dollar amount.

'''
#include <cstdlib>
#include <iostream>
#include <iomanip>

using namespace std;

int main() {
int days;
double total;
double daily;

cout << "App to calculate pay if 1 cent was earned on day 1 and doubled every day\n";
cout << "------------------------------------------------------------------------\n\n";
cout << "Enter number of days: ";

cin >> days;

for (int i = 1; i <= days; i++) {
    total += daily;
    daily *= 2;
}

cout << "Your total pay will be $" << total / 100 << endl;
return 0;

}
'''

gets me this output "Your total pay will be $1.52017e-317"

when i change the final cout to this...
'''
cout << "Your total pay will be $" << fixed << setprecision(2) << total / 100 << endl;
'''

I get "gets me this output "Your total pay will be $0.00"

It makes everything zero dollars?

I am confused.........

restive gateBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

restive gateBOT
#
How to Format Code on Discord
Markup

```cpp
int main() {}
```

Result
int main() {}
prisma totem
#

` not '

sly crest
#

OK again sorry

#

I did make an attempt, does that count 😦

prisma totem
#

ok I am trying to use setprecision to set a dollar amount.

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

using namespace std;

int main() {
    int days;
    double total;
    double daily;
    
    cout << "App to calculate pay if 1 cent was earned on day 1 and doubled every day\n";
    cout << "------------------------------------------------------------------------\n\n";
    cout << "Enter number of days: ";
    
    cin >> days;
    
    for (int i = 1; i <= days; i++) {
        total += daily;
        daily *= 2;
    }
    
    cout << "Your total pay will be $" << total / 100 << endl;
    return 0;
}

gets me this output "Your total pay will be $1.52017e-317"

when i change the final cout to this...

cout << "Your total pay will be $" << fixed << setprecision(2) << total / 100 << endl;

I get "gets me this output "Your total pay will be $0.00"

It makes everything zero dollars?

I am confused.........

sly crest
#

I was not able to edit it

#

I did try

prisma totem
#

total and daily are not initialised

#

but you are using them

#

that's UB

#

also how can you do total += daily; daily *= 2; if daily has no value

#

also don't do using namespace std if you can help it

sly crest
prisma totem
sly crest
#

!solved

restive gateBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

shrewd oyster
#

did you initialized them?
total = 0, daily = 1

modern bramble
# prisma totem that's UB

@sly crest Btw, I told you exactly the same thing just an hour prior to this:

Message 1:
Btw, as of right now this code has UB because you never initialized neither total nor daily. ( [Source](#1346208058661011579 message) )

Message 2:
When you write

double total;
double daily;
```then none of these two local variables actually have a value, which means trying to retrieve their value isn't possible (well, more correctly: Doesn't make sense). ( [Source]([#1346208058661011579 message](/guild/331718482485837825/thread/1346208058661011579/p/1346208058661011579/#msg-1346211019176935485)) )

Message 3:
In your `total += daily` line you retrieve the value of both `total` and `daily` before setting either of them to a value (Source immediately below Message 2).


If you don't understand an answer then don't be silent about it, but don't go to a new thread and post the unfixed code there.