Hello everybody! I’m new to C++. Need some help. I am trying to prompt the user to enter the file name. File itself includes list of 5 emails.
After user enters the file name, program should check the list of emails and output all emails that have wrong email format (each email should have only one “@” character and one dot).
So I need to create 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).
It should be really easy, but I just can’t implement it. I hope somebody can help me.

This is what I got so far:

#include <iostream>
#include <fstream>
#include <cstring>
#include <sstream>
#include <stdlib.h>
using namespace std;

const unsigned MaxRecLen = 80;
const unsigned MaxFileName = 200;

int main()
{
	ifstream inF;
	ofstream of;

	char infile[ MaxFileName + 1 ],
	     outfile [ MaxFileName + 1 ];
	char email [ MaxRecLen ];
	string line;
		
		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;
}

I'm not exactly sure how to check if the list of emails has all emails with only one "@" character... Any ideas?

Look at each character. Increment a value when you see @. When done, if the value is 1 you're goo, otherwise bad.

Rather than use strlen to drive your loop, use the getline function directly:

while(inf.getline(email,maxRecLen))
{
  //etc.
}

This way you won't need that initial read to get a value for strlen. When I tried it your way, I was getting an extra string. That if statement isn't doing anything either, so you can get rid of that.

I'm not exactly sure how to check if the list of emails has all emails with only one "@" character... Any ideas?

A string is essentially an array of characters with a null on the end. How would you search an array if you know the endpoint?

Hi. It doesn't work again. Could you take a look at that & tell what's wrong?

#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();
}

What is it doing/not doing and what do you expect it to do? In the future, it helps to know what area is going awry. It helps us get to the problem much faster.

void main()

Ugh, you had it correct before, what happened?

Remember that atFound > 1 is already true if atFound > 1 and false if it isn't, there's no need to test that. You can either return !(atFound > 1); or return atFound == 1 to get the result that you want. Yours is not wrong at all, but you're retesting something that doesn't need to be.

Looking at line 47, a getline command returns a reference to an istream object, not a string (don't worry about what the first part means, but just test the string directly with oneAt() after you've read it in).

(as an aside, give http://www.daniweb.com/forums/post155265-18.html a read about why .eof() isn't a good idea)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.