Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m not sure why I\'m having so many problems, C++ I was a breeze, II is drivin

ID: 3620310 • Letter: I

Question

I'm not sure why I'm having so many problems, C++ I was a breeze, II is driving me crazy! Anyway, I'm having a hard time understanding how to get the final answer. I have created this code to accept a text file with the information. I just can't get it to where I can search the information and return matched info. Please help by commenting on everything so I can understand what the process is. (see ch 10, programming challenges #18). Below I'll copy the problem as the instructor slightly modified it, and follows is my code. Please help!

Write a program that has an array of at least 11 string objects that hold peoples names and phone numbers. Read the names from a flat file. The file phoneNumbers.txt has been provided for you. You may that file or make up your own strings.

Listing of phoneNumbers.txt:
Becky Warren, 555-1223
Joe Looney, 555-0097
Geri Palmer, 555-8787
Lynn Presnell, 555-1212
Holly Gaddis, 555-8878
Sam Wiggins, 555-0998
Bob Kain, 555-8712
Tim Haynes, 555-7676
Warren Gaddis, 555-9037
Jean James, 555-4939
Ron Palmer, 555-2783

The program should ask the user to enter a name or partial name to search for in the array. Any entries in the array that match the string entered should be displayed. For example, if the user enters Palmer the program should display the following names from the list:
Geri Palmer, 555-8787
Ron Palmer, 555-2783

--------------------------------(Below is my code)----------------------------------------

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

// prototypes
string *makeArray(int);

int main()

{
int count;
int size;
string line;
string name;
string *phoneNumbers; // Will point to an array
ifstream inFile;
string filename;

cout << "Enter the name of the input file ";
getline(cin, filename);
inFile.open(filename.c_str());

//Determine number of items in file
size = 0;
while (getline(inFile, line))
{
size++;
}
inFile.close();

//Create array
phoneNumbers = new string[size];

inFile.open(filename.c_str());

//Read items into array
count = 0;
while (getline(inFile, line))
{
phoneNumbers[count].assign(line);
count++;
}

inFile.close();

for(count = 0; count < size; count++)
{
cout<<phoneNumbers[count]<< endl;
}

cout<<"Enter the name or partial name to search: ";
getline(cin, name);

for(count = 0; count < size; count++)
{
int space;
space = phoneNumbers[count].find(' ');

int comma;
comma = phoneNumbers[count].find(',') - 1;

if (!phoneNumbers[count].compare(0, comma, name))
{
cout<<phoneNumbers[count]<<endl;
}
else if (!phoneNumbers[count].compare(0,space - 1,name))
{
cout<<phoneNumbers[count]<<endl;
}
else if (!phoneNumbers[count].compare(space+1, comma, name))
{
cout<<phoneNumbers[count]<<endl;
}
}


system ("pause");

return 0;
}

Explanation / Answer

please rate - thanks combination your code and mine #include <iostream>
#include <string>
#include<ctype.h>
#include <fstream>
using namespace std;
string toUpper(string);
int main()
{int num=11,count=0,i,n,size;
string::size_type found;
string line;
string name,temp,temp2;
ifstream inFile;
string filename;
string *phone;
cout << "Enter the name of the input file ";
cin>> filename;
inFile.open(filename.c_str());
if(inFile.fail())             //is it ok?
       { cout<<"file did not open first time please check it ";
        system("pause");
        system("exit");
        }
//Determine number of items in file
size = 0;
while (getline(inFile, line))
{
size++;
}
inFile.close();
inFile.clear();
//Create array
phone = new string[size];
inFile.open(filename.c_str());
if(inFile.fail())             //is it ok?
       { cout<<"file did not open second time please check it ";
        system("pause");
        system("exit");
        }
//Read items into array
count = 0;
for(i=0;i
getline(inFile, phone[i]);
inFile.close();
cin.ignore ( 80, ' ' );              
cout<<"enter a name or partial name to search for: ";
getline(cin,name);
temp=toUpper(name);
cout<<" Names and numbers found ";
for(i=0;i
   {temp2=toUpper(phone[i]);
   found=temp2.find(temp);
   if (found
      {cout<<
       count++;
       }
   }
if(count==0)
      cout<<" Sorry no name with "<<<" in your phone list ";
system("pause");
return 0;
}
string toUpper(string s)
{int i;
for(i=0;i
    s[i] = toupper(s[i]);
return s;
}

();i++)>;>[i]>())>;i++)>;i++)>