Advertisement
Guest User

Untitled

a guest
Aug 16th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.27 KB | None | 0 0
  1.                 #include <iostream>
  2.                 #include <fstream>
  3.                 #include <string>
  4.                 #define MAX 50
  5.  
  6.                 using namespace std;
  7.  
  8.                 int main() // You are NOT allowed to change the main() function
  9.                 {
  10.                     char name[MAX+1];
  11.                     int getIndex(char[], char);
  12.                     ifstream myfile("customer.txt", ios::in);
  13.                     if (myfile.fail())
  14.                     {
  15.                         cerr << "Failed to open the file\n";
  16.                         return 1;
  17.                     }
  18.                     while (! myfile.eof())
  19.                     {
  20.                         myfile.getline(name, sizeof(name));
  21.                         cout << getIndex(name, 'A') << endl; // searching for the index of the > character “a”
  22.                     } // while
  23.                     myfile.close();
  24.                     return 0;
  25.                 } // main
  26.                 // Return the index where the character in the variable “what” is found > in the character array
  27.                 // Return -1, if the character in the variable “what” is NOT found in the > character array “s”.
  28.                 int getIndex(char s[], char what)
  29.                 {
  30.                     for(int i = 0; i <= strlen(s); i++)
  31.                     {
  32.                         char w = tolower(what);
  33.                         char c = tolower(s[i]);
  34.                                                
  35.                         if(c == w)
  36.                             return i;
  37.                     }
  38.                     return -1;// <----- having problem with this search code. the professor said is a 10 min code, really easy but i spend the last 7 hours, googling and trying codes. it wont work.. please help.
  39.                 } // getIndex
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement