-
Question
I have a input file of 20 whole numbers, the user enters a number and it searchs the array for a match, if a match is found it tells you the number enterd was found and displays the number. What i need to be able to do is add a counter of how many times that number appears in the infile, currently it stops as soon as the number is found.
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <cassert>
using namespace std;
const int MAXSIZE = 20;
void get_score(int code[], int& count);
int find_match(int inCode, int code[], int count);
int main()
{
int code[MAXSIZE] = {0};
int inCode = 0, count = 0, location = 0;
int total = 0;
get_score(code, count);
cout << "Enter a score: ";
cin >> inCode;
while(inCode != -1)
{
location = find_match(inCode, code, count);
if(location < MAXSIZE)
{
cout << "Code: " << total << " "<< code[location] << endl;
}
else
cout << "Code not found\n";
cout << "Enter a Code: ";
cin >> inCode;
}
return 0;
}//end main
//------------------------------------------
void get_score(int code[], int & count)
{
ifstream fin;
fin.open("pricelist.txt", ios::in);
fin >> code[MAXSIZE];
assert(!fin.fail());
int c;
while(fin >> ws && !fin.eof())
{
if(count < MAXSIZE)//still room
{
fin >> code[count];
fin >> ws;
count++;
}
else
{
fin >> c;
fin >> ws;
cout << "No room for " << c << endl;
}
}
}
//search
int find_match(int inCode, int code[], int count)
{
int total = 0;
int sub = 0;
bool found = false;
while(sub < count && ! found)
{
if(inCode == code[sub])
found = true;
else
sub++;
}
if(found)
return sub;
else
return MAXSIZE;
}
-
>>What i need to be able to do is add a counter of how many times that number appears in the infile, currently it stops as soon as the number is found.
so remove the variable that "stops the loop as soon as the number is found" and add a counter (as an extra parameter to the function as well).
-
If you want to work with the STL, you can use the count function defined in the algorithm header.
Code:
#include <algorithm>
#include <iostream>
int array[10] = { 2, 5, 7, 2, 6, 5, 4, 3, 2, 5 };
int value;
std::cout << "Enter a value to search for: ";
std::cin >> value;
std::cout << "The value " << value << " exists "
<< std::count(array,array+10,value) << " times." << std::endl;
Possible output:
Code:
Enter a value to search for: 5
The value 5 exists 3 times.
If not, I would maybe suggest your find_match function be modified to return the number of times the item to be searched for is found. From your code example you don't really appear to need the location (index) of the array where the value to be searched for is found. Your code could therefore be rewritten somewhat such that this:
Code:
location = find_match(inCode, code, count);
if(location < MAXSIZE)
{
cout << "Code: " << total << " "<< code[location] << endl;
}
else
cout << "Code not found\n";
Can be made to look like:
Code:
int num = find_match(inCode, code, count);
if( num )
// If match was found, we already know the value equals inCode, we don't need
// to output code[location] to display that value, saves a bit of typing
cout << "Code: " << num << " "<< inCode << endl;
else
cout << "Code not found\n";
-
ala Sebastiani
Code:
find_match(inCode, code, count, total);
cout << "Code: " << total << " instances of "<< inCode<< "found in code" << endl;
void find_match(int inCode, int code[], int count, int & total)
{
int sub = 0;
while(sub < count)
{
if(inCode == code[sub++])
++total;
}
}
-
Keeps on countring
I tried something similiar but with multiple entries are entered it beings to stack and total just keeps adding up from what it previously had.
Code:
find_match(inCode, code, count, total);
cout << "Code: " << total << " instances of "<< inCode<< "found in code" << endl;
void find_match(int inCode, int code[], int count, int & total)
{
int sub = 0;
while(sub < count)
{
if(inCode == code[sub++])
++total;
}
}
-
Then set total to zero before you call the find_match function.
-
or you could do this:
Code:
int numOfInCode;
numOfInCode = find_match(inCode, code, count);
cout << "Code: " << numOfInCode << " instances of "<< inCode<< "found in code" << endl;
int find_match(int inCode, int code[], int count)
{
int sub = 0;
int total = 0;
while(sub < count)
{
if(inCode == code[sub++])
++total;
}
return total;
}
note that it doesn't matter whether you declare total in main() and send it to find_match as a reference or declare total local to find_match and return the final result of total back to main explicitly, you still need to set it to zero each time you call find_match().