does not output what i want. loop might be broken

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
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <fstream>
#include <iomanip> 
#include <string>
using namespace std;
bool validation (char c) 
{
    bool result = false;
    if (c >='A' && c <= 'Z' || c >= 'a' && c <= 'z'|| c >= '0' && c <='9' ||c =='.'||c =='-' || c =='+') result = true;
return result;
}

int main () {
    //bool ValidEmailCharacters;
	string email;
	//string anEmail;
	int s = 0;
	int e = 0;
	
	string fileName; 
	string defaultfile = "fileContainingEmails.txt";
	ifstream fin;
	ofstream fout;
	cout << "Enter your file  (ENTER for Default): " ;
	getline (cin, fileName);
	
	if (fileName.length() < 2) {
	    fileName = defaultfile;
	}

	fin.open (fileName);
    // if (!fin.good()) break;
	
	fout.open ("copyPasteMyEmail.txt");
	
	//string* email[i] = new string [newemail];     //Pulls out email from txt file
	while (true)    //reads until end of file
	{
	if (!fin.good()) break;
	
	fin >> email;
	cout << "Test" << endl;
	for (int i = 0; i > email.length(); i++)
		if (email[i] == '@')
		{
		cout << "Test1" << endl;
			for( s = i; s < email.length(); s--)
			{    
			cout << "Test2" << endl;
				if (s < 0) break;
				if (validation(email[s]) == false) break;
			}
			bool Dot = false;
				for (e = i; e < email.length(); e++)
				{
				cout << "test3" << endl;
					if (e == email.length()) break;
					if (validation(email[e]) == false) break;
					if (email[e] == '.') Dot = true;
				}
				cout << "Test4" << endl;
			string anEmail = email.substr (s, e-s);
			cout << anEmail << endl;	
		}
    }
	//fout << anEmail << endl;
   
	return 0 ;
	
}
		
What is it supposed to output?
closed account (o3hC5Di1)
Hi there,

As IceThatJaw mentioned, it's usually best to give us an indication of what you expect it to output and what it is actually outputting, that makes debugging a lot easier for us.

Now, as for this line:

1
2
 //line 37
while (true)    //reads until end of file 


That's not true - the loop will keep running as long as true == true, meaning forever.
If you want to loop until the end of the file you will need to use some function of the istream that returns a value on which you can base the assumption that there is another line or not.

If you have any further questions, please elaborate a little bit on the problem.

All the best,
NwN
@NwN
I believe the OP is using this line to break out of the infinite loop:
if (!fin.good()) break;

This is bad coding, but it should work. I don't understand why they have a while loop set for infinity and then break out that way. I also believe there is an issue with the validation function:
1
2
3
4
5
6
bool validation (char c) 
{
    bool result = false;
    if (c >='A' && c <= 'Z' || c >= 'a' && c <= 'z'|| c >= '0' && c <='9' ||c =='.'||c =='-' || c =='+') result = true;
return result;
}


A few things, you don't need to create a new variable, especially for bool functions. The next issue is that you're not using any parenthesis in your if statement and I don't believe it's handling it like you believe it should. Currently, every condition after the && must be true, and every condition after || can be true. The same code simplified:
1
2
3
4
5
bool validation (char c) 
{
    if ((c >='A' && c <= 'Z') || (c >= 'a' && c <= 'z')|| (c >= '0' && c <='9') ||c =='.'||c =='-' || c =='+') return true;
    return false;
}


Even better is to split it up:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
bool validation (char c) 
{
    // Capital Letters
    if (c >='A' && c <= 'Z')
        return true;
    // Lowercase Letters
    else if (c >= 'a' && c <= 'z')
        return true;
    // Numbers
    else if (c >= '0' && c <='9')
        return true;
    // Valid Punctuation
    else if ((c =='.') || (c =='-') || (c =='+'))
        return true;
    return false;
}
for (int i = 0; i > email.length(); i++) That will never run. The loops executes while the condition holds.

As for the reading
1
2
	if (!fin.good()) break;
	fin >> email; //¿what if it fails here? 
Prefer while(fin>>email)

1
2
3
4
5
6
7
8
9
10
11
12
bool validation (char c) 
{
//    if ((c >='A' && c <= 'Z') || (c >= 'a' && c <= 'z')|| (c >= '0' && c <='9') ||c =='.'||c =='-' || c =='+') return true;
//    return false;
   return 
      c >='A' and c <= 'Z' //isupper
      or c >= 'a' and c <= 'z' //islower.
      //isalpha
      or c >= '0' and c <='9' //isdigit
      //isalnum
      or c =='.' or c =='-' or c =='+';
}
The parenthesis are not needed because of precedence rules
I completely forgot that && and || have different precedence. I need to keep a copy of the precedence order close by.
Topic archived. No new replies allowed.