Hello tgp23,
How would I put in that vector? Would that be my "names" variable instead of the array?
|
To make a more proper use of a vector I would first create a struct. Something like:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
struct Animal
{
std::string s_name;
int s_?{};
int s_?{};
Animal(std::string name, int ?, int ?)
{
s_name = name;
s_? = ?;
s_? = ?;
}
};
|
Since I do not know what the numbers are for I do not know what to call them.
Then what you would have is
std::vector<Animal> animals;
After you input the name and 2 ints you would use
animals.emplace_back(name, int?, int?);
where "int?" is the name of the variable.
But if you can not use "app" with the "ofstream" then you are not likely to be able to use a vector yet.
Although the idea of the struct can still be used with an array.
For what it is worth looking at:
1 2 3 4 5 6 7 8
|
while (inFile >> names[count])
{
for (int j = 0; j < 2; j++)
{
inFile >> animalNumber[count][j];
}
count++;
}
|
This could be shortened to:
1 2 3 4 5 6
|
while (inFile >> names[count])
{
inFile >> animalNumber[count][0] >> animalNumber[count][1];
count++;
}
|
As a suggestion "inFile" is more descriptive than "ifIn". And I like to use "outFile" for an output stream.
The for loop is a bit over kill for just 2 columns.
When I get to this:
1 2 3 4
|
cout << "Add, Look, or Done? ";
cin >> decision;
if (decision == "Look") // If user types in Look
|
You are asking a lot of any given user here to enter the word correctly. This if statement is comparing to "Look", but the else if is comparing "Add" and "add". What happen if the caps lock key is on and someone types in "aDD" or "lOOK".
It would be simpler to either define "decision" as a "char" or just check "decision[0]". One disadvantage to using a "char" is that there might be more characters left in the input buffer that would need to be cleared.
One thing that you can count on is on any given day with any given user someone will break your program with out even trying.
In the if statement consider this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
if (decision == "Look") // If user types in Look
{
//cout << "\nName to look up? ";
//cin >> searchAnimal;
while (cout << "\nName to look up? (Done to quit): " && cin >> searchAnimal && searchAnimal != "Done")
{
int number = search(names, animalNumber, count, searchAnimal);
if (!number)
{
cout << ">>> There is no data in the array for " << searchAnimal << '\n';
}
else
{
cout << ">>> There are " << number << " " << searchAnimal << '\n';
}
//cout << "\nName to look up ? ";
//cin >> searchAnimal;
}
cout << "Thank You!" << endl;
}
|
The return statement at the end is not needed because the else ifs and will be bypassed leaving at the last line of "main" before the closing brace. Also the return in the else statement is not needed.
The "add" need reworked to output to a file. Not being able to use "app" you would have to write the part of the arrays used to the file. You would also need a variable to keep track of how much of the array is used or step through the array until you find an empty name.
Should have mentioned this earlier. It is always best to initialize your variables when they are defined.
1 2
|
int animalNumber[MAX_ROWS][MAX_COLUMNS]{};
int count{};
|
The empty {}, or the uniform initializer, available from C++11 on, will let the compiler choose what is the best form of zero to give the variable. Or you could put a number between the {} if you need.
Strings are empty when defined and do not need initialized.
Andy