Sponsored By

Sign in to follow this  
buckwheat88

Converting C To C++

Recommended Posts

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

Share this post


Link to post
Share on other sites

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 to

int 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;

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this