Unknown result .

Hello I'm doing a program that is used to calculate the commission of mlm company,apparently when I testing the function search by id and name , both having the similar function via different approach , when i type in random value it will still run the if-else function and result in some random calculated numbes.

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

using namespace std;

void menu();
void selection();
void data_input();
double total_sales(int);
void id_search();
void name_search();
void member_recruit();
void add_new_agent_sales();
void edit_month_sales();
void edit_member_recruit();
void delete_agent();
void display_agent_and_awards();

const int MAX_SIZE=20;
int CURRENT_SIZE=10;

char tmp[50];
string id[10],name[10];
double t_sales[10],t_comm[10];
double Jan[MAX_SIZE],Feb[MAX_SIZE],Mar[MAX_SIZE],Apr[MAX_SIZE],May[MAX_SIZE],Jun[MAX_SIZE],Jul[MAX_SIZE],Aug[MAX_SIZE],Sep[MAX_SIZE],Oct[MAX_SIZE],Nov[MAX_SIZE],Dec[MAX_SIZE];
int member[MAX_SIZE];

fstream dataFile;


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
void data_input()
{
	int a=0,b=0;
	dataFile.open("agent.dat",ios::in);

	if(dataFile)
	{
		cout<<"File loaded sucessfully!\n";
		system("pause");
		system("cls");
		while(!dataFile.eof())
		{
			getline(dataFile,id[a]);
			getline(dataFile,name[a]);
			dataFile>>Jan[a]>>Feb[a]>>Mar[a]>>Apr[a]>>May[a]>>Jun[a]>>Jul[a]>>Aug[a]>>Sep[a]>>Oct[a]>>Nov[a]>>Dec[a];
			dataFile>>member[a];
			dataFile.getline(tmp,50);/*for unknown reason if this line was not added , the function will enter infinite loop*/
			//cout<<id[a]<<endl; //not yet developed 
			//cout<<name[a]<<endl;
			//cout<<Jan[a]<<endl;
			//cout<<member[a]<<endl;
			a++;
		}
	}
	else 
	{
		cout<<"Error, file failed to open.\n";
		system("pause");
		exit(1);
	}
}


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
void id_search()
{
	string mlm_id;
	int i=0;
	double n;
	cout<<"Please enter your ID: ";
	cin>>mlm_id;
	for(int j=0;j<10;j++)//abc
	{
		if(mlm_id.compare(id[j])==1)
		{
			i=j;
		}
	}
	n=total_sales(i);
	if(n>=10000 && n<=49999)
	{
		cout<<"Award: Silver Award"<<endl;
		cout<<"Commission: "<<n*0.08<<endl;
	}
	else if(n>=50000 && n<=99999)
	{
		cout<<"Award: Gold Award"<<endl;
		cout<<"Commission: "<<n*0.13<<endl;
	}
	else if(n>=100000 && n<=199999)
	{
		cout<<"Award: Platinum Award"<<endl;
		if(member[i]>=100 && member[i]<=199)
		{
			cout<<"Special Award: Century Platinum Award"<<endl;
			cout<<"Commission: "<<(n*0.20)+(member[i]*40)<<endl;
		}
		else if(member[i]>200)
		{
			cout<<"Special Award: Millennium Platinum Award"<<endl;
			cout<<"Commission: "<<(n*0.22)+(member[i]*60)<<endl;
		}
		else cout<<"Commission: "<<n*0.18<<endl;
	}
	else if(n>200000)
	{
		cout<<"Award: Diamond Award"<<endl;
		if(member[i]>=300 && member[i]<=499)
		{
			cout<<"Special Award: Century Diamond Award"<<endl;
			cout<<"Commission: "<<(n*0.30)+(member[i]*70)<<endl;
		}
		else if(member[i]>500)
		{
			cout<<"Special Award: Millennium Diamond Award"<<endl;
			cout<<"Commission: "<<(n*0.32)+(member[i]*80)<<endl;
		}
		else cout<<"Commission: "<<n*0.25<<endl;
	}
}

