2013-10-26 5 views
-2

21 세 생일에 할머니가 저축 예금 계좌를 개설하고 계좌에 1000 달러를 입금합니다. 저축 계좌는 계좌 잔금에 3 %의이자를 지불합니다. 더 이상 돈을 계좌에 입금하지 않고 계좌에서 돈을 인출하지 않으면 1 년에서 5 년 사이에 저축 계좌가 얼마의 가치가 있습니까?while 루프 C++ 기본이자 계산기

답변을 제공하는 프로그램을 만드십시오. 다음 공식을 사용하여 해답을 계산할 수 있습니다. b = p * (1 + r) n. 공식에서 p는 원금 (예금 금액), r은 연이율 (3 %), n은 연수 (1 ~ 5), b는 저축 예금 계좌의 잔액 n 번째 해의 끝. for 루프를 사용하십시오.

는 어떤 도움을 크게 이것은 내가 지금까지 무엇을 가지고

감사하고 내가 할 모든

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

void main() 
{ 
// Inputs // 

double princ = 0.0; 
double rate = 0.0; 
int years = 0; 
int year = 1; 
double total = 0.0; 

// Ask User For INFO // 

cout << "What is the principle? "; 
cin >> princ; 
cout << "What is the rate in decimal? "; 
cin >> rate; 
cout << "how many years? "; 
cin >> years; 



for (double total; total = princ*(1+rate)*year;) 
{ 
cout << "The balance after year " << year << " is "<< total << endl << endl; 
year += 1; 
} 

while((years + 1)!= year); 

system("pause"); 
} 
+4

'void main()','system ("pause")'... 사람들은이 물건을 어디에서 배울 것인가? – dreamlax

+0

@dreamlax 학생들이 처음 배우면서 Visual Studio를 사용하게되면 Visual Studio가 프로그램이 끝나 자마자 자동으로 디버깅 콘솔을 닫으므로 혼란스러워집니다. 따라서 강사와 샘플 연습에서는 시스템 ("PAUSE")을 자주 추가합니다. ;'끝에 (Windows에서는) "Press any key to continue ..."와 같은 메시지가 출력되고 계속하기 전에 입력을 기다립니다. –

답변

0

귀하의 문제는 당신이 어떻게 든 forwhile 루프를 혼합이다 무한 루프됩니다. 또한 main 기능은 이미 코멘트에 언급 void를 반환하지해야

for (double total; (years +1) != year; total = princ*(1+rate)*year) 
{ 
cout << "The balance after year " << year << " is "<< total << endl << endl; 
year += 1; 
} 

대신 int main()

을해야한다 :

대신

for (double total; total = princ*(1+rate)*year;) 
{ 
cout << "The balance after year " << year << " is "<< total << endl << endl; 
year += 1; 
} 

while((years + 1)!= year); 

당신은 아마 이런 걸 원하는 것

1

for 루프가 어떻게 작동하는지 오해했습니다. 그것은 당신의 예제에서 특정 수의 무언가를하기 위해 사용됩니다. 다음과 같은 것 :

double interest = 1.0 * rate: 
double accumulated = 1.0 * interest; 

for (auto i=1; i < years; ++i) { 
    accumulated *= interest; 
    cout << "The balance after year " << i << " is " << (princ * accumulated) << std::endl; 
}