ashumrani
ashumrani

Reputation: 56

Couldn't get my C++ program to display output

I am supposed to write a program that display's the income in cents for the number of days the user inputs. The income is supposed to be doubled for each day.

I don't know yet if my arithmetic is correct but, my problem is that when I run the code I don't get an output for the last part of the code where the calculation takes place.

Your Help is greatly appreciated,

#include <iostream>
#include <iomanip>

using std::cout;
using std::cin;
using std::fixed;
using std::setprecision;
using std::endl;

int main()
{
    int days = 0;
    double cents = 0.00;

    cout << "Enter the number of days you want income to be calculated for ";
    cin >> days;

    while((days < 1)||(days > 30))
    {
        cout << "\nInvalid entry try a number from 1 to 30 ";
        cin >> days;
    }

    while(days!=days)
    {
        cents = (pow(0.01, 2) + cents);
        cout << "\nYour income for " << days << " days is " << fixed << setprecision(2)
            << cents << " cents " << endl;
        days++;
    }

    system("PAUSE");
    return 0;
}

Upvotes: 0

Views: 88

Answers (3)

paxdiablo
paxdiablo

Reputation: 881093

while(days!=days)

Other than a floating point NaN (which is considered equal to nothing, including itself), there is never a situation where this would be true.

Your loop would be better written as (assuming your cents calculation is correct, something I haven't checked):

for (int day = 0; day < days; day++) {
    cents = (pow(0.01, 2) + cents);
    cout << "\nYour income for " << day + 1 << " days is " << fixed << setprecision(2) << cents << " cents " << endl;
}

Upvotes: 1

The Apache
The Apache

Reputation: 1074

As everyone answered, you made a typo (i.e.. while(days != days)

The correct way to display is to introduce a new variable that can be used as an iterator.

int day = 0;
while(day != days)
{
    day++;
    ....
}

Upvotes: 0

Luke Joshua Park
Luke Joshua Park

Reputation: 9795

I expect this is a typo, or just a misunderstanding on your part, but the following condition:

while(days!=days)

Will never be true. Thus the code inside the following braces will never be executed. days will always be equal to days.

Upvotes: 1

Related Questions