having problems on line 49

Hello I would like to get some feedback on my overall code here. And i am getting stuck on line 49. your feedback will be appreciated.


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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
  #include<iostream>
#include<string>
#include<fstream>

using namespace std;
 // Declare size of periodic table
const int num_element = 103;

//declare struture
struct elemInfo{
	string elemCode;
	string elemName;
	int e_num;
	int eweight;	
};
// array of elements info
elemInfo eArray[num_element];
//declare prototypes
void loadArray(struct elemInfo eArray[], int num_element );
int displayMenu(int);
int code_search(struct elemInfo eArray[], string elem_code);
int weight_search(struct elemInfo eArray[], int elem_weight);

int main()
{
	//declaration
	int menu, weight, opt1 = 0, opt2 = 0;
	string elem_code;
	
	//call function
	loadArray(eArray,  num_element);
	menu = displayMenu(menu);
	if(menu == 1){
		cout << "enter element code" << endl;
		cin >> elem_code;
		opt1 = code_search(eArray, elem_code);
		if (opt1 == -1){
		cout << "invalid input" << endl;	
		}
		else
		cout << eArray[opt1].elemCode << endl;
		cout << eArray[opt1].elemName << endl;
		cout << eArray[opt1].e_num << endl;
		cout << eArray[opt1].eweight << endl;
		
	}
	if (menu == 2){
		cout << "enter element number" << endl;
		cin >> elem_weight;
		opt2 = weight_search(eArray, elem_weight);
		if (opt2 == -1){
		cout <<  "invalid input" << endl;		
		}
		else
		cout << eArray[opt1].elemCode << endl;
		cout << eArray[opt1].elemName << endl;
		cout << eArray[opt1].e_num << endl;
		cout << eArray[opt1].eweight << endl;
	}
	
 return 0; 
}

// stucture function function
void loadArray(struct elemInfo eArray[], int num_element ){
	ifstream openFile;
	openFile.open("periodic.txt");
	int index = 0;
	while(openFile >> eArray[index].elemCode >> eArray[index].elemName >> eArray[index].e_num >> eArray[index].eweight)
	index++;	
}
// menu function
int displayMenu(int){
	int menu = 0;
	cout << "   Table Menu|n "
	<< " element code\n" << "element number\n"
	<< "enter choice";
	cin menu;
	if (menu == 1){
	return menu;	
		
	}
	else
	cout << "invalid input" << endl;
	cin menu;		
}

// search function
int code_search(struct elemInfo eArray[], string elem_code){
	int opt2 = -1, index = 0;
	bool found = false;
	while (index < num_element && !found ){
		if(eArray[index].e_num == elem_code){
			opt1 = index;
			break;
		}
		index++;
	}
	return opt1;
	
}

// search function
int code_search(struct elemInfo eArray[], string elem_weight){
	int opt2 = -1, index = 0;
	bool found = false;
	while (index < num_element && !found ){
		if(eArray[index].e_num == elem_weight){
			found = true;
			opt2 = index;
			break;
		}
		index++;
	}
	return opt2;
	
}




Shouldn't L104 be weight_search( ? Also isn't elem_weight an int - not a string?

The search functions don't need found. Consider something like (not tried):

1
2
3
4
5
6
7
int weight_search(const elemInfo eArray[], int elem_weight) {
	for (int index {}; index < num_element; ++index)
		if (eArray[index].eweight == elem_weight)
                    return index;

	return -1;
}

Last edited on
having problems on line 49

The code as presented has several problems.

1. elem_weight used at line 49 in main is an undeclared variable.

2. Lines 78 & 85 are missing the insertion operator>>

3. opt1 in the code_search function is undeclared.

4. Lines 93 & 108 you are trying to compare for equality an int to a std::string.

Did this even compile successfully?
it did not compile.
HellocMonkey wrote:
it did not compile.

So... to say your code has a single problem at line 49 is rather understanding the multiple problems it does have.

