here is my code

int main()
{
    int i;
    char * cstr;
    char * p;
      string *userInput;
      int sizeOfArray;

      cin >> sizeOfArray;

      add(i,userInput,sizeOfArray);

      
system("PAUSE");

}


void add(int i, string *userInput, int sizeOfArray)
{    
     string Input;
     char * cstr;
     char * p;
     userInput = new string [sizeOfArray];
      for(i= 0; i< sizeOfArray; i++)
      {
      getline(cin,*userInput);
      getCommands(cstr,p,userInput,Input);
      }
             
      
      delete [] userInput;
}

i can only type the number for sizeOfArray,
and i cannot further enter anything..

Recommended Answers

All 10 Replies

your problem is switching between cin and getline().
Try:

int main()
{
    int i;
    char * cstr;
    char * p;
      string *userInput;
      int sizeOfArray;
 
      getline(cin, sizeOfArray);  //changed
 
      add(i,userInput,sizeOfArray);
 
 
system("PAUSE");
return 0;  //your main needs an int return val
 
}
 
 
void add(int i, string *userInput, int sizeOfArray)
{    
     string Input;
     char * cstr;
     char * p;
     userInput = new string [sizeOfArray];
      for(i= 0; i< sizeOfArray; i++)
      {
      getline(cin,*userInput);
      getCommands(cstr,p,userInput,Input);
      }
 
 
      delete [] userInput;
}

but it give another error:
no matching function for call to `getline(std::istream&, int&)' ...

Sry, I assumed getline() has an implementation with int, 2 solutions:
1. catch your sizeOfArray with a string and convert it to int with atoi()
2. This: and here is why http://icttrends.com/problem-using-getline-after-cin-in-c-program.html

int main()
{
    int i;
    char * cstr;
    char * p;
      string *userInput;
      int sizeOfArray;
 
      cin >> sizeOfArray;
      getline(cin,*userInput);
      add(i,userInput,sizeOfArray);
      

 
 
system("PAUSE");
return 0;
}
 
 
void add(int i, string *userInput, int sizeOfArray)
{    
     string Input;
     char * cstr;
     char * p;
     userInput = new string [sizeOfArray];
      for(i= 0; i< sizeOfArray; i++)
      {
      getline(cin,*userInput);
      getCommands(cstr,p,userInput,Input);
      }
 
 
      delete [] userInput;
}

Also if you want an in depth explanation.
http://www.daniweb.com/forums/thread90228.html

this time it give another error for this line :(

getCommands(cstr,p,userInput,Input);

cannot convert `std::string*' to `char*' for argument `3' to `void getCommands(std::string, char*, char*, std::string)'

lmgtfy.com/?q=cannot%20convert%20`std::string*'%20to%20`char*'%20
Please try and make me feel like I'm helping you learn and not debugging your work for you. There are thousands of these around. If you never tried it (not trying to insult you) it's a very good idea to just copy your errors and google it.

@tech9: Read the error message very carefuly -- it is telling you everything you need to know in order to fix the problem. You passed a std::string object, but the function requires char*. Now how do you correct that? Ask yourself how to convert std::string to char*. If you don't know then look at the online documentation for std::string, which as this one. Read through the list of methods and the descriptions for what they do and see if you can find one that does what you want.

void getCommands(char * cstr,char * p,string *userInput)
{

but my function should require a string, why it says the function need a char*???

int main()
{
    int i;
    char * cstr;
    char * p;
      string *userInput;
      int sizeOfArray;

      cin >> sizeOfArray;

      add(i,userInput,sizeOfArray); //call function add

      
system("PAUSE");
return 0;

}

void add(int i, string *userInput, int sizeOfArray)
{    
     
     char * cstr;
     char * p;
     userInput = new string [sizeOfArray];
      for(i= 0; i< sizeOfArray; i++)
      {
      getline(cin,*userInput); //get a line from input
      getCommands(cstr,p);
      }
      

      delete [] userInput;
}

the above code compile with no error,
but still, i can't enter anything after i input the sizeOfArray

>>but my function should require a string, why it says the function need a char*???

because you just passed a pointer to a string. add() allocates memory for the pointer but since the pointer was not passed by reference main() never knows a thing about it.

You need to pass the string by reference, not by pointer. No allocation will be necessary in add(). And variable i is useless -- delete it.

void add(string& userInput, int sizeOfArray);


int main()
{
    char * cstr;
    char * p;
    string userInput;
    int sizeOfArray;

      cin >> sizeOfArray;

      add(userInput,sizeOfArray); //call function add

      
system("PAUSE");
return 0;

}

void add(string& userInput, int sizeOfArray)
{    
     
     char * cstr;
     char * p;

     for(int i= 0; i< sizeOfArray; i++)
     {
      getline(cin,userInput); //get a line from input
// what does getCommands() do ???
      getCommands(cstr,p);
      }
      

}

Cin doesn't read characters after number. Try using cin.ignore after cin>>.

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.