-1

I am writing a void function the goes through a loop and each time it reads the whole line and saves it as text but when I run the program I get an empty line then the input mince the last line in the input is there any way to remove the empty line and print all the lines that I give to the stdin

#include <iostream>
#include <vector>
#include <cstdio>
#include <cmath> 
#include <algorithm>

void cap_tag(int n){
    for ( int i = 0; i < n ; i++  ){
        std::string text; 
        std::getline(std::cin , text );
        std::cout <<text << std::endl; 
    }
}

int main(){
    int n , q ; 
    std::cin >> n >> q;
    std::cout << n << " " << q << std::endl;
    cap_tag(n);
    return 0; 
}

my input : 4 3

hello world

my l0v3 !$ lisa @nn

what is wrong with this 5555

any-thing is not printed

random stuff

random stuff

random stuff

output : 4 3

hello world

my l0v3 !$ lisa @nn

what is wrong with this 5555

tochka
  • 29
  • 4
  • 1
    Does this answer your question? [Why does std::getline() skip input after a formatted extraction?](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) – Algirdas Preidžius Mar 05 '20 at 17:16
  • unfortunately it does not – tochka Mar 05 '20 at 17:18
  • @tochka How so? Your program seems like a perfect candidate for that behavior. `text` becomes empty, right? – Ted Lyngmo Mar 05 '20 at 17:19
  • This is likely the problem you're seeing. You could provide us with example input and output to show us exactly what is happening though, if that's not the case. – JohnFilleau Mar 05 '20 at 17:20
  • I am using an online compiler and when I define the stdin as a mix of letters and numbers and symbols my program prints an empty line then the lines I defined before but the first thing it prints is an empty line then the lines I defined but It does not print the last line – tochka Mar 05 '20 at 17:21
  • @tochka Then in order for us to replicate that, put that input in an `std::istringstream` instead and then we can all run and investigate what's going on. – Ted Lyngmo Mar 05 '20 at 17:22
  • @TedLyngmo yes text becomes empty only when I =0 – tochka Mar 05 '20 at 17:24
  • 2
    Please edit your post with the sample input that is causing the issue. – Thomas Matthews Mar 05 '20 at 17:24
  • @tochka "_unfortunately it does not_" Why? `std::cin >> n >> q;` is a formatted extraction, and your input becomes empty, when using `std::getline` after said formatted extraction. Did you even read that, linked, question? – Algirdas Preidžius Mar 05 '20 at 17:27
  • 1
    Now when we've seen the input, does adding this line before `std::getline` help? `std::cin.ignore(std::numeric_limits::max(), '\n');` – Ted Lyngmo Mar 05 '20 at 17:32
  • @ TedLyngmo no it makes things worse by that i mean my input becomes 4 3 hello world what is wrong with this 5555 random stuff random stuff and this is wrong the lines with symbols are gone from the output – tochka Mar 05 '20 at 17:34
  • Adding that `cin.ignore` line BEFORE the loop (either in the function or before the function - just sometime after the formatted input operation). Should clear this up. When you enter your loop, there's already a `\n` on the input. Your first call to `getline` consumes that and does nothing. If you put it in the `for` loop you're ignoring every other input. – JohnFilleau Mar 05 '20 at 17:37
  • @tochka It was meant as a _one time_ ignore before the `std::getline`. At the point where you go from formatted input to unformatted input. – Ted Lyngmo Mar 05 '20 at 17:39
  • i tried using 'std::cin.ignore()' and it does not help i get the same output as before – tochka Mar 05 '20 at 17:42
  • 1
    @tochka It _should_ help. Look here where I've made a simulated `cin` from a `std::istringstream`: https://godbolt.org/z/ZzZQ_d – Ted Lyngmo Mar 05 '20 at 17:44

1 Answers1

0
#include <iostream>
#include <vector>
#include <cstdio>
#include <cmath> 
#include <algorithm>

void cap_tag(int n){


    for ( int i = 0; i < n ; i++  ){
        std::string text; 

        std::getline(std::cin , text );

        std::cout <<text << std::endl; 



    }
}







int main(){

    int n , q ; 
    std::cin >> n >> q;
    std::cout << n << " " << q << std::endl;
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

    cap_tag(n);



    return 0; 

}
tochka
  • 29
  • 4