Console freezes when complied.

Hi, I was told to write this code which will read text from a file, which is laid out sort of like this:

Id: 4343
Name: Daniel
Number: 88442939
Email: daniel@yahoo.com
Id: 4829
Name: Steel
Number: 2348795
Email: Steel@gmail.com

The Problem is that whenever I execute the code the console simply ... freezes. I checked and it looks like the condition for input is causing this, but it's not that it's not working at all - after forcefully closing the compiler, you can see that two of the text files 'names' and 'numbers' is being formed, but with weird values. Heres the 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <iostream>
#include <fstream>
using namespace std;

void Displayer(int, int, char[], char[]);
void IdDisplay(int);
void NameDisplay(char[]);
void NumberDisplay(int);
void EmailDisplay(char[]);

int main()
{
	int Id = {}, Number = {};
	char Name[20], Email[30];

	Displayer(Id, Number, Name, Email);

	return 0;
}

void Displayer(int I, int N, char A[], char E[])
{
	ifstream Main;
	Main.open("Users.txt");
	char Word[20];
	while (!Main.eof())
	{
		Main >> Word;
		if (Word[0] == 'i')
		{
			Main >> I;
			IdDisplay(I);
			for (int i = 0; i < 20; i++)
			{
				Word[i] = {};
			}
		}
		else if (Word[0] == 'n')
		{
			if (Word[1] == 'a')
			{
				Main.getline(A, 20, '\n');
				NameDisplay(A);
				for (int i = 0; i < 20; i++)
				{
					Word[i] = {};
				}
			}
			else if (Word[1] == 'u')
			{
				Main >> N;
				NumberDisplay(N);
				for (int i = 0; i < 20; i++)
				{
					Word[i] = {};
				}
			}
		}
		else if (Word[0] == 'e')
		{
			Main.getline(E, 30, '\n');
			EmailDisplay(E);
			for (int i = 0; i < 20; i++)
			{
				Word[i] = {};
			}
		}
	}

}

void IdDisplay(int I)
{
	ofstream f1;
	f1.open("Ids.txt");
	f1 << I << endl;
}

void NameDisplay(char A[])
{
	ofstream f2;
	f2.open("Names.txt");
	f2 << A << endl;
}

void NumberDisplay(int N)
{
	ofstream f3;
	f3.open("Numbers.txt");
	f3 << N << endl;
}

void EmailDisplay(char E[])
{
	ofstream f4;
	f4.open("Emails.txt");
	f4 << E << endl;
}
Last edited on
The first thing to note is your input keywords are all capitalised, but you're only testing lowercase.

All your output files delete any saved content - do you want to open them in append mode?

Changing all your identifiers to single letters makes your code much harder to read.

What compiler / IDE do you use?
When I run it in VS2019 I see an empty console window with a blinking cursor, but it's not frozen. What do you expect to see. You don't write anything to the console.

When working with files, the first thing to do when you try to open or create a file is to check if the file can be used.
Insert this this code snippet after line 24
1
2
3
4
5
6
7
if(Main)
  std::cout << "Users.txt opened.\n";
else
{
  std::cout << "Failed to open Users.txt\n";
  return;
}
1
2
3
4
5
6
7
8
9
if( std::ifstream file{ "users.txt" } ) // if the file is opened for input
{
    const int WORD_SZ = 20 ;
    char word[WORD_SZ] {} ;
    while( file >> std::setw(WORD_SZ) >> word ) // for each word read (max WORD_SZ-1 characters) from the file
    {
        // do whatever with the word that was read
    }
}
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
#include <fstream>

using namespace std;

void Displayer(int, int, char[], char[]);
void IdDisplay(int);
void NameDisplay(char[]);
void NumberDisplay(int);
void EmailDisplay(char[]);

int main()
{
    int Id{0}, Number{0};
    char Name[20], Email[30];
    
    Displayer(Id, Number, Name, Email);
    
    return 0;
}

void Displayer(int I, int N, char A[], char E[])
{
    ifstream Main;
    Main.open("Users.txt");
    
    if(!Main.is_open())
    {
        cout << "No such file\n";
        exit(1);
    }
    
    char Word1[20], Word2[20];
    while (Main >> Word1 >> Word2)
    {
        cout << "Word1 = " << Word1 << " Word2 = " << Word2 << '\n';
        
        if (Word1[0] == 'I')
        {
            IdDisplay(stoi(Word2));
        }
        else if (Word1[1] == 'a')
        {
            NameDisplay(Word2);
        }
        else if (Word1[1] == 'u')
        {
            NumberDisplay(stoi(Word2));
        }
        else if (Word1[1] == 'm')
        {
            EmailDisplay(Word2);
        }
        else
        {
            cout << "Word1 has no use\n";
        }
    }
}

void IdDisplay(int I)
{
    std::ofstream outfile;

    ofstream f1;
    f1.open("Ids.txt", std::ios_base::app );
    f1 << I << endl;
}

void NameDisplay(char A[])
{
    ofstream f2;
    f2.open("Names.txt", std::ios_base::app );
    f2 << A << endl;
}

void NumberDisplay(int N)
{
    ofstream f3;
    f3.open("Numbers.txt", std::ios_base::app );
    f3 << N << endl;
}

void EmailDisplay(char E[])
{
    ofstream f4;
    f4.open("Emails.txt", std::ios_base::app );
    f4 << E << endl;
}


BTW Your function parameters need to be looked at very carefully. It's luck alone that most aren't required, hence the WORD_SZ advice above :)
Last edited on
Also note that ofstream will remove any existing file content when the file is opened. See againtry's open statements above.
Topic archived. No new replies allowed.