Thread: Converting C to C++

  1. #1
    Pumkin King
    Join Date
    Sep 2005
    Location
    Michigan, USA
    Posts
    12

    Converting C to C++

    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:
    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;
    }
    and this is what I have so far in my converted program:
    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;
    }
    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

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    No they weren't initialized they contrain random values, set them to 0 to avoid getting this message.
    also use these includes
    Code:
    #include <iostream>
    #include <fstream>
    and use this
    Code:
    using namespace std;

  3. #3
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    cin >> syntax is somewhat different than scanf. Readings:

    Basic C++ I/O http://www.cplusplus.com/doc/tutorial/basic_io.html
    Reference istream::operator>> http://www.cplusplus.com/ref/iostrea...ratorgtgt.html
    C++ I/O FAQ http://www.parashift.com/c++-faq-lite/input-output.html

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    85
    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<iostream>
    using namespace std;
    int main()
    {
    	// Declaration
    	int years;
    	double amount, rate;
    	// Heading
    	cout <<"Million\n\n";
    	// Input Section
    	do {
    		cout <<"Enter Amount ";
    		cin >> amount;
    	} while(amount < 1.00 || amount > 250000.0);
    	do {
    		cout <<"\nEnter Rate (Ex: .03, .04) ";
    		cin >> rate;
    	} while(rate < .01 || rate > .29);
    	// Processing
    	years = 0;
    	while(amount < 1000000.0) {
    		++years;
    		amount = amount + amount * rate;
    	}
    	// Output Section
    	cout <<"\n\n" << years << " Years required to reach $" << amount << endl;
    	return 0;
    }
    I prefer c++ inputs using cin however scanf is easier to get required data from input line

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting 32 bit binary IP to decimal IP (vice-versa)
    By Mankthetank19 in forum C Programming
    Replies: 15
    Last Post: 12-28-2009, 07:17 PM
  2. Tips for converting C prog. to C++?
    By eccles in forum C++ Programming
    Replies: 7
    Last Post: 01-15-2005, 07:38 AM
  3. Converting Sign Magnitude Integer Binary Files
    By Arthur Dent in forum C Programming
    Replies: 7
    Last Post: 09-13-2004, 10:07 AM
  4. Converting from Screen to World Coordinates
    By DavidP in forum Game Programming
    Replies: 9
    Last Post: 05-11-2004, 12:51 PM
  5. converting string to float
    By twhubs in forum C Programming
    Replies: 1
    Last Post: 09-16-2001, 09:02 AM