can someone tell me why my getline statements aren't working correctly? My compliler seems to just skip over them in my functions... here's my code:
#include <iostream>
#include <string>
using namespace std;
void locate(string[],int&);
void substitute(string[],int&);
const int MAX_LINES=7;
int main ( void )
{
char command;
int i,currentLine;
string array[7],data;
currentLine=0;
for(i=0;i<=6;i++)
{
getline(cin,data);
array[i]=data;
}
cout<<"--stop!--"<<endl;
while(command!='q')
{
cout<<"command?";
cin>>command;
switch (command)
{
case'l':
locate(array,currentLine);
break;
case's':
substitute(array,currentLine);
break;
}
}
system("PAUSE");
return 0;
}
void locate(string array[], int& currentLine)
/*accepts a string and searches array starting at cuttentLine-th element and changes current line to the next line containing that string*/
{
int i,x,counter=0;
string datafind,line;
cout<<"Find What?"<<endl;
getline(cin,datafind);//skips this!
for(i=currentLine;i<=MAX_LINES;i++)
{
if(i>MAX_LINES-1)
{
cerr<<"ERROR: Entry not found"<<endl;
return;
}
line=array[i];
x=line.find(datafind);
if (x>=0)
{
currentLine=currentLine+counter;
cout<<"Array[CurrentLine]:"<<array[currentLine]<<endl;
return;
}
counter=counter+1;
}
system("PAUSE");
return;
}
void substitute( string array[], int& currentLine)
/*accepts a string and replaces that string within currentLine with a new string*/
{
int x;
string oldstring,newstring,line;
getline(cin,oldstring);//what you want to substitute out (skips this when i compile)
getline(cin,newstring);//what you want to substitute in
line=array[currentLine];
do
{
x=line.find(oldstring);
line.replace(x,oldstring.length(),newstring);
cout<<line<<endl;
}
while(x>=0);
array[currentLine]=line;
cout<<"CurrentLine:"<<array[currentLine]<<endl;//print new line
}