Hi guyz, am new to C++ and trying to get a hang of the concept.
I was practicing an example i saw somewhere and basically trying to count the number of years at which it would take a user given input account balance to go either equal to zero or below zero.
I came up with this but for some reason it has got me brainstorming....
int main (){
int years = 1;
while (check){
cout << "Please input the starting balance: ";
cin >> bank_acct_start;
do{
for ( years=1 ; years<5000 ; years++ ){
balance = bank_acct_start - 500;
}
}while (balance>=0);
cout << years << endl;
}
firstly, the condition in d for parenthesis where i had "years<5000", i got a bit confused on what comparism to make there and then the code for some reason does not print the number of years at which the user inputted balance falls to either 0 or less for me...i need clarification please.
#include <iostream>
usingnamespace std;
int main ()
{
//make a variable that stores the amount of years, and set its first value 1
int years = 1;
int balance = 0;
bool check = true;
//until check is false, keep looping the same code
while (check)
{
//show text in the console
cout << "Please input the starting balance: ";
//allow the user to type in something, when the user presses enter whatever he typed in will go to the balance variable
cin >> balance;
//do the code in this loop at least one time
do
{
//set years to 1, if years is less than 5000 then do the code in this loop and at the end, add 1 to years
for (years = 1 ; years < 5000 ; years++)
{
//for every year (loop), decrease the balance by 500.
balance -= 500;
}
}
while (balance >= 0); //keep doing this as long as the balance is positive
//show the number of the years, should be 5000, because that's what the value is when the for-loop ends
cout << years << endl;
}
}
Thanks Zephilinox (491) but what i actually need to achieve is the number of years the user input would be 0 or less, for instance, if the user input was 1000, it would only deduct 500 twice leaving the number of years to be 2. That's what the cout statement should say ..."2" and not "5000". could it be i have a problem with my for loop?