buckwheat88 Posted November 27, 2005 Report Share Posted November 27, 2005 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:/* 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;}and this is what I have so far in my converted program:// 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;}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?Thanks in advance for the help Quote Link to post Share on other sites
Hai-Etlik Posted November 27, 2005 Report Share Posted November 27, 2005 Well, the lazy way to go would be to change#include <stdio.h>to #include <cstdio>using namespace std;And otherwise leave the original alone. Your teacher probably doesn't want that though.The read and write operations on iostreams do NOT take formatting strings like printf and scanf. And you don't pass a pointer to the read method.int foo;scanf("%i", &foo);printf("The integer variable foo has a value of %i.\n", foo);is equivalent toint foo;cin >> foo;cout << "The integer variable foo has a value of " << foo << "." << endl;And the proper C++ include for iostream is#include <iostream>use namespace std; Quote Link to post Share on other sites
buckwheat88 Posted November 28, 2005 Author Report Share Posted November 28, 2005 Great! Didn't think that would help, because it confused me at first. Started thinking about it more, before asking and looking like a fool. Figured it out and got it to work for all my programs. Thanks for the help Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.