Question

This is a discussion on Question within the C++ Programming forums, part of the General Programming Boards category; I have a input file of 20 whole numbers, the user enters a number and it searchs the array for ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    2

    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;
    }

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    >>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).
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    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";
    Last edited by hk_mp5kpdw; 12-03-2004 at 05:25 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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;
       }
    }

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    2

    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;
       }
    }

  6. #6
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Then set total to zero before you call the find_match function.

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21