The search function only work for the first code and anycode after the first is deemed as the item not found

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
100
101
102
#include<iostream>
#include<fstream>
#include<cctype>
#include<iomanip>
#include<cstring>
#include<cstdlib>

using namespace std;

typedef struct
{
	char code[5];
	char name[30];
	int quantity;
}MATERIAL;

int readMat(MATERIAL m[], int size);
void listMat(MATERIAL m[], int size);
void searchMat(MATERIAL m[], int size);

int main(void)
{

	MATERIAL mat[50]; //store up to 50 materials
	int MaterialNum = readMat(mat, 50);
	listMat(mat, MaterialNum);
	searchMat(mat, MaterialNum);
	
	return 0;
}

int readMat(MATERIAL m[], int size)
{
	ifstream inFile;
	int numMat = 0;;
	inFile.open("materials.txt");
	if (!inFile)
	{
		cout << "Error opening file !\nCheck the name of the file !\n";
		exit(1);
	}
	else
	{
		for (int i = 0; !inFile.eof(); i++)
		{
			fflush(stdin);
			inFile.getline(m[i].code, 5, ',');
			inFile.getline(m[i].name, 30, ',');
			inFile >> m[i].quantity;
			numMat++;
		}
		inFile.close();
	}
	return numMat;
}

void listMat(MATERIAL m[], int numMat)
{
	int i;
	cout << fixed << left;
	cout << setw(10) << "Code";
	cout << setw(50) << "Type";
	cout << setw(10) << "Quantity";
	cout << endl;
	for (i = 0; i < numMat; i++)
	{
		cout << setw(10) << m[i].code;
		cout << setw(50) << m[i].name;
		cout << setw(10) << m[i].quantity;
		cout << endl;
	}
}

void searchMat(MATERIAL m[], int numMat)
{
	char searchCode[5];
	int found = 0;
	cout << "\nEnter the material code number:";
	cin >> searchCode;
	for (int i = 0; i < numMat; i++)
	{
		if (_strcmpi(m[i].code, searchCode) == 0)
		{
			system("cls");
			cout << fixed << left;
			cout << setw(10) << "Code";
			cout << setw(50) << "Type";
			cout << setw(10) << "Quantity";
			cout << endl;
			cout << setw(10) << m[i].code;
			cout << setw(50) << m[i].name;
			cout << setw(10) << m[i].quantity;
			cout << endl;
			found++;
		}
	}
	if (found == 0)
	{
		system("CLS");
		cout << "The material ID does not exist. \n";
	}
 }


Last edited on
This is my text file contents
1
2
3
4
5
6
7
8
9
201,hammers, 50
202,flat-head screwdrivers, 50
203,philips screwdrivers, 50
204,tape measures, 20
205,flat-head screws, 200
206,philips screws, 200
207,cordless drill,	30
208,spanner, 50
209,utility knife, 10
line 44 for (int i = 0; !inFile.eof(); i++)
don't rely on eof(). It doesn't work as you expect:
https://softwareengineering.stackexchange.com/questions/318081/why-does-ifstream-eof-not-return-true-after-reading-the-last-line-of-a-file

line 46 fflush(stdin);
Don't do this. https://stackoverflow.com/questions/2979209/using-fflushstdin

Wht this c-style code in a C++ app ?
Better use std::string and std::vector
For using the using namespace std it was compulsory for us to write our code in this style by our professor in class, i had read that it was a bad habit for doing that but i have no choice but to follow with it.

For line 44 however i just gone through the article i would change my code for it. However it doesnt help with my problem as all the data is still stored in the variable currently but i still couldnt display them through the search function. Was there something wrong with the logic of it ?
Was there something wrong with the logic of it ?

The problem is line 49 inFile >> m[i].quantity; The >> leaves \n in the buffer so next time the code starts with \n and of course "\n202" != "202";

Adding inFile.ignore(255, '\n'); after line 49 should solve the problem.
I also would leave the for loop with a break after you found it. There is only 1 item to be found.
If you have to use c-style null terminated strings, then consider:

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 <cctype>
#include <iomanip>
#include <cstring>

using namespace std;

struct MATERIAL {
	char code[5] {};
	char name[30] {};
	int quantity {};
};

size_t readMat(MATERIAL m[], size_t size);
void listMat(const MATERIAL m[], size_t size);
void searchMat(const MATERIAL m[], size_t size);

int main(void)
{
	constexpr size_t MaxMat {50};
	MATERIAL mat[MaxMat] {};
	const size_t MaterialNum {readMat(mat, MaxMat)};

	listMat(mat, MaterialNum);
	searchMat(mat, MaterialNum);
}

size_t readMat(MATERIAL m[], size_t size)
{
	size_t numMat {};
	ifstream inFile("materials.txt");

	if (!inFile) {
		cout << "Error opening file !\nCheck the name of the file !\n";
		exit(1);
	}

	for (; numMat < size && inFile.getline(m[numMat].code, 5, ',') && inFile.getline(m[numMat].name, 30, ',') && inFile >> m[numMat].quantity && inFile >> ws; ++numMat);
	return numMat;
}

void listMat(const MATERIAL m[], size_t numMat)
{
	cout << fixed << left << setw(10) << "Code" << setw(50) << "Type" << "Quantity\n";

	for (size_t i = 0; i < numMat; ++i)
		cout << left << setw(10) << m[i].code << setw(50) << m[i].name << setw(10) << m[i].quantity << '\n';
}

void searchMat(const MATERIAL m[], size_t numMat)
{
	char searchCode[5] {};
	size_t found {};

	cout << "\nEnter the material code number:";
	cin >> setw(5) >> searchCode;

	for (size_t i = 0; i < numMat; ++i)
		if (_strcmpi(m[i].code, searchCode) == 0) {
			//system("cls");
			cout << fixed << left << setw(10) << "Code" << setw(50) << "Type" << "Quantity\n";
			cout << setw(10) << m[i].code << setw(50) << m[i].name << setw(10) << m[i].quantity << '\n';
			++found;
		}

	if (found == 0) {
		//system("CLS");
		cout << "The material ID does not exist. \n";
	}
}

Last edited on
Thank you all for the help the problem have been fixed. Have a nice day !
Topic archived. No new replies allowed.