I pointed out the problems my compiler (Visual Studio 2022) reports that when fixed should at the very least let the code be compiled.

Whether the program logic to produce the expected output is accurate or not, I can't say.

You wanted feedback, you've gotten it. Go fix the known problems and let's try again. :)
ok I think I got all the other problem kinks out but I have an error on line 49 still.

49 C:\Users\Administrator\Desktop undefined reference to `weight_Search(elemInfo*, int)'
btw I cant install visualstudio 2022 for some reason.


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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include<iostream>
#include<string>
#include<fstream>

using namespace std;
 // Declare size of periodic table
const int num_element = 103;

//declare struture
struct elemInfo{
	string elemCode;
	string elemName;
	int elnum;
	int eweight;	
};
// array of elements info
elemInfo eArray[num_element];
//declare prototypes
void loadArray(struct elemInfo eArray[], int num_element );
int displayMenu(int);
int code_search(struct elemInfo eArray[], string eCode);
int weight_Search(struct elemInfo eArray[], int elemWeight);

int main(){
	//declaration
	int menu,  elemWeight, opt1 = 0, opt2 = 0;
	string  eCode;
	
	//call function
	loadArray(eArray,  num_element);
	menu = displayMenu(menu);
	if(menu == 1){
		cout << "enter element code" << endl;
		cin >> eCode;
		opt1 = code_search(eArray, eCode);
		if (opt1 == -1){
		cout << "invalid input" << endl;	
		}
		else
		cout << eArray[opt1].elemCode << endl;
		cout << eArray[opt1].elemName << endl;
		cout << eArray[opt1].elnum << endl;
		cout << eArray[opt1].eweight << endl;
		
	}
	if (menu == 2){
		cout << "enter element number" << endl;
		cin >> elemWeight;
		opt2 = weight_Search(eArray, elemWeight);
		if (opt2 == -1){
		cout <<  "invalid input" << endl;		
		}
		else
		cout << eArray[opt2].elemCode << endl;
		cout << eArray[opt2].elemName << endl;
		cout << eArray[opt2].elnum << endl;
		cout << eArray[opt2].eweight << endl;
	
	}
	
// return menu; 
}

// stucture function function
void loadArray(struct elemInfo eArray[], int num_element ){
	ifstream openFile;
	openFile.open("periodic.txt");
	int index = 0;
	while(openFile >> eArray[index].elemCode >> eArray[index].elemName >> eArray[index].elnum >> eArray[index].eweight)
	index++;	
}
// menu function
int displayMenu(int) {
	int menu = 0;
	cout << "   Table Menu\n "
	<< " element code\n" << "element number\n"
	<< "enter choice";
	cin >> menu;
	if (menu == 1){
	return menu;		
	}
	else if (menu == 2){
	return menu;
		
	}
	else
	cout << "invalid input" << endl;
	cin >> menu;
	return menu;	
}

// search function
int code_search(struct elemInfo eArray[], string eCode){
	int opt1 = -1;
	for (int index = 0; index < num_element; index++){
		if (eArray[index].elemCode == eCode){
		opt1 = index;
		break;
		}
	
	}
	return opt1;
	
}

// search function
int weight_Search(struct elemInfo eArray[], string elemWeight){
	int opt2 = -1, index = 0;
	bool found = false;
	while (index < num_element && !found ){
	if(eArray[index].elemName == elemWeight){
		found = true;
		opt2 = index;
		break;
		}
		index++;
	}
	return opt2;
	
}




Last edited on
This compiles OK (VS2022) but not tried as no data provided.

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
103
104
105
106
107
108
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

constexpr size_t num_element {103};

struct elemInfo {
	string elemCode;
	string elemName;
	unsigned e_num {};
	unsigned eweight {};
};

size_t loadArray(elemInfo eArray[]);
unsigned displayMenu();
int code_search(const elemInfo eArray[], const string& elem_code, size_t numElems);
int weight_search(const elemInfo eArray[], unsigned elem_weight, size_t numElems);

int main() {
	elemInfo eArray[num_element] {};

	const auto numElems {loadArray(eArray)};

	if (numElems == 0)
		return (std::cout << "Cannot open file\n"), 1;

	for (unsigned menu {}; (menu = displayMenu()) > 0; ) {
		if (menu == 1) {
			string elem_code;

			cout << "Enter element code: ";
			cin >> elem_code;

			const auto opt1 {code_search(eArray, elem_code, numElems)};

			if (opt1 == -1)
				cout << "Invalid input\n";
			else {
				cout << eArray[opt1].elemCode << '\n';
				cout << eArray[opt1].elemName << '\n';
				cout << eArray[opt1].e_num << '\n';
				cout << eArray[opt1].eweight << '\n';
			}
		} else if (menu == 2) {
			unsigned weight {};

			cout << "Enter element weight: ";
			cin >> weight;

			const auto opt2 {weight_search(eArray, weight, numElems)};

			if (opt2 == -1)
				cout << "Invalid input\n";
			else {
				cout << eArray[opt2].elemCode << '\n';
				cout << eArray[opt2].elemName << '\n';
				cout << eArray[opt2].e_num << '\n';
				cout << eArray[opt2].eweight << '\n';
			}
		} else
			cout << "Unknown option\n";
	}
}

size_t loadArray(elemInfo eArray[]) {
	ifstream openFile("periodic.txt");

	if (!openFile)
		return 0;

	size_t index {};
	for (; index < num_element && (openFile >> eArray[index].elemCode >> eArray[index].elemName >> eArray[index].e_num >> eArray[index].eweight); ++index);
	return index;
}

unsigned displayMenu() {
	unsigned menu {};

	cout << "   Table Menu\n"
		<< "1) element code\n"
		<< "2) element number\n"
		<< "0) exit\n"
		<< "\nEnter choice";

	do {
		cin >> menu;
	} while (menu > 2 && (std::cout << "Invalid option\n"));

	return menu;
}

int code_search(const elemInfo eArray[], const string& elem_code, size_t numElems) {
	for (int index {}; index < numElems; ++index)
		if (eArray[index].elemCode == elem_code)
			return index;

	return -1;
}

int weight_search(const struct elemInfo eArray[], unsigned elem_weight, size_t numElems) {
	for (int index {}; index < numElems; ++index)
		if (eArray[index].eweight == elem_weight)
			return index;

	return -1;
}

HellocMonkey wrote:
I have an error on line 49 still

I don't know if you've figured it out, so if not I'll show what is going wrong.

M'ok, you fixed the issue with elemWeight not being declared. You also fixed the int vs. std::string issue with your weight_Search function declaration on line 22.

What you didn't do is change the function definition on line 107. It is still expecting a std::string to be passed for the 2nd parameter instead of an int.

int weight_Search(struct elemInfo eArray[], string elemWeight)
should be the same as the function declaration
int weight_Search(struct elemInfo eArray[], int elemWeight)

A simple "ooops!" mistake that's all too easy to make.

I cant install visualstudio 2022 for some reason

VS 2022 is for Windows 10 or 11 only, with a 64-bit processor.
https://docs.microsoft.com/en-us/visualstudio/releases/2022/system-requirements

VS 2019 can be installed on a Windows system that has a 32-bit processor, Windows 7 or above
https://docs.microsoft.com/en-us/visualstudio/releases/2019/system-requirements

If you don't know what your processor bit-ness is, bring up the Start menu, and type "system information". Run the System Information app and from the "System Summary" page there is an entry for "System Type." If you see "x86-based PC" or "x64-based PC" and are running Windows 10 or 11 you can try to install Visual Studio. Whether it is 2019 or 2022 depends on your CPU.

On my main development PC, x64 Windows 10 Pro, I have both 2019 & 2022 installed. I normally use 2022, on occasion I use 2019.
Last edited on
Topic archived. No new replies allowed.