Search Results

Search found 2 results on 1 pages for 'user1781966'.

Page 1/1 | 1 

  • Returning and printing string array index in C

    - by user1781966
    I've got a function that searches through a list of names and I'm trying to get the search function to return the index of the array back to the main function and print out the starting location of the name found. Everything I've tried up to this point either crashes the program or results in strange output. Here is my search function: #include<stdio.h> #include<conio.h> #include<string.h> #define MAX_NAMELENGTH 10 #define MAX_NAMES 5 void initialize(char names[MAX_NAMES][MAX_NAMELENGTH], int Number_entrys, int i); int search(char names[MAX_NAMES][MAX_NAMELENGTH], int Number_entrys); int main() { char names[MAX_NAMES][MAX_NAMELENGTH]; int i, Number_entrys,search_result,x; printf("How many names would you like to enter to the list?\n"); scanf("%d",&Number_entrys); initialize(names,Number_entrys,i); search_result= search(names,Number_entrys); if (search_result==-1){ printf("Found no names.\n"); }else { printf("%s",search_result); } getch(); return 0; } void initialize(char names[MAX_NAMES][MAX_NAMELENGTH],int Number_entrys,int i) { if(Number_entrys>MAX_NAMES){ printf("Please choose a smaller entry\n"); }else{ for (i=0; i<Number_entrys;i++){ scanf("%s",names[i]); } } } int search(char names[MAX_NAMES][MAX_NAMELENGTH],int Number_entrys) { int x; char new_name[MAX_NAMELENGTH]; printf("Now enter a name in which you would like to search the list for\n"); scanf("%s",new_name); for(x = 0; x < Number_entrys; x++) { if ( strcmp( new_name, names[x] ) == 0 ) { return x; } } return -1; } Like I mentioned before I have tried a lot of different ways to try and fix this issue, but I cant seem to get them to work. Printing X like what I have above is just the last thing I tried, and therefor know that it doesn't work. Any suggestions on the simplest way to do this?

    Read the article

  • basic file input using C

    - by user1781966
    So im working on learning how to do file I/O, but the book I'm using is terrible at teaching how to receive input from a file. Below is is their example of how to receive input from a file, but it doesn't work. I have copied it word for word, and it should loop through a list of names until it reaches the end of the file( or so they say in the book), but it doesn't. In fact if I leave the while loop in there, it doesn't print anything. #include <stdio.h> #include <conio.h> int main() { char name[10]; FILE*pRead; pRead=fopen("test.txt", "r"); if (pRead==NULL) { printf("file cannot be opened"); }else printf("contents of test.txt"); fscanf(pRead,"%s",name); while(!feof(pRead)) { printf("%s\n",name); fscanf(pRead, "%s", name); } getch(); } Even online, every beginners tutorial I see does some variation of this, but I can't seem to get it to work even a little bit.

    Read the article

1