How to Create Bool Function and Check the File for specific characters?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • john dhoe
    New Member
    • Jan 2011
    • 4

    How to Create Bool Function and Check the File for specific characters?

    I got stuck with creating 2 functions:
    - bool oneAt(char email[] ); - to check if each email has ONLY one “@” character.
    - bool oneDot(char* email); - to check if each email has ONLY one “.” (dot).

    Can somebody help?

    Here's my code:

    Code:
     
    
    #include <iostream>
    #include <fstream>
    #include <cstring>
    #include <sstream>
    #include <stdlib.h>
    using namespace std;
    
    bool oneAt(char email[]);
    
    const unsigned MaxRecLen = 80;
    const unsigned MaxFileName = 200;
    
    int main()
    {
    	ifstream inF;
    	ofstream of;
    
    	char infile[ MaxFileName + 1 ],
    	     outfile [ MaxFileName + 1 ];
    	char email [ MaxRecLen ];
    	
    		
    		cout << "Enter File Name: ";
    		cin >> infile;
    		cout << "Output File: ";
    		cin >> outfile;
    
    	inF.open( infile );	
    	of.open( outfile );
    
    	inF.getline (email, MaxRecLen );
    	
    	while (strlen(email)){
    		if(strlen(email) < 0){
    		}
    		of << email << endl;
    		inF.getline(email, MaxRecLen);
    	}
    	inF.close();   
    	of.close();
    	return 0;
    }
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    We won't write the code for us. Show us what you have tried.

    Comment

    • john dhoe
      New Member
      • Jan 2011
      • 4

      #3
      Didn't I write the code above?:-)

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Sure, but the code above isn't the code you're having trouble with.

        Comment

        • john dhoe
          New Member
          • Jan 2011
          • 4

          #5
          Exactly. I can't go further, because I don't know how. How can I write code if I don't know what to write?
          That's why I posted the problem here...

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            The general idea is to use a loop to step through the array and check the ASCII character code while keeping a count of the character you're looking for.

            Comment

            • john dhoe
              New Member
              • Jan 2011
              • 4

              #7
              hi! could you please check my code & tell why it's not working? it doesn't output what it is supposed to... here's my code:

              Code:
              #include <iostream>
              #include <fstream>
              #include <cstring>
              #include <sstream>
              #include<conio.h>
              using namespace std;
              
              
              const unsigned MaxRecLen = 80;
              const unsigned MaxFileName = 200;
              bool oneAt(char email[])
              	{
              	int atFound = 0;
              	
              	if(email[0] == '@')
              		atFound=2;
              
              	for(int i = 0; i < strlen(email); i++)
              	{
                  if(email[i] == '@')
                  atFound++;
              	}
              	if(atFound>1)
              		return false;
              	else
              		return true;			
               }
              void main()
              {
              	ifstream inF;
              	ofstream of;
              
              	char infile[ MaxFileName + 1 ],
              	     outfile [ MaxFileName + 1 ];
              	char email [ MaxRecLen ];	
              		
              		cout << "Enter File Name: ";
              		cin >> infile;	
              		
              	inF.open( infile );	
              	of.open( outfile );
              	
              bool a;
              
              	while (!inF.eof() )    
                   {
                    //   if(oneAt(inF.getline (email, MaxRecLen) ))
              		 cout<<inF.getline (email, MaxRecLen );
              
              		
              		 /*if(oneAt(b))
              			 cout << b << endl;*/
                   }	
              	inF.close();   
              	of.close();
              	
              	getch();
              }

              Comment

              • Rabbit
                Recognized Expert MVP
                • Jan 2007
                • 12517

                #8
                It would help if we knew what it was outputting that it's not supposed to.

                Comment

                Working...