I need to write a program 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. Like this : 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

c++ on unix if this is important

Almost nothing and for this I doesn't show here to mistake you or you to abide in my code

Without something to go on I don't know where your difficulties are. Can you write a program to read a data file? Can you write a program that writes a data file? Can you write a program that squares numbers? Start on one or more of those pieces and post back some actual code.

Also, it is disrespectful to tell us that something is urgent. Your rush does not become our rush.

I need to write a program 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. Like this : 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

c++ on unix if this is important

#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;

/*
 * NOTE:  This is simplified example code for an approach to solving your 
 * problem. To make it "production" quality, exception handling and type/format
 * safety can be added
 */

int main(int argc, char* argv[])
{
    if (3 != argc) {
        cout << "Error! you need tp provide the input file name and output filenamen"
             << "e.g. " << argv[0] << " input output " << endl;
        exit(EXIT_FAILURE); // exit() and EXIT_FAILURE is part of <cstdlib>
     }

    ifstream in(argv[1]);
    ofstream out(argv[2]);

    float value = 0.0, square = 0.0;

    while (in >> value ) 
    {
        square = value * value;
        out << square << endl;
    }

    in.close();
    out.close();

    return 0;
}

Edited 3 Years Ago by Reverend Jim: Fixed formatting

london_coder you should have let Theanonymous to attempt writing a program by himself

In the future please try to refrain from giving away a code sample that an OP can turn around and hand in for a grade (as I'm doubtful that not having exceptions and type safety will hinder that). Guide the poster through the process and help him/her reach the conclusions that they need to.

this is the code which I write, this doesn't fulfill the condition completely but I think that is not a problem

#include <iostream>

#include <fstream>

#include <string>



using namespace std;



int main()

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

double sqr=0;

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

   in.close();

   out.close();

   return 0;

}

Edited 6 Years Ago by Theanonymous: n/a

Okay. Good effort in posting something (and not copying the other post). For next time if you use [ ] in the code tags like

[code] [/code] (there's still time to edit your post with the edit button in the lower left corner).

Now the question is does it work as is at this point? (does it read in the numbers correctly and output the right answers)

yes it work very fine print every number on new line in the new file and detect every number in the input file

Do you know what argc and argv are? Have you written anything where you've had them as parameters for main?

Do you know what argc and argv are? Have you written anything where you've had them as parameters for main?

No

Give this a read: http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1046139290&id=1043284392 and using your square data.txt squares.txt example from above either write a separate small program (that you can probably cut and paste into your larger one anyway) or make it so your program prints out the filenames "data.txt" and "squares.txt" or whatever you pass into it on the command line. Once that's done (with a few adjustments) you should be all set with your program.

An excellent mini-tutorial by Narue on which the "ink" is barely dry:http://www.daniweb.com/forums/post1175948.html#post1175948 (it's in the C forum but I don't know of any nuances in using them in C versus C++)

Edited 6 Years Ago by jonsca: no pun intended

this is the code which I write, this doesn't fulfill the condition completely but I think that is not a problem

#include <iostream>

#include <fstream>

#include <string>



using namespace std;



int main()

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

double sqr=0;

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

   in.close();

   out.close();

   return 0;

}

i have the same task maybe we are from the same school :D:D
is this really gonna do the job

This question has already been answered. Start a new discussion instead.