How to do a case insensitive search for strings! I need to be able to type lower and uppercase letters. This is my code so far:

// Search the stock's index given a symbol
int searchStockBySymbol(int numStocks, string *stockSymbols)
{
bool found = false;
string symbol;
cout << "Enter the symbol: ";
getline(cin, symbol);

for (int i = 0; i < numStocks; i++)
{
if (stockSymbols[i].compare(symbol) == 0)
{
found = true;
return i;
}
}
if (found == false)
{
cout << "ERROR! Symbol not found!" << endl;
}

exit (-2);
}
Not the most efficient, but this should be adequate for the task at hand:

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
#include <string>
#include <cctype>

// return a string with all upper case characters converted to lower case
std::string to_lower( std::string str )
{
    for( char& c : str ) c = std::tolower(c) ;
    return str ;
}

// return true if a and b compare equal ignoring case
bool eq_nocase( std::string a, std::string b ) { return to_lower(a) == to_lower(b) ; }

// return position of the stock in the array, -1 if not found
int find_stock_by_symbol( const std::string* stock_symbols, int num_symbols,
                          const std::string& symbol_to_find )
{
   if( stock_symbols != nullptr )
   {
       for( int i = 0 ; i < num_symbols ; ++i )
           if( eq_nocase( stock_symbols[i], symbol_to_find ) ) return i ;
   }

   return -1 ; // not found
}
Thank you. I'm trying to compile it, but it's giving me errors saying "str' is undefined.
It may help if you post the code that you tried to compile,
along with the verbatim text of the error diagnostic (which would contain the offending line number).
This is my entire code. I have to look up a trading symbol, case insensitive. My code isn't compiling after I added your suggestion. I'm not sure if I'm editing it correctly.

#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;

// Function prototypes
void ArrSort(string *[], int);

// Get the stock information from the file
void loadStocksFromFile(ifstream &inFile, int numStocks, string
*stockSymbols, string *stockNames, int *stockNumShares, double *stockPrices)
{
for (int i = 0; i < numStocks; i++)
{
string stockInfo = " ";
getline(inFile, stockInfo);

stringstream ss(stockInfo);

// Get stock symbol
string stockSymbol;
getline(ss, stockSymbol, ' ');

// Get company name
string stockName = "";
string token;

while (true)
{
getline(ss, token, ' ');

if (token[token.size() - 1] == '#')
{
token = token.substr(0, token.size() - 1);
stockName += token;

break;
}

stockName += token;
stockName += " ";
}

// Get number of shares
int stockNumShare = 0;
getline(ss, token, ' ');
stringstream(token) >> stockNumShare;

// Get price per share
double stockPrice = 0;
getline(ss, token, ' ');
stringstream(token) >> stockPrice;

// Store the information
stockSymbols[i] = stockSymbol;
stockNames[i] = stockName;
stockNumShares[i] = stockNumShare;
stockPrices[i] = stockPrice;
}
}

// Search the stock's index given a symbol
int searchStockBySymbol(int numStocks, string *stockSymbols)
{
bool found = false;
string symbol;
cout << "Enter the symbol: ";
getline(cin, symbol);

for (int i = 0; i < numStocks; i++)
{
if (stockSymbols[i].compare(symbol) == 0)
{
found = true;
return i;
}
}
if (found == false)
{
cout << "ERROR! Symbol not found!" << endl;
}

exit(-2);
}

// Sort the array of pointers
void ArrSort(string *iparr[], int size)
{
int startScan, minIndex;
string *minElem;

for (startScan = 0; startScan < (size - 1); startScan++)
{
minIndex = startScan;
minElem = iparr[startScan];

for (int index = startScan + 1; index < size; index++)
{
if (*(iparr[index]) < *minElem)
{
minElem = iparr[index];
minIndex = index;
}
}
iparr[minIndex] = iparr[startScan];
iparr[startScan] = minElem;
}

//Display the trading symbol 3 to a line
cout << "Available stocks: " << endl;
cout << endl;
for (int count = 0; count < size; count++)
{
if (count > 0 && (count % 3) == 0)
cout << endl;
cout << *iparr[count] << ' ';

}
cout << endl;
}

