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
}

ater

cout<<"command?";
	  cin>>command;

you need to remove trainling whitespaces left in the buffer. modify the line to

getline( cin >> ws, datafind );//now this should be ok!

and you should be ok.

note: there are other logical errors in your code, locate them after you fix this one first.

yay! it works!... kind of... when i call my substitute function, my window closes immediately after the do while loop finishes... maybe a hint?:icon_redface:

this is the part that causes the problem:

do
     {
      x=line.find(oldstring); // *** what happens if you do not find it?
      line.replace(x,oldstring.length(),newstring);
      cout<<line<<endl;
     }
     while(x>=0);

also, for portability, make x a string::size_type and compare with string::npos eg.

string::size_type x = str.find(some_string) ;
if( x == string::npos ) { /* not found */ }

thank you so much! i'll work on it

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.