Why Does This Compile But Have No Output?

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
#include <iostream>
#include <fstream>
#include <string>

    using namespace std;

    int main()
   {
bool Found;
 string str;
ifstream file ("example.dat",ios::in | ios::binary);

 string str2;
ifstream file2 ("example2.dat",ios::in | ios::binary);


while(!file.eof() || Found) // The "Is found true?" check is placed at both while statements to ensure that they both end as soon as a match is found.
{

	getline(file, str, ' '); //Get the first string inside example.dat and store it.
	while (!file2.eof() || Found) //This is one way to end our loop early, when a match is found it stops the loop
	{
		getline(file2, str2, ' '); //Get the first string inside example2.dat and store it
		if (str == str2)
		{
			Found = true; //This is our bool to check if a match was found.
		}
	   str2.clear(); // erase str2 so we can check the next string in example2.dat
	}
	str.clear(); // erase str so we can check the next string in example.dat
}

if (Found)
{ 
     cout<<"This order is valid."<<endl;
}
else 
{
    cout<<"This order is invalid."<<endl;
}


system("pause");
  return 0;

}


This program compares two files. Example.dat is compared to Example2.dat and if any of the values in Example.dat do not match the values in Example2.dat, the output on the screen will read "This order is invalid." If everything matches up, the output will read "This order is valid." Recently, someone has been very helpful in helping me tweak my program and I believe I have my code almost all correct, but after I compile it, I run it and there is no output. Just a blank .exe box. Anyone know what the problem could be?
Last edited on
There are few problem with the code above
while condition should be like this
while(!file.eof() && Found)

In place of OR condition you need to put "and" as if OR it will go into infinite loop once the strings are same.

Also as good programming practice you must initialize the objects before using.
bool Found = false;


hope this will work.
Thanks for your suggestion. I revitalized the code, but unfortunately, it still does not return the correct value. Revised code:
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
#include <iostream>
#include <fstream>
#include <string>

    using namespace std;

    int main()
   {
bool Found=true;
 string str;
ifstream file ("example.dat",ios::in | ios::binary);

 string str2;
ifstream file2 ("example2.dat",ios::in | ios::binary);



while(!file.eof() && Found) // The "Is found true?" check is placed at both while statements to ensure that they both end as soon as a match is found.
{
	// From your post I assume the entries in the files are separated by spaces.
	getline(file, str, ' '); //Get the first string inside example.dat and store it.
	while (!file2.eof() && Found) //This is one way to end our loop early, when a match is found it stops the loop
	{
		getline(file2, str2, ' '); //Get the first string inside example2.dat and store it
		if (str == str2)
		{
			Found = true; //This is our bool to check if a match was found.
		}
	   str2.clear(); // erase str2 so we can check the next string in example2.dat
	}
	str.clear(); // erase str so we can check the next string in example.dat
}

if (Found)
{ 
     cout<<"This order is valid."<<endl;
}
else 
{
    cout<<"This order is invalid."<<endl;
}


system("pause");
  return 0;

}



Example.dat:
ABCD4 _BCD5 _BC13 QQQE3 QQQ3R QQQQD AAAAC AAAAB AAAAD CCCC5 CCC14 BBBBE BBBBG

Example2.dat:
AAAAC AAAAB AAAAD CCCC5 CCC14 BBBBE BBBBG ABCD4 _BCD5 _BC13 QQQE3 QQQ3R QQQQD ERFG8 ZZZZ4 ZE_B12 PPP14 REWD2

As I said earlier, I'm comparing these two files. Example1 can change to whatever values a user saves, but Example2 stays the same. The goal of the program is to determine whether or not the file as a whole is valid (i.e. whatever values saved in Example.dat match those in Example2.dat)

Last edited on
As per my understanding Example2.dat file contains all the template data and you need to compare the each and every data of Example1 file into Example2.dat file.

for this you require two counters one to get the number of values in Example1.dat file and save it
and another one will keep on increanig as you find each string matcihng.
if (str == str2)
{
Found = true; //This is our bool to check if a match was found.
statusflag++ // like this
}

compare the count of both the counters it must match for exact matching of files.

hope it works for you
Thanks for the response. I guess I'm somewhat confused on this process. After you use the second counter which counts as you find each string matching, you compare that to the number of values in the first file? And if they equal each other, this would thus mean that your files equal each other and would print out on screen "This order is valid"?

If that's how you go about using a counter for each matching string, how would you use one to get the number of values in Example1.dat?

I already tried getline(file, str, ' '); statusflag++ but that didn't work at all. and then at the end of the file,


if (statusflag == statusflag2)
{
cout<<"This order is valid."<<endl;
}
else
{
cout<<"This order is invalid."<<endl;
}

. But it doesn't seem to work for me.
Last edited on
Topic archived. No new replies allowed.