int main()
{
// Prompt user to enter filename
string filename;
cout << "Enter the filename: ";
getline(cin, filename);
cout << endl;

// Open the file
ifstream inFile;
inFile.open(filename.c_str());

// Display error message if the file cannot be opened
if (!inFile.is_open())
{
cout << "ERROR! File cannot be opened." << filename << endl;
return -1;
}

// Read the first line in the file to identify the number of stocks
string numStocksTemp = "";
getline(inFile, numStocksTemp);
int numStocks = 0;
stringstream(numStocksTemp) >> numStocks;

// Allocate memory for parallel arrays
string *stockSymbols = new string[numStocks];
string *stockNames = new string[numStocks];
int *stockNumShares = new int[numStocks];
double *stockPrices = new double[numStocks];

// Call the loadStocksFromFile function
loadStocksFromFile(inFile, numStocks, stockSymbols, stockNames, stockNumShares, stockPrices);

// Close the file
inFile.close();

//create array of pointers
string **SortedArray = nullptr;
SortedArray = new string *[numStocks];

//store address
for (int count = 0; count < numStocks; count++)
SortedArray[count] = &stockSymbols[count];

ArrSort(SortedArray, numStocks);

// Perform a search
cout << endl;
int index = searchStockBySymbol(numStocks, stockSymbols);
cout << endl;

// Display the stock information
cout << endl;
cout << fixed;
cout << setw(30) << left << "Company Name:";
cout << setw(25) << right << stockNames[index];
cout << endl;

cout << setw(30) << left << "Number of Shares:";
cout << setw(25) << right << stockNumShares[index];
cout << endl;

cout << setw(30) << left << "Current Price (per share):";
cout << setw(25) << right << setprecision(2) << stockPrices[index];
cout << endl;

cout << setw(30) << left << "Current Value:";
cout << setw(25) << right << setprecision(2) << (stockNumShares[index] * stockPrices[index]);
cout << endl;
cout << endl;

// Delete the memory
delete [] SortedArray;
delete [] stockSymbols;
delete [] stockNames;
delete [] stockNumShares;
delete [] stockPrices;

return 0;
}
> My code isn't compiling after I added your suggestion. I'm not sure if I'm editing it correctly.

Post the code that is not compiling, along with the verbatim text of the error diagnostic.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <string>
#include <cctype>
using namespace std;

// Function prototypes
void ArrSort(string *[], int);

// Get the stock information from the file
void loadStocksFromFile(ifstream &inFile, int numStocks, string
*stockSymbols, string *stockNames, int *stockNumShares, double *stockPrices)
{
for (int i = 0; i < numStocks; i++)
{
string stockInfo = " ";
getline(inFile, stockInfo);

stringstream ss(stockInfo);

// Get stock symbol
string stockSymbol;
getline(ss, stockSymbol, ' ');

// Get company name
string stockName = "";
string token;

while (true)
{
getline(ss, token, ' ');

if (token[token.size() - 1] == '#')
{
token = token.substr(0, token.size() - 1);
stockName += token;

break;
}

stockName += token;
stockName += " ";
}

// Get number of shares
int stockNumShare = 0;
getline(ss, token, ' ');
stringstream(token) >> stockNumShare;

// Get price per share
double stockPrice = 0;
getline(ss, token, ' ');
stringstream(token) >> stockPrice;

// Store the information
stockSymbols[i] = stockSymbol;
stockNames[i] = stockName;
stockNumShares[i] = stockNumShare;
stockPrices[i] = stockPrice;
}
}

// return a string with all upper case characters converted to lower case
std::string to_lower(std::string str)
{
for (char& c : str) c = std::tolower(c);
return str;
}

// return true if a and b compare equal ignoring case
bool eq_nocase(std::string a, std::string b) { return to_lower(a) == to_lower(b); }

// return position of the stock in the array, -1 if not found
int find_stock_by_symbol(const std::string* stock_symbols, int num_symbols,
const std::string& symbol_to_find)
{
if (stock_symbols != nullptr)
{
for (int i = 0; i < num_symbols; ++i)
if (eq_nocase(stock_symbols[i], symbol_to_find)) return i;
}

return -1; // not found
}

