HELP PLZ!!

I'm having trouble of my code outputing Found. What the code has to do is that it is going to read the InFile than going to say if it is found or not. It keeps not reading the if statement in the searchName function. I really need help!!!



Jean Rousseau
1001 15.50
Steve Woolston
1002 1423.20
Michele Rousseau
1005 52.75
Pete McBride
1007 500.32
Florence Rousseau
1010 1323.338
Lisa Covi
1009 332.356
Don McBride
1003 12.32
Chris Carroll
1008 32.356
Yolanda Agredano
1004 356.00
Sally Sleeper
1006 32.362


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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <iostream>
#include <iomanip>
#include <cstring>
#include <math.h>
#include <fstream>
#include <string>
using namespace std;


int searchName(string searchName, const string NAME_AR[], const int AR_SIZE, bool &isFound);
void inputArrays(ifstream& iFile, string nameAr[], int idsAr[], float balanceAr[], const int AR_Size);


int main()
{
	const int AR_SIZE = 10;

    ifstream iFile;
    ofstream oFile;


    string inFileName;
    string outFileName;
    string sName;
    string nameAr[AR_SIZE];
    int idsAr[AR_SIZE];
    float balanceAr[AR_SIZE];
    int nameIndex;
    bool isFound = false;

    ///

    cout << "What input file would you like to use? ";
    getline(cin, inFileName);
    iFile.open(inFileName.c_str());
    cout << "What output file would you like to use? ";
    getline(cin, outFileName);
    oFile.open(outFileName.c_str());

     inputArrays(iFile, nameAr,idsAr,balanceAr, AR_SIZE);


    oFile << setw(9)<< left << "ID #" << setw(25) << "NAME" << setw(11)
    		<< right << "BALANCE DUE" << endl;
    oFile << setw(9)<< left <<  "----" << setw(25)
    		<< "--------------------" << setw(11)
    		<< right << "-----------" << endl;



    do{
        cout << "\nWho do you want to search for (enter done to exit): ";
        getline(cin, sName);
        if(sName!= "done")
        {
            nameIndex = searchName(sName, nameAr, AR_SIZE, isFound);
            if (isFound)
            {
            oFile << setw(9)<< left << idsAr[nameIndex] << setw(25) <<
            		nameAr[nameIndex] << setw(1) << "$" << right <<
            		setw(10) << setprecision(2) << fixed <<
            		balanceAr[nameIndex] << endl;
            
            }

        }
    }while(sName != "done");

    oFile << setw(20) << "Average Balance Due: $" ;
    cout << endl << "Thank you for using my program.";

    return 0;
}

void inputArrays(ifstream& iFile, string nameAr[], int idsAr[], float balanceAr[], const int AR_SIZE)
{

    for (int index = 0; iFile && index < AR_SIZE; index++)
    {
        getline(iFile,nameAr[index]);
        iFile >> idsAr[index];
        iFile >> balanceAr[index];
        iFile.ignore(1000, '\n');

    }

}

int searchName(string sName, const string NAME_AR[], const int AR_SIZE, bool &isFound)
{
    int searchIndex = 0;
	for (int index = 0; index < AR_SIZE; index++)
	{
        if (sName == NAME_AR[index])
        {
            searchIndex = index;
            isFound = true;
        }
	}
    if (isFound)
    {
        cout << "Found.\n";
    }
    else
    {
        cout << sName << " was not found.\n";
    }
	return searchIndex;
}


}
Have you tried stepping through it with a debugger, to see whether or not it's executing the contents of the if block?
Ya it just still wont read the
1
2
3
4
5
 if (sName == NAME_AR[index])
        {
            searchIndex = index;
            isFound = true;
        }
The names stored in your arrays have a trailing space that is causing your check to fail.

I'm sorry I'm still new to this. I don't really understand what you mean?

When you read the data from the file into your arrays, each name is stored in nameAr, each index of this array has each name but each name stored has a extra space at the end of it.

So, take this example

nameAr[0] = "Jean Rousseau "

thats what the contents look like in memory. Try typing that name with a space at the end and you will see its found.

The fault lies with the array population code.
Now everything is now saying found. But now it is going through the if loop.
Last edited on
Nvm I got it Thank you!

The original problem will be resolved if you modify the array population code to not put that trailing space on each name, it now seems you have another problem.

Your passing isFound to the function, which when you have searched 1 found name it sets this to true, so each time you pass this to the function its always true. There is nothing in the searchName function which sets this to false if a name wasn't found.



I dont see any logic in passing isFound as a parameter to the function to be honest.
Topic archived. No new replies allowed.