how do I use the variable from a For loop outside the loop in C++? -
So I'm a little distraught in my programming skills and I have no real experience other than college classes I have taken in the past I'm working on a program for a class, but I'm in a turnaround; I do not know how to use a variable's value inside a Therefore, when I run the program, everything runs fine, but the value of the counter is always shown as "0" in the last I think why this is doing this, I think..and it is because of the declaration "int counter = 0" outside of the loop, so I think that after the loop leaves it Returns to that value If I do not start the counter variable, I get an error, and if I declare the value inside the loop, then I get an error in trying to use it in the CAIT statement. That's up to I'm not even sure that my loop is correctly structured ... basically I want to add this weekly variable, as long as What do you want to do Your manifesto outside the loop allows you to end the For your problem, it seems that you have never initiated the for loop outside of a loop. Here's the code that I am referring to:
Include "iostream" using the namespace std; Int want, need; Int counter = 0; Four responses, cont; Int diff (int want, int is); Int main () {cout & lt; & Lt; "Welcome! \ N"; Cout & lt; & Lt; "This program will help you reach your savings goals! \ N"; Cout & lt; & Lt; "Would you like to use this program? (Y / n) \ n"; Cin & gt; & Gt; reaction; While (feedback == 'Y') {cout & lt; & Lt; "Please enter all dollars only in full dollars! \ N"; Cout & lt; & Lt; "Please enter the amount of money you want to save: $"; Cin & gt; & Gt; Want; Cout & lt; & Lt; "\ N Please enter the amount of money you saved: $"; Cin & gt; & Gt; is; If (is> = want) {cout & lt; & Lt; "You have already reached or exceed your goal, this program will not be able to assist you! \ N"; System ("pause"); Return 0; } cout & lt; & Lt; "\ NYou need to save $" & lt; & Lt; Difference (want, is) & lt; & Lt; "More money to reach your goal! \ N"; Cout & lt; & Lt; "Do you want to help me with the savings plan?"; Cin & gt; & Gt; remaining part; While (cont == 'y') {int menu; Cout & lt; & Lt; "Please choose from the following options: \ n"; Cout & lt; & Lt; "1 - Daily Savings Plan \ n"; Cout & lt; & Lt; "2 - Weekly Savings Plan \ n"; Cout & lt; & Lt; "3 - Monthly Savings Plan \ n"; Cout & lt; & Lt; "Enter the associated number of your choice: \ n"; Cin & gt; & Gt; Menu; Switch (menu) {Case 1: {int daily; Cout & lt; & Lt; "You have selected the daily savings plan \ n"; Cout & lt; & Lt; "How much money can you save every day? $"; Cin & gt; & Gt; daily; For (int x = daily; x & lt; need; x ++) {daily = daily + daily; Counter ++; } cout & lt; & Lt; "\ N It Will Take You" & lt; & Lt; Counter & lt; & Lt; "Day to reach your goal! \ N"; break; } Case 2: {Weekly Weekly; Cout & lt; & Lt; "You have chosen the Weekly Savings Plan \ n"; Cout & lt; & Lt; "How much money can you save every week? $"; Cin & gt; & Gt; Weekly; For (int x = weekly; x & lt; need; x ++) {counter ++; } cout & lt; & Lt; "\ N It Will Take You" & lt; & Lt; Counter & lt; & Lt; "Week to complete your goal! \ N"; break; } Case 3: {int monthly; Cout & lt; & Lt; "You have chosen the monthly savings plan \ n"; Cout & lt; & Lt; "How much money can you save each month?" Cin & gt; & Gt; Monthly; For (int x = monthly; x & lt; need; x ++) {monthly = monthly + monthly; Counter ++; } cout & lt; & Lt; "\ N It Will Take You" & lt; & Lt; Counter & lt; & Lt; "Months to reach your goal! \ N"; break; } Default: cout & lt; & Lt; "You made an invalid selection"; Cout & lt; & Lt; "Do you want to see a separate savings plan? (Y / n) \ n"; Cin & gt; & Gt; remaining part; }}}} Int diff (int want, int) {want to return - is; }
cout statement
x = need < / Code> I also accept that for this Stems are repeated, then comes the week of the output should hope all understand as numbers; Any and all help is appreciated.
border (double (need / weekly) and that round needs to be divided by weekly.
cout value to
the
for the loop Does not affect
requirement , so you never need to register for your loop Not defined because undefined is less than or equal to 0.
Comments
Post a Comment