Help with beginning programmer

Hi,
I'm trying to write a simple program, but I keep getting errors.
Is there anything glaringly wrong in the following code? Thanks!

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
// INSTALLATION:  MTSU                                         
// REMARKS:  The program's goal is to pick out and print all of the valid email
// addresses in the information given and nothing else.
// 
// 
#include <iostream>
#include <fstream>
#include <ostream>
#include <string>
using namespace std;

int main()
{ 
   
                             
   string word;                    
   int i=0;
   char ch['@'];
   size_t found;
   
   ifstream fileIn;
   ofstream fileOut;
   fileIn.open("mail.dat");
   fileOut.open("addresses.dat");
   if (!fileIn)
   {
      cout << "Can't open the input file." << endl; 
      return 1;
   }
   while (! fileIn.eof())
   {
      fileIn >> word;
      found=word.find_first_of(ch);
      if (found!=string::npos);
      {
         fileOut << word;
         cout << word << endl;
         i++;
      }
   }
   // close files
   fileIn.close();
   fileOut.close();
   return 0;
}
char ch['@'];
This defines a char array with (what I assume is) the size of the ascii value for @. Just define it like you do the int above.
@Warnis that's actually pretty funny... that's probably exactly what that evaluates to (unless the compiler just disallows it)
Warnis is right, and no compiler can disallow it.
You should do something like
char ch = '@';
or
char ch[] = "@";
I personally do not know which one is the right one, i don't use iostream that much, eventually try them both.
Last edited on
closed account (zb0S216C)
+1 Warnis

@Perimate: Did you mean to use functional notation? char ch( '@' )?

Wazzak
closed account (zvRX92yv)
File streams aren't booleans! WHY DOES EVERYONE KEEP DOING THAT!?!

Use leStream.good().

And instead of endl, please use '\n', endl does a bunch of extra stuff like flushing the buffer..
Topic archived. No new replies allowed.