Hi, I have to convert some of my programs from C to C++. I think I'm doing it right, but I'm getting errors. This is my original program:
and this is what I have so far in my converted program:Code:/* million.c by Aron 10/03/2005 */ /* This program asks the user to input a dollar * amount and an interest rate, and the program * will calculate how long it will take to reach * $1 million. Input comes from the keyboard and * output is sent to the screen */ #include<stdio.h> int main() { /* Declaration */ int years; double amount, rate; /* Heading */ printf("Million\n\n"); /* Input Section */ do { printf("Enter Amount "); scanf("%lf", &amount); } while(amount < 1.00 || amount > 250000.0); do { printf("\nEnter Rate (Ex: .03, .04) "); scanf("%lf", &rate); } while(rate < .01 || rate > .29); /*Processing */ years = 0; while(amount < 1000000.0) { ++years; amount = amount + amount * rate; } /* Output Section */ printf("\n\n%d Years required to reach $%.02lf\n", years, amount); return 0; }
I'm getting the errors "local variable 'amount' used without having been initialized" and "local variable 'rate' used without having been initialized", but weren't they both initialized in the declaration?Code:// million.cpp by Aron 11/27/2005 // This program asks the user to input a dollar // amount and an interest rate, and the program // will calculate how long it will take to reach // $1 million. Input comes from the keyboard and // output is sent to the screen #include<stdio.h> #include<iostream.h> int main() { // Declaration int years; double amount, rate; // Heading cout <<"Million\n\n"; // Input Section do { cout <<"Enter Amount "; cin >>"%lf", &amount; } while(amount < 1.00 || amount > 250000.0); do { cout <<"\nEnter Rate (Ex: .03, .04) "; cin >>"%lf", &rate; } while(rate < .01 || rate > .29); // Processing years = 0; while(amount < 1000000.0) { ++years; amount = amount + amount * rate; } // Output Section cout <<"\n\n%d Years required to reach $%.02lf\n", years, amount; return 0; }
Thanks in advance for the help



LinkBack URL
About LinkBacks


