How to prompt user to enter File Name and Evaluate it?

Jan 27, 2011 at 9:45pm
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.
Last edited on Jan 27, 2011 at 9:46pm
Jan 28, 2011 at 12:01am
I'm not sure if your criteria for what makes an "invalid" email is, but real emails can have periods in them...
Last edited on Jan 28, 2011 at 12:02am
Jan 28, 2011 at 5:28am
This is what I have so far:



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
#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;
}
Jan 28, 2011 at 5:31am
I just need to check if list of emails has emails with only one "@" character & one "."
Jan 28, 2011 at 4:56pm
1
2
3
4
5
6
7
8
bool oneAt(char email[])
{
int NoOfAts = 0;

//Iterate through the 'email' string using a loop, and increment 'NoOfAts' when you found a '@'
//then return false if( NoOfAts == 0) else return true.

}

you can implement oneDot(char* email) in the same manner.
Last edited on Jan 28, 2011 at 4:58pm
Jan 29, 2011 at 7:27pm
I'm not sure how to implement that...:(
Jan 29, 2011 at 7:34pm
@lisak,

in C++ the correct inclusion of "stdlib" is:

#include<cstdlib>

and:

#include<stdlib.h>

is in C.
Feb 2, 2011 at 6:27am
hi. it doesn't work again. could you please take a look at that & let me know what's wrong?

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


Topic archived. No new replies allowed.