in my program, the user has to type in a name
ex: Mary Peterson
and the program has to run through an array where i stored the names, and find that name and display that name and say it has been found. I have a rough skeleton structure of how i thought the program should run. but of course that hasn't worked.
there are 5 names within the array, if that helps in any way.
i'm very new to the programming language, and would very much appreciate if you guys kept to the basics and thoroughly explain how you guys came up with your solutions
The loop will start at 0 and go up to less than the size of the array. The loop counter will be used as the index for the array. So all you have to do is put that if-clause inside the loop.
Two things. First, you need a loop, to step through each item in the array in turn.
Second, you only need a single match to consider the item 'found'. But in order to declare it 'not found' it must fail to match on every item. Hence a simple if/else like this isn't sufficient
1 2 3 4
if (searchName == student_name[i])
cout << "found"; // logic ok
else
cout << "not found"; // logic error
You could loop over the contents of the array using a for loop, and have a flag to check if you found it. Inside the loop, if you find a match, you set flag and say that you found the value. If you get to the end of the loop, and the flag isn't set, then you can say that you didn't find the student.
For the flag, either it could just be a bool called 'didFindStudent' or something, or you could have an int or something which is either -1 (didn't find anything) or something else (found it, and it's at this position in the array).
also, i don't think a for loop would help in this case. i'm not sure which loop to use and how to use the loop to run through each item in the array until a single match is found
Have you ever used a loop to look through an array? Here's an example; you can base your code off this, somewhat:
1 2 3 4 5 6 7 8 9 10
// assuming an array like this:
int array[10] = { 5, 2, 6, 9, 1, 8, 3, 7, 4, 0 };
// you can loop like this:
for (int i = 0; i < 10; ++i) {
std::cout << array[i] << "\n";
}
yeah for one of my option in my program, i have to have the program run and look through an array and display what's inside the array. that part works succesfully.
if it helps, i'll give the .txt file on what it looks like.
Mary Peterson, 95, 93, 77, 94, 77,
Jake Andersen, 90, 90, 95, 93, 49,
Susan Cooper, 79, 94, 44, 90, 73,
Mike Smith, 95, 93, 30, 79, 97,
Jim Blair, 53, 45, 97, 39, 59,
Clark Lee, 70, 95, 45, 39, 77,
Kennedy Davis, 77, 34, 55, 74, 93,
Kim Bronson, 93, 94, 99, 77, 97,
Sunny Hill, 79, 95, 59, 93, 95,
Sam Benson, 95, 75, 49, 75, 73,
i'm still struggling to get the program to read the name (i used a getline for that) and then match the search name with one of the student names that are stored in the array. also, not to confuse things now that i've shown you guys what the text file looks like, the names of the student are stored in their own 1-D array called student_names
set found = false
input searchname
start at first element in array
loop:
does name = searchname?
if yes
found = truebreak out of loop
increment loop counter
loop back:
if found = true
use name
else
declare name not found