I made a program for my homework it doesnt fulfill the task completely
but i think it's gonna do the job.What do you say will it do the job or not ????

Heres the task:Write a program square that calculates the squares of all numbers that are contained in a file of floating point numbers. The obtained new numbers form a new file. For example if the file contains the numbers 1, 2.5, 0.1, the numbers 1, 6.25 and 0.01 form the resulting file. The file names are specified on the command line:

square data.txt squares.txt

And heres what i made:

#include <iostream>

#include <fstream>

#include <string>

using namespace std;

int main()

cout << "Enter <strong class="highlight">the</strong> input <strong class="highlight">file</strong> name: ";
   string finame;
   cin >> finame;
   ifstream <strong class="highlight">in</strong>;
   <strong class="highlight">in</strong>.open(finame.c_str());
   if (<strong class="highlight">in</strong>.fail())
   {  cout << "Error opening " << finame << endl;
      return 1;            
   cout << "Enter <strong class="highlight">the</strong> output <strong class="highlight">file</strong> name: ";
   string foname;
   cin >> foname;
   ofstream out;
   if (out.fail())
   {  cout << "Error opening " << foname << endl;;
      return 1;            

double sqr=0;

double sqrt;
   while (!in.eof())
   {  char ch;
      <strong class="highlight">in</strong>.get(ch);
      if ('0' <= ch && ch <= '9') /* it was <strong class="highlight"><vb_highlight>a</strong></vb_highlight> digit */
      {  <strong class="highlight">in</strong>.unget();
         double n;
         <strong class="highlight">in</strong> >> n; /* read integer starting with ch */
         sqr = n*n;
     out << sqr << endl;
      else cout << ch; 

   <strong class="highlight">in</strong>.close();


   return 0;


>> The file names are specified on the command line:

That means you need to declare main() with two arguments -- argc and argv. argv[1] is the input file name and argv[2] is the output file name. Your program should not prompt for these names.

>>cout << "Enter <strong class="highlight">the</strong> input <strong class="highlight">file</strong> name:

Huh??? what is "<strong class= ... " business? You are supposed to be writing c++ code, not html.

This article has been dead for over six months. Start a new discussion instead.