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

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;

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...