I wrote the following code for a homework problem, but it's stuck in an infinate loop. I'm not sure how to correct that problem. The other issue is I want the answer to be given in two decimal places and not rounded in any way and it appears that my answers are being rounded. Any help would be greatful!

// this program will calculate the salary after a set number of days
// the user will input the number of days to obtain the salary
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{
	// declare variables
	float n = 0;
    float total = 0;
    float dollaramount = 0;
	
	// prompt user for the number of days 
	cout << "Please enter the number of days to obtain the sum of $0.01 per\n"
    "day that doubles for the amount of days that you input:";
    cin >> n;
    
    while (n >= 1 || n <= 30)
    {
          total = pow(2, n) - 1;
          dollaramount = total/100;
          
          cout << "The total dollar amount will be:" << setprecision (2) 
          << fixed << dollaramount << " on the " << n << " day.";
          
          }
     
     cout << " You entered a number outside of 1 through 30, please\n"
          "start over.";
           
    system ("pause");

	return 0;
}

Dani AI

Generated

The infinite loop here had two causes that other replies already hinted at: the loop condition logic and the loop body not changing the input. and were right that you need the range check to require both bounds (not the OR that always succeeds), and was right that a loop must change or re-read the controlling value. For a single calculation you do not need a while loop at all — just validate days once and compute; if you want repeated prompts, use a loop that reads a new value inside the body (a do/while or while(true) with a break).

For money calculations avoid floating-point rounding. pow returns floating types and formatting with fixed + setprecision will round, not truncate. A safe approach is to work in integer cents: compute the exact number of pennies (2^days - 1), then split into dollars and cents for display. That gives exact, non-rounded two-decimal output.

Example approach (keeps the logic clear and avoids rounding/precision problems):

#include <iostream>
#include <iomanip>

int main() {
    int days;
    if (!(std::cin >> days)) return 0;
    if (days < 1 || days > 30) {
        std::cout << "You entered a number outside 1..30\n";
        return 0;
    }

    unsigned long long pennies = (1ULL << days) - 1; // exact 2^days - 1
    unsigned long long dollars = pennies / 100;
    unsigned int cents = pennies % 100;

    std::cout << "Total: " << dollars << '.' << std::setw(2) << std::setfill('0')
              << cents << " on day " << days << '\n';
    return 0;
}

Notes: use a 64-bit unsigned type so 2^30 - 1 fits comfortably; avoid pow for integer powers when exact integer results are needed. For background on pow returning floating types and on shift semantics see the cppreference pages on pow and on shift operators: std::pow and shift operators.

Recommended Answers

All 4 Replies

while (n >= 1 && n <= 30) since the while loop runs while it's true you want it to cut out when either one of these is false. It's a bit counterintuitive at first.

To solve your other problem, I believe you need

float n = 0f;
float total = 0f;
float dollaramount = 0f;

to specify that those are float constants.

In the 'while' condition, you are not changing the 'n' value and that is why it is going in infinite loop.

for the infinite loop, while (n >= 1 || n <= 30) so -96 for example is valid because its below or equal to 30 and 5023 is also valid because it is bigger or equal to 1. Because with || (or) only one statement must be true.

to correct that, you must do : while (n >= 1 && n <= 30) like Jonsca said. Because any 'n' must respect both of these boundaries.

on top your 'n' value never changes...

while (n >= 1 || n <= 30)
{
      total = pow(2, n) - 1;
      dollaramount = total/100;

      cout << "The total dollar amount will be:" << setprecision (2) 
      << fixed << dollaramount << " on the " << n << " day.";

u must enter : cin>> n; // to get a new number.

      }

Not sure about rounding off.

Okay! I finally made it work. In order for me to have a proper decimal number print out, line 14 needed to be set up like this...

double long dollaramount = 0;

I also had to set the precision up properly for the " on day " << n <<, part of the program.

Thanks for all of your help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.