How to find the highest value in a vector?

Feb 8, 2015 at 11:14pm
I can find the lowest value, but not the highest value. I don't understand what I am doing wrong. Any help will be appreciated. Here is my code:

 
removed




Here is my output:


contents of vector:
7 1 13 50 37 6
The stats.txt file has been generated.
The highest and the smallest salaries are: 7 and 7
The stats.txt file has been generated.
The highest and the smallest salaries are: 7 and 1
The stats.txt file has been generated.
The highest and the smallest salaries are: 7 and 1
The stats.txt file has been generated.
The highest and the smallest salaries are: 7 and 1
The stats.txt file has been generated.
The highest and the smallest salaries are: 7 and 1
The stats.txt file has been generated.
The highest and the smallest salaries are: 7 and 1
Last edited on Feb 9, 2015 at 8:17am
Feb 9, 2015 at 6:21am
after further testing it seems like the problem is that lowest/highestSalary is only holding onto single digit numbers, UNLESS there are no single digit numbers in the file? Any idea what is causing this? My program worked fine until I separated this part into a function. :/
Last edited on Feb 9, 2015 at 6:24am
Feb 9, 2015 at 6:36am
Im not an expert myself but i would try to use if instead of while.

1
2
3
4
5
6
7
8
9
                if(vect.at(i) > highestSalary){
			highestSalary = vect.at(i);
			outFile << highestSalary; // write the highest salary data to the stats.txt file
		}

		if (vect.at(i) < lowestSalary){
			lowestSalary = vect.at(i);
			outFile << lowestSalary; // write the lowest salary data to the stats.txt file
		}
Last edited on Feb 9, 2015 at 6:38am
Feb 9, 2015 at 6:42am
closed account (SECMoG1T)
vector<string???>vect)
string <-- ?????--> highestSalary, lowestSalary;

Why do you want to use strings
Feb 9, 2015 at 6:49am
well, the data in the txt file is comma delimited, so the only way I knew how to extract it and put it into a vector was with a string. My program is actually working correctly if there are no single digit numbers now. However the second I change it so that some of the data has single digits, it screws up and gives the highest salary as the highest single digit number.

edit here is my code updated so far:
1
2
removed



output :

Please select 1, 2, 3, or 4:
1. Search salary
2. Generate stats file
3. Print salaries
4. Quit program
2
Salary Data (in dollars):
$07 $01 $13 $50 $37 $06
highest:07
lowest:07

highest:07
lowest:01

highest:13
lowest:01

highest:50
lowest:01

highest:50
lowest:01

highest:50
lowest:01

The stats.txt file has been generated.
The highest and the smallest salaries are: $50 and $01
Please select 1, 2, 3, or 4:
1. Search salary
2. Generate stats file
3. Print salaries
4. Quit program


HOWEVER, if I change the data file that goes into the vector I get this:


Please select 1, 2, 3, or 4:
1. Search salary
2. Generate stats file
3. Print salaries
4. Quit program
2
Salary Data (in dollars):
$7 $1 $13 $50 $37 $6
highest:7
lowest:7

highest:7
lowest:1

highest:7
lowest:1

highest:7
lowest:1

highest:7
lowest:1

highest:7
lowest:1

The stats.txt file has been generated.
The highest and the smallest salaries are: $7 and $1
Please select 1, 2, 3, or 4:
1. Search salary
2. Generate stats file
3. Print salaries
4. Quit program
Last edited on Feb 9, 2015 at 8:18am
Feb 9, 2015 at 7:26am
line 18 and 22, string comparison is not the same as integer comparison.
so, convert to integer, then compare.

i.e. you "ok" as the output of the following program!
1
2
3
4
5
6
7
8
#include <iostream>

int main(){	

	if("10" < "9") std::cout <<  "ok";
		
return 0;	
}
Last edited on Feb 9, 2015 at 7:29am
Feb 9, 2015 at 7:26am
Of course. string::operator< does lexicographical comparison and a word that starts with character '7' is "larger" than words that start with any of '0'--'6'.

well, the data in the txt file is comma delimited, so the only way I knew

Please describe the exact format so that we can start to learn other ways.
Feb 9, 2015 at 7:26am
closed account (SECMoG1T)
try this

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

void load_vec(std::vector<double>& vec)
{
   std::ifstream read("salary.txt");///this file should only be comma delimited and shoudln't 
                                                     ///contain other alpha characters such as '$' for simplicity
   if(!read){std::cout<<"failed to open file\n"; return;}

   std::string temp_val;
   std::stringstream ss;
   double values;

   while(read&&std::getline(read,temp_val,','))
         ss<<" "<<temp_val;

    while(ss>>values)
        vec.push_back(values);
}


void print(const std::vector<double>& dvec)
{
    std::cout << "contents of vector: " <<std::endl;

    for(const auto& item : dvec)
        std::cout<<item<<" ";

    std::cout<<"\n\n";
}

void generateStats(const std::vector<double>&vect)
{
	std::ofstream outFile("stats.txt"); //generate new text file
	if(!outFile){std::cout<<"failed to open stats file\n"; return;}

    	double highestSalary=*(std::max_element(vect.begin(),vect.end()));
    	double lowestSalary =*(std::min_element(vect.begin(),vect.end()));

        outFile   <<"Max : "<<highestSalary<<" Min : "<<lowestSalary<<std::endl;
		std::cout << "The stats.txt file has been generated." << std::endl << "The highest and the smallest salaries are: "
			      << "$" << highestSalary << " and $" << lowestSalary << std::endl;
}

int main()
{
    std::vector<double> salary_vec;
    load_vec(salary_vec);
    print(salary_vec);
    generateStats(salary_vec);
}
Last edited on Feb 9, 2015 at 7:38am
Feb 9, 2015 at 7:35am
1
2
3
4
5
6
7
8
9
#include <iostream>
#include <string>
int main(){	

	if(std::stoi("10") < std::stoi("9")) std::cout <<  "ok";
	else std::cout << "not";
		
return 0;	
}


output:
not
Feb 9, 2015 at 8:15am
anup30 thank you so much!
Topic archived. No new replies allowed.