void name_search()
{
	string mlm_name;
	int i=0;
	double n;
	cout<<"Please enter your name: ";
	cin>>mlm_name;
	for(int j=0;j<10;j++)//abc
	{
		if(mlm_name.compare(name[j])==1)
		{
			i=j;
		}
	}
	n=total_sales(i);
	cout<<n;
	if(n>=10000 && n<=49999)
	{
		cout<<"Award: Silver Award"<<endl;
		cout<<"Commission: "<<n*0.08<<endl;
	}
	else if(n>=50000 && n<=99999)
	{
		cout<<"Award: Gold Award"<<endl;
		cout<<"Commission: "<<n*0.13<<endl;
	}
	else if(n>=100000 && n<=199999)
	{
		cout<<"Award: Platinum Award"<<endl;
		if(member[i]>=100 && member[i]<=199)
		{
			cout<<"Special Award: Century Platinum Award"<<endl;
			cout<<"Commission: "<<(n*0.20)+(member[i]*40)<<endl;
		}
		else if(member[i]>200)
		{
			cout<<"Special Award: Millennium Platinum Award"<<endl;
			cout<<"Commission: "<<(n*0.22)+(member[i]*60)<<endl;
		}
		else cout<<"Commission: "<<n*0.18<<endl;
	}
	else if(n>200000)
	{
		cout<<"Award: Diamond Award"<<endl;
		if(member[i]>=300 && member[i]<=499)
		{
			cout<<"Special Award: Century Diamond Award"<<endl;
			cout<<"Commission: "<<(n*0.30)+(member[i]*70)<<endl;
		}
		else if(member[i]>500)
		{
			cout<<"Special Award: Millennium Diamond Award"<<endl;
			cout<<"Commission: "<<(n*0.32)+(member[i]*80)<<endl;
		}
		else cout<<"Commission: "<<n*0.25<<endl;
	}
}


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
void name_search()
{
	string mlm_name;
	int i=0;
	double n;
	cout<<"Please enter your name: ";
	cin>>mlm_name;
	for(int j=0;j<10;j++)//abc
	{
		if(mlm_name.compare(name[j])==1)
		{
			i=j;
		}
	}
	n=total_sales(i);
	cout<<n;
	if(n>=10000 && n<=49999)
	{
		cout<<"Award: Silver Award"<<endl;
		cout<<"Commission: "<<n*0.08<<endl;
	}
	else if(n>=50000 && n<=99999)
	{
		cout<<"Award: Gold Award"<<endl;
		cout<<"Commission: "<<n*0.13<<endl;
	}
	else if(n>=100000 && n<=199999)
	{
		cout<<"Award: Platinum Award"<<endl;
		if(member[i]>=100 && member[i]<=199)
		{
			cout<<"Special Award: Century Platinum Award"<<endl;
			cout<<"Commission: "<<(n*0.20)+(member[i]*40)<<endl;
		}
		else if(member[i]>200)
		{
			cout<<"Special Award: Millennium Platinum Award"<<endl;
			cout<<"Commission: "<<(n*0.22)+(member[i]*60)<<endl;
		}
		else cout<<"Commission: "<<n*0.18<<endl;
	}
	else if(n>200000)
	{
		cout<<"Award: Diamond Award"<<endl;
		if(member[i]>=300 && member[i]<=499)
		{
			cout<<"Special Award: Century Diamond Award"<<endl;
			cout<<"Commission: "<<(n*0.30)+(member[i]*70)<<endl;
		}
		else if(member[i]>500)
		{
			cout<<"Special Award: Millennium Diamond Award"<<endl;
			cout<<"Commission: "<<(n*0.32)+(member[i]*80)<<endl;
		}
		else cout<<"Commission: "<<n*0.25<<endl;
	}
}


Any hint will help , thank you !
Last edited on
when i type in random value it will still run the if-else function and result in some random calculated numbes
where exactly is the problem?


What I see is that you use the string compare() function wrongfully. Look at this
http://www.cplusplus.com/reference/string/string/compare/

You shouldn't compare with 1. You can always write if(mlm_name == name[j])


Better turn this while(!dataFile.eof()) into while(dataFile.good())

Be aware that each line that you read in could possibly lead to a problem. But never read a line that doesn't exists
Topic archived. No new replies allowed.