problems with getline code

Sorry to bother everyone here, but I am having a large amount of trouble with the getline feature of C++. The goal of my program is to read a txt file with the source of a previous assignment of mine to find "magic words" which include "cin", "cout", "main", "return" and "include"., I have attached both the code and the reference file. The problem I am having is that my getline isnt passing anything currently. I was hoping that with the current code it would pass 1 line of code at a time to my array so that I could check for the words specified in the assignment but that doesnt seem to be working as intended...

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <string>
#include <fstream>
#include <cstdlib>
#include <iostream>

using namespace std;

void FindWord(char[], char[], double);

int main()
{
     ifstream inFile;
     ofstream outFile;
     
     string line;
     
     inFile.open("AssignmentOne.txt");
     
     if (inFile.fail())
     {
          cout<<"\nThe file was not successfully opened"
              <<"\n Please check that the file currently exists."
              <<endl;
          exit(1);
     }
     cout<<"The file exists; opening.\n";
     
     outFile.open("Output.txt");
     
     //read and distplay the files contents
     
     char a[100];
     double count=1;
     
     while(getline(inFile,line))
     {
         inFile >> a[100];
         FindWord("cin",a,count);
         FindWord("cout",a,count);
         FindWord("main",a,count);
         FindWord("return",a,count);
         FindWord("include",a,count);
         count++;
         
     }
    
     inFile.close();
     outFile.close();
     system ("Pause"); 
     return 0;     
}

void FindWord(char a[], char b[], double count)
{
     int i,j,startPlace;
     
     for(int k=0;a[k]!=0;k++);       //determine the lenght of the magic word
     
     for(int i=0;b[i]!=0;i++)
     {
             int k=0;
             while((a[k+i]==b[k]) && (k<j) &&(a[k+i]!='\0'))
             {
                   k++;
             }
             if(k==j)
             {
                   startPlace=i+1;
                   cout<<"The magic word \""<<a<<"\" is found at column "<<startPlace
                       <<" of line "<<count<<".\n";
             }
     }
}


REFERENCE FILE:
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
31
32
33
34
35
36
37
38
/*
Objective: To determine how many terms of the infinite equation "pi=4-(4/3)+4(5)-4(7)..." are needed to calculate the value of pi accurately.
*/
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
while(1)
{
    int count=1;
    float pi, sum=4, n, temp;

    cout<<"Enter a value of pi:";       //value of pi is entered
    cin>>pi;
    cout<<"Enter the # of places after the decimal for the precision check:"; //needed to determine rounding with precision
    cin>>n;             
    while(abs(sum-pi) >= (0.05*pow(10,(-n))))         //precision check
    {
                       if((count%2)>0)         //odd numbers
                       {
                                   temp=((2*count)+1);
                                   sum=sum -(4 / temp);
                                   count++;
                       }
                       else                    //even numbers
                       {
                                  temp=((2*count)+1);
                                  sum=sum +(4 / temp);
                                  count++;
                       }
    }
    
    cout<<"The number of terms needed is "<<count<<" to calculate the value of "<<pi<<".\n\n";
}           
return 0;
}


Thanks in advance for any help.
Why don't you just use strings instead of char* to hold the data...strings have functions like .find() which would be helpful for you.
well I think our teacher wants us to use the character arrays for this since that was how our previous lab was. so far it doesnt display anything other than "The file exists; opening." which I assume means my getline is set up improperly. Any ideas?
Ok, getline will get a line from the file up until a '\n' (by default) and put it into a string (not sure if it works on char*)...it then will move the file pointer down one line. Therefore, your while loop has really strange logic...check for inFile.good() instead, and use the getline to get the data directly into the char* (or string).
Sorry about the retarded logic in the while loop, I only used what our junky text book had in their examples. I found out how to do it after a while of attempting random things. The key to getting this working using very similar code was to assign the string in the getline to the character array using the strcpy function.

If I was doing this for personal use I definately wouldnt have followed the retarded structure that this assignment came in... using strings it would have been much easier lmao.
Topic archived. No new replies allowed.