Thee Oomer
Thee Oomer

Reputation: 27

Looping failure

I need to make the program loop until the user inputs "XXXX" as the name. Problem is, my code only loops once at most and exits. I have tried messing with do ... whiles for an entire day and can't seem to figure this out but I feel like i'm really close!

Edit: You guys are awesome! Made both of the changes but now it skips the name prompt and goes straight to the deposit prompt. How would i get it to include the name prompt in the loop?

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int main() {

string name, XXXX;
float deposit, time, interest, total, totinterest;
int compound;

cout << left;

int flag = 0;

do {

    cout << "Enter your name or XXXX to stop: ";
    getline(cin, name);
    cout << name << " enter the following information: " << endl;


    if (name == "XXXX"){
        flag = 1;
    }else 
    {

        cout << setw(60) << "\tAmount on deposit: ";
        cin >> deposit;
        if (!deposit || deposit < 0){
            cout << "\tPlease enter a positive number! ";

        }

        cout << setw(60) << "\tYears on deposit: ";
        cin >> time;
        if (!time || time < 0){
            cout << "\tPlease enter a positive number! ";
            return(0);
        }

        cout << setw(60) << "\tNumber of times the interest is compounded per year: ";
        cin >> compound;
        if (!compound || compound < 0){
            cout << "\tPlease enter a positive number! ";
            return(0);
        }

        if (time >= 5)
            interest = .045;
        else if (time >= 4)
            interest = .04;
        else if (time >= 3)
            interest = .035;
        else if (time >= 2)
            interest = .025;
        else if (time >= 1)
            interest = .02;
        else if (time < 1)
            interest = .015;

        total = (deposit)*pow((1 + interest / compound), (compound * time));
        totinterest = total - deposit;

        cout << left << setprecision(2) << fixed;
        cout << endl;
        cout << setw(15) << "Name " << setw(14) << "Years" << setw(18) << "Deposit Amount" << setw(18) << "Interest Earned " << setw(18) << "Total" << endl;
        cout << "===============================================================================" << endl;
        cout << setw(15) << name << setw(14) << time << setw(1) << "$" << setw(17) << deposit << setw(1) << "$" << setw(17) << totinterest << setw(1) << "$" << setw(18) << total << endl;
        cout << endl;
        cout << "Thank you for using tax program. Have a nice day. " << endl;
        return(0);

    }

} while (flag = 0);

}

Upvotes: 2

Views: 98

Answers (6)

doptimusprime
doptimusprime

Reputation: 9395

Because you are using = instead of == in while condition.

Change flag = 0 to !flag or flag==0.

Apart from this, you have return 0 which is unconditional. This return statement will make the program to exit anyhow.

Upvotes: 4

Ron Thompson
Ron Thompson

Reputation: 1096

You have a return(0); in your else clause. Also, you have = instead of == in your while loop, which (after you remove the return statement) is a recipe for an infinite loop.

Upvotes: 2

shauryachats
shauryachats

Reputation: 10385

return(0) will exit the main() and the program will terminate. Hence, remove it from the source.

Also the condition in while (flag = 0) will always return false. Change it to while (flag == 0) otherwise it would result in a loop that iterates only once.

And finally, you need <math.h> for the pow() function.

#include <math.h>

Upvotes: 5

ghostDrankMyCoffee
ghostDrankMyCoffee

Reputation: 31

Firstly, you want your while condition to be while(flag == 0) and not while (flag = 0)

Also, if the user enters an invalid value of time or deposit or compound(such as less than 0), then your code prints out an error message and quits. Instead, you want to put a while loop there that keeps prompting the user for a valid input as long they keep giving an incorrect input.

Finally, you wanna be using something like myString.compare(otherString) instead of the == operator

Upvotes: 3

atomCode
atomCode

Reputation: 902

You have to include

#include <math.h> 

for your pow function to work. I get an error that pow is not defined.

Upvotes: 3

rm4
rm4

Reputation: 721

return(0) will get you out of the main. Remove that line and retry.

Upvotes: 5

Related Questions