Will it do the job?

HI.
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#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;
   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;
      <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();

   out.close();

   return 0;

}
Last edited on
What do you say will it do the job or not ?

You are the best judge for that, it's your code, your assignment, just compile and run it.

p/s why does your code look so messy? or is it only me/my browser?
It seems like he somehow managed to break the syntax highlighting function.
closed account (z05DSL3A)
What do you say will it do the job or not ? ... The file names are specified on the command line:
square data.txt squares.txt


Not

See: Accepting command line arguments:
http://www.cprogramming.com/tutorial/print/lesson14.html
Last edited on
The code seems to have picked up some HTML syntax highlighting from his IDE/browser or whatever it was copied from.
Try this: http://www.cplusplus.com/reference/clibrary/cctype/isalpha/

Then if false cast "ch" to an integer through the stringstream and do the math. else do whatever. File Data is generally easier to work with if you read it in as a string.
Topic archived. No new replies allowed.