new to character array

i need guidelines with searching for a character within a character array from a file. and more importantly, i need to understand the logic behind this a lot better. so any info would be much appreciated.
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
#include <iostream>
#include <fstream>
#include <string>
#define MAX 50
using namespace std;
int main() 
{
char name[MAX+1];
int getIndex(char[], char);
ifstream myfile("customer.txt", ios::in);
if (myfile.fail())
{
	cerr << "Failed to open the file\n";
	return 1;
}
while (! myfile.eof())
{
myfile.getline(name, sizeof(name));
cout << getIndex(name, 'a') << endl;  // searching for the index of the character “a”
} // while
myfile.close();
return 0;
} // main


int getIndex(char s[], char  what)
{
/*This is where i am stuck. i am suppose to return the character in the variable "what" and return a negative 1 if there is not variable in the character array "s"*/
} 
Last edited on

I think you'll need to pass the arrays length to getIndex as well. You could write to the function like this:

http://pastebin.org/209992

Fafner
Your algorithm shall look like

i = 0 ;
while( not at the end of character array)
{
if( array[i] == what ) // If character at position i is equal to what
return i ;
i = i+1;
}
return -1 ; // If charcter not found in the array , return -1


Last edited on
@fafner, can you write down your code again. it seemed to disappeared. THANKS for both of your help. im getting an understanding of how to do this assignment.

@dufresne- when i use your code, the program displays numbers instead.
this is what i wrote:
1
2
3
4
5
6
7
8
9
10
11
12
13
int getIndex(char s[], char what)
{
	int i =0;
	while (true)
	{
		if (s[i] == what)
			return i;
			i =i +1;
	}
	return -1;
	

}// getIndex 
well the problem with that is that ihave to return a -1 if the vairable isnt there. i changed the preivous while statement into while(i <80), and it works but it seems to stop reading when it beings to read the numbers.
the text file goes something like this,
1
2
3
4
Bob 10300 AZ  1567.00
Bob 10301 NM  10987.00
Bob 10303 NM  6545.00
Bob 10307 AZ  4564.00


for the first line my code returns me a negative one but there is an A there where "AZ" appears but i will try your code and see if it works. i cant atm. thank you again
Haha, I must have done something wrong in pastebin;) The problem with your code is that the while-statement continues even after the end of the array is reached, because the "return i" is only called if the char it is looking for is actually found. Here what I recommend:

1
2
3
4
5
6
7
8
int getIndex(char s[], char what, int length) {
	for (int i = 0; i < length; i++) {
		if (s[i] == what) {
			return i;
		}
	}
	return -1;
}


EDIT:
This will return -1 if the char isn't found. Sorry for all the reposting, I made some mistakes in the previous code;)

Fafner
Last edited on
ahh thanks. it works now. thank you!
Topic archived. No new replies allowed.