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.

Dani AI

Generated

Common problems in the posted code are the cause of the unexpected behavior: using C-style buffers and strlen to drive the loop, testing .eof(), calling void main(), and opening an output file whose name was never read (uninitialized). ’s counting idea and ’s advice to use getline are the right direction; a concise, safer approach is to read each line into a std::string, count the characters with std::count, and write invalid emails to an output stream only after checking that both counts equal 1.

#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>

bool oneAt(const std::string& s) { return std::count(s.begin(), s.end(), '@') == 1; }
bool oneDot(const std::string& s) { return std::count(s.begin(), s.end(), '.') == 1; }

int main() {
    std::string infn, outfn, line;
    std::getline(std::cin, infn); // prompt first, then read; same for outfn
    std::getline(std::cin, outfn);
    std::ifstream in(infn); if(!in) return 1;
    std::ofstream out(outfn); if(!out) return 1;
    while (std::getline(in, line)) {
        if (line.empty()) continue;
        if (!oneAt(line) || !oneDot(line)) out << line << '\n';
    }
    return 0;
}

Notes and troubleshooting: trim leading/trailing whitespace before checks if the file lines may include spaces. Verify the output filename is actually read before opening (the second code sample posted had outfile uninitialized). Don’t use .eof() to control reading; the while (std::getline(...)) pattern is robust. For more realistic validation (dots allowed in local part, multiple subdomains, proper positions of @ and .) consider a regex-based validator instead of the simple “exactly one” rule — the class/assignment may require the simpler rule, so confirm that requirement before making it stricter.

This integrates ’s simple character-count idea and ’s getline loop while fixing the common bugs seen in ’s posted attempts.

Recommended Answers

All 7 Replies

This version of getline should probably be your tool of choice

Beyond that, which parts are stumping you? Can you open a file? Can you search through the characters of a string?

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.