Nov 22, 2015 at 3:27am UTC
I need some help trying to figure out why my program is not passing variables to other functions
#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>
// Ask the user to enter a name
std::string enterName( void )
{
std::string name;
std::cout << "Enter name: ";
std::getline( std::cin >> std::ws, name );
return name;
}
// Ask the user to enter an address
std::string enterAddress( void )
{
std::string address;
std::cout << "Enter address: ";
std::getline( std::cin >> std::ws, address );
return address;
}
// Ask the user to enter a phone number
std::string enterPhone( void )
{
std::string phone;
std::cout << "Enter phone number: ";
std::getline( std::cin >> std::ws, phone );
return phone;
}
void printRecord( const int record, const int numRecords, const std::string names[], const std::string addresses[], const std::string phones[] )
{
if ( record >= 0 && record < numRecords )
{
std::cout << "Name: " << names[record] << " Address: " << addresses[record] << " Phone: " << phones[record] << std::endl;
}
}
// Search the address book by name string
int searchRecords( const int numRecords, const std::string names[], const std::string addresses[], const std::string phones[] )
{
if ( numRecords > 0 )
{
std::string search;
std::cout << "Enter name to search for: ";
std::getline( std::cin >> std::ws, search );
int i;
for ( i = 0; i < numRecords; i++ )
{
if ( names[i] == search )
{
return i;
}
}
if ( i == numRecords )
{
return numRecords;
}
}
return -1;
}
// Sort the address book by name string ascending or descending
void sortRecords( const int numRecords, std::string names[], std::string addresses[], std::string phones[], bool descending )
{
if ( numRecords > 0 )
{
int i;
for ( i = 0; i < numRecords; i++ )
{
for ( int j = 0; j < numRecords - i - 1; j++ )
{
if ( descending )
{
if ( names[j] < names[j + 1] )
{
std::string temp = names[j];
names[j] = names[j + 1];
names[j + 1] = temp;
temp = addresses[j];
addresses[j] = addresses[j + 1];
addresses[j + 1] = temp;
temp = phones[j];
phones[j] = phones[j + 1];
phones[j + 1] = temp;
}
}
else
{
if ( names[j] > names[j + 1] )
{
std::string temp = names[j];
names[j] = names[j + 1];
names[j + 1] = temp;
temp = addresses[j];
addresses[j] = addresses[j + 1];
addresses[j + 1] = temp;
temp = phones[j];
phones[j] = phones[j + 1];
phones[j + 1] = temp;
}
}
}
}
}
else
{
std::cout << "There are no entries in the address book yet!" << std::endl;
}
}
// Display all of the records in the address book in table format
void printAllRecords( const int numRecords, const std::string names[], const std::string addresses[], const std::string phones[] )
{
if ( numRecords > 0 )
{
std::cout << "+--------------------+-----------------------+-----------+" << std::endl;
std::cout << "| Name | Address | Phone |" << std::endl;
std::cout << "+--------------------+-----------------------+-----------+" << std::endl;
for ( int i = 0; i < numRecords; i++ )
{
std::cout << "|" << std::setw( 20 ) << names[i];
std::cout << "|" << std::setw( 23 ) << addresses[i];
std::cout << "|" << std::setw( 11 ) << phones[i];
std::cout << "|" << std::endl;
}
std::cout << "+--------------------+-----------------------+-----------+" << std::endl;
}
else
{
std::cout << "There are no entries in the address book yet!" << std::endl;
}
}
int insertRecord( int numRecords, std::string names[], std::string addresses[], std::string phones[] )
{
names[numRecords] = enterName();
addresses[numRecords] = enterAddress();
phones[numRecords] = enterPhone();
if ( !names[numRecords].empty() && !addresses[numRecords].empty() && !phones[numRecords].empty() )
{
numRecords++;
}
else
{
std::cout << "Error! One of the address book entries is empty!" << std::endl;
}
return numRecords;
}
void saveRecords( const int numRecords, const std::string names[], const std::string addresses[], const std::string phones[] )
{
std::ofstream file;
file.open( "records.csv", std::ios::out | std::ios::app );
if ( file.fail() )
{
std::cerr << "Error opening file \"records.csv\" for writing!" << std::endl;
}
else
{
for ( int index = 0; index < numRecords; index++ )
{
file << names[index] << "," << addresses[index] << "," << phones[index] << std::endl;
}
file.close();
}
}
int restoreRecords( const int maxRecords, std::string names[], std::string addresses[], std::string phones[] )
{
int numRecords = 0;
std::ifstream file;
file.open( "records.csv" );
if ( file.fail() )
{
std::cerr << "Error opening file \"records.csv\" for reading!" << std::endl;
return 0;
}
else
{
while ( !file.eof() )
{
if ( numRecords == maxRecords )
{
file.close();
return numRecords;
}
std::string buffer;
std::getline( file, buffer );
if ( file.good() )
{
size_t comma1 = buffer.find_first_of( ',' );
names[numRecords] = buffer.substr( 0, comma1 );
size_t comma2 = buffer.find_last_of( ',' );
addresses[numRecords] = buffer.substr( comma1+1, comma2-comma1-1 );
phones[numRecords] = buffer.substr( comma2+1, buffer.size() );
numRecords++;
}
}
file.close();
}
if ( numRecords > 0 )
{
std::cout << "Restored " << numRecords << " records in the address book" << std::endl;
}
return numRecords;
}
void displayMenu()
{
std::cout << "+-+------------------------------------+" << std::endl;
std::cout << "|#| Address Book |" << std::endl;
std::cout << "+-+------------------------------------+" << std::endl;
std::cout << "|1| Enter records into address book|" << std::endl;
std::cout << "|2| Search the address book by name|" << std::endl;
std::cout << "|3| Sort address book by name (A-Z)|" << std::endl;
std::cout << "|4| Sort address book by name (Z-A)|" << std::endl;
std::cout << "|5| Print all the address book records|" << std::endl;
std::cout << "|6| Exit and Save|" << std::endl;
std::cout << "+-+------------------------------------+" << std::endl;
}
int menuChoice( const int minValue, const int maxValue )
{
char choice = 0;
while ( choice < minValue + '0' || choice > maxValue + '0' )
{
std::cout << "Enter option (" << minValue << "-" << maxValue << "): ";
std::cin.get( choice );
}
return static_cast<int>(choice - '0');
}
void menuLogic( const int choice )
{
}
// main function
int main( int argc, char** argv )
{
// Address book size
const int MAX_SIZE = 10;
// Address book records arrays
std::string names[MAX_SIZE];
std::string addresses[MAX_SIZE];
std::string phones[MAX_SIZE];
// The number of entries valid in the address book
int numRecords = restoreRecords( MAX_SIZE, names, addresses, phones );
bool endProgram = false;
while ( !endProgram )
{
displayMenu();
int choice = menuChoice( 1, 6 );
switch ( choice )
{
default:
break;
case 1:
if ( numRecords > MAX_SIZE-1 )
{
std::cout << "Address book is full!" << std::endl;
break;
}
numRecords = insertRecord( numRecords, names, addresses, phones );
break;
case 2:
{
int result = searchRecords( numRecords, names, addresses, phones );
if ( result == -1 )
{
std::cout << "There are no entries in the address book yet!" << std::endl;
}
else if ( result == numRecords )
{
std::cout << "No matching entries in the address book for the search string!" << std::endl;
}
else
{
printRecord( result, numRecords, names, addresses, phones );
}
}
break;
case 3:
sortRecords( numRecords, names, addresses, phones, false );
break;
case 4:
sortRecords( numRecords, names, addresses, phones, true );
break;
case 5:
printAllRecords( numRecords, names, addresses, phones );
break;
case 6:
saveRecords( numRecords, names, addresses, phones );
endProgram = true;
break;
}
}
// This will only be reached if choice 6 is selected
return 0;
}
code]
Put the code you need help with here.
[/code]