0

I've tried using getline(), but the problem is that i have this in loop, and the program simply doesn't stop to get the inputs like it normally does when using cin.

Here's how my code looks like:

    for (int i=0; i<value; i++){
       cout<<endl<<"what are You saerching for?";
       getline(cin, searchFor[i]);
       cout<<"How it should be changed?";
       getline(cin, changeTo[i]);
    }

It works normally using cin, but that way i can't get more than 1 word to the table (both tables are strings).

Gortax
  • 3
  • 1
  • 2
  • 3
    Please reconsider your use of what are often considered bad practices: [`using namespace std;`](http://stackoverflow.com/q/1452721/1171191) and [`endl`](http://chris-sharpe.blogspot.co.uk/2016/02/why-you-shouldnt-use-stdendl.html) (those are links to explanations). – BoBTFish Jun 23 '16 at 13:08
  • 3
    can you give an example of input and expected output? beware that mixing `getline(cin, ...)` and `cin >> variable` can lead to weird results, since `>>` stops reading at the 1st whitespace character, and `cin` only at endlines – tucuxi Jun 23 '16 at 13:08
  • @BoBTFish true in general but irrelevant in this case. Unless OP is writing library code (unlikely, would have mastered IO first), I see no harm in avoiding the std:: prefix everywhere. – tucuxi Jun 23 '16 at 13:12
  • @tucuxi the example can be just any kind of text. I'm doing program (just for learning) that would fing a phrase in line of text and then change it. The loop is so you can change more than 1 phrase at the time. And i want to use `getline()` because i want to be able to also search for white spaces etc. – Gortax Jun 23 '16 at 15:50

1 Answers1

0

You could simply use scanf to do this.

Here is a little example that ask the user to type text, then print the user entry on "enter" key press

#include <iostream>
#include <stdio.h>

int main(int argc, char **argv){
    char buf[2048];
    std::cout << "enter something" << std::endl;
    scanf("%[^\t\n]", buf);
    printf("Your input : %s\n", buf);
    return 0;
}

Note In that example, buf is only 2048 bytes, this means that a user could enter more than 2048 caracters, and break this little code with a seg fault, be safe with strings.

tucuxi
  • 17,561
  • 2
  • 43
  • 74
richerarc
  • 250
  • 1
  • 8
  • Unless i'm doing something wrong or i don't know, can I put the buf variable into a table? I need that text to be in a variable, tho when i put that in the loop i can't even type in the text that i want put in. I mean i could not put that in the table, but that way it ahs to change the text right after i type both values in, which is not what i wanted to accomplish. – Gortax Jun 23 '16 at 16:05
  • You could do this by copying the idea behind `argv` . Since it's a `char **`, you can use it as a "array" of `char*` . Once again double check your memory allocation – richerarc Jun 24 '16 at 23:43
  • To address the **Note**, you could use `%2047[^\t\n]` as a format specifier. It is guaranteed to stop reading at the 2047th character, avoiding buffer overflow – tucuxi Jul 18 '16 at 13:36
  • Why the C code in C++? This is perfectly doable using `std::cin`. – eesiraed Feb 10 '19 at 06:28