// Sort the array of pointers
void ArrSort(string *iparr[], int size)
{
int startScan, minIndex;
string *minElem;

for (startScan = 0; startScan < (size - 1); startScan++)
{
minIndex = startScan;
minElem = iparr[startScan];

for (int index = startScan + 1; index < size; index++)
{
if (*(iparr[index]) < *minElem)
{
minElem = iparr[index];
minIndex = index;
}
}
iparr[minIndex] = iparr[startScan];
iparr[startScan] = minElem;
}

//Display the trading symbol 3 to a line
cout << "Available stocks: " << endl;
cout << endl;
for (int count = 0; count < size; count++)
{
if (count > 0 && (count % 3) == 0)
cout << endl;
cout << *iparr[count] << ' ';

}
cout << endl;
}

int main()
{
// Prompt user to enter filename
string filename;
cout << "Enter the filename: ";
getline(cin, filename);
cout << endl;

// Open the file
ifstream inFile;
inFile.open(filename.c_str());

// Display error message if the file cannot be opened
if (!inFile.is_open())
{
cout << "ERROR! File cannot be opened." << filename << endl;
return -1;
}

// Read the first line in the file to identify the number of stocks
string numStocksTemp = "";
getline(inFile, numStocksTemp);
int numStocks = 0;
stringstream(numStocksTemp) >> numStocks;

// Allocate memory for parallel arrays
string *stockSymbols = new string[numStocks];
string *stockNames = new string[numStocks];
int *stockNumShares = new int[numStocks];
double *stockPrices = new double[numStocks];

// Call the loadStocksFromFile function
loadStocksFromFile(inFile, numStocks, stockSymbols, stockNames, stockNumShares, stockPrices);

// Close the file
inFile.close();

//create array of pointers
string **SortedArray = nullptr;
SortedArray = new string *[numStocks];

//store address
for (int count = 0; count < numStocks; count++)
SortedArray[count] = &stockSymbols[count];

ArrSort(SortedArray, numStocks);

// Perform a search
cout << endl;
//int index = searchStockBySymbol(numStocks, stockSymbols);
int index = find_stock_by_symbol(const std::string* stock_symbols, int num_symbols,
const std::string& symbol_to_find);
cout << endl;

// Display the stock information
cout << endl;
cout << fixed;
cout << setw(30) << left << "Company Name:";
cout << setw(25) << right << stockNames[index];
cout << endl;

cout << setw(30) << left << "Number of Shares:";
cout << setw(25) << right << stockNumShares[index];
cout << endl;

cout << setw(30) << left << "Current Price (per share):";
cout << setw(25) << right << setprecision(2) << stockPrices[index];
cout << endl;

cout << setw(30) << left << "Current Value:";
cout << setw(25) << right << setprecision(2) << (stockNumShares[index] * stockPrices[index]);
cout << endl;
cout << endl;

// Delete the memory
delete [] SortedArray;
delete [] stockSymbols;
delete [] stockNames;
delete [] stockNumShares;
delete [] stockPrices;

return 0;
}


1>------ Build started: Project: ConsoleApplication1, Configuration: Debug Win32 ------
1> StockLookUp.cpp
1>g:\stocklookup.cpp(184): error C2062: type 'int' unexpected
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Modify the 'Perform a search' part as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/********************************************************************************
// Perform a search
    cout << endl;
//int index = searchStockBySymbol(numStocks, stockSymbols);
    int index = find_stock_by_symbol(const std::string* stock_symbols, int num_symbols,
                                     const std::string& symbol_to_find);
    cout << endl;
    *********************************************************************************/

    // perform a search
    std::cout << "Enter the symbol to be searched: " ;
    std::string symbol_to_searh_for ;
    std::getline( std::cin, symbol_to_searh_for ) ;
    int index = find_stock_by_symbol( stockSymbols, numStocks, symbol_to_searh_for ) ;

    if( index == -1 ) // not found
    {
        std::cerr <<  "ERROR! Symbol not found!\n" ;
        return 2 ;
    }

// Display the stock information 
Topic archived. No new replies allowed.