2017-11-13 4 views
-2

私はこのコードを実行していますが、コードを最初から再起動するループを作っていますが、月の日付を変更してプログラムを再起動し、ユーザーに何ヶ月か尋ねてプログラムに一定の私が1ヶ月目を最初の1ヶ月として追加したので、時間の量。Cで月の日付を変更している間にコードを繰り返す方法は?

#include<stdio.h> 
#include<stdlib.h> 
float twag(float amount,float rate) 
{ 
    float si; 
    si = (amount * rate)/100; 
    return si; 
} 

float twag2(float amount,float rate) 
{ 
    float er; 
    er = twag(amount, rate) + amount; 
    return er; 
} 

int main() 
{ 
    char answer; 
    do { 
     float amount; 
     float rate; 
     float si; 
     float er; 

     printf("Month1\n"); 

     printf("\nEnter intial deposit : "); 
     scanf("%f", &amount); 

     printf("\nEnter Rate of Interest : "); 
     scanf("%f", &rate); 

     printf("\nSimple Interest : %.2f \n", twag(amount,rate)); 

     printf("\nEnd Payment: %.2f \n",twag2(amount,rate)); 

     if (amount <= 100) 
      printf("interest rate should be 10%%\n"); 

     else if (amount <= 200) 
      printf("interest rate should be 50%%\n"); 

     else if (amount <= 500) 
      printf("interest rate should be 80%%\n"); 

     else if (amount >= 500) 
      printf("interest rate should be 90%%\n"); 
     system("PAUSE"); 


     printf("\nPress Y to continue to the second month or Press any Key To 
     Exit"); 
     scanf(" %c", &answer); // 
    } 
    while (answer == 'y' || answer == 'Y'); 

    return 0; 
} 
+0

フォーマット/インデントのようになりますので、uのループ、そしてあなたの関心は、ループの中に一定でなければならない:( –

+0

イム申し訳ありません初心者:/ –

+1

クリーンそれを試してみてください。悪いF/Iは、スコープ/フロー制御のバグに加えて、他の人(助けたいと思うかもしれない人のようなもの)を理解するために避けがたい努力をしなければならない:( –

答えて

0

あなたは毎月の金利を適用していると仮定すると、あなたは、少なくとも今日の日付から始まる、現在の月と年の記録を保持する必要があるので、それを行う方法はlocaltime

void get_date(int *month, int *day, int *year) 
{ 
    struct tm *current; 
    time_t timenow; 
    time(&timenow); 
    current = localtime(&timenow); 
    *month = current->tm_mon+1; 
    *day = current->tm_mday; 
    *year = current->tm_year+1900; 
} 
を使用することです

私はそれ以外にも、あなたは、彼らがそれぞれの時間ヨーヨーをリセットしようとしていることを意味ループ、内部であなたのループ変数を定義し、system("PAUSE")が悪い理由であなたread hereを提案あなたのコードは、この

#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 

float twag(float amount,float rate); 
float twag2(float amount,float rate); 
void get_date(int *month, int *day, int *year); 

int main() 
{ 
    char answer; 
    float amount, rate, si, er; 
    int day, month, year; 

    printf("\nEnter intial deposit : "); 
    scanf("%f", &amount); 

    printf("\nEnter Rate of Interest : "); 
    scanf("%f", &rate); 

    get_date(&month, &day, &year); 

    do 
    { 
     // increment date by 1 month 
     if (++month > 12) month=1, year++; 

     printf("\nDate %d/%d/%d \n", day,month,year); 
     printf("\nSimple Interest : %.2f \n", twag(amount,rate)); 
     printf("\nEnd Payment: %.2f \n",twag2(amount,rate)); 
     amount = twag2(amount,rate);  

     printf("\nPress Y to continue to the next month or Press any Key To Exit"); 
     scanf("%c", &answer); 
    } 
    while (answer == 'y' || answer == 'Y'); 

    return 0; 
} 
関連する問題