0

Why can not getline()? Is it because of the string left in the buffer? I also acted to remove the buffer in doubt. But it's impossible. Do you know why?

int len;
    cout << "input length : ";
    cin >> len;

    int* intPass = new int[len];
    int* intSolu = new int[len];

    string strPass;

    getline(cin, strPass);

    cout << strPass;

    cout << "intPass output : ";
    for (int i = 0; i < len; i++) {
        cin >> intPass[i];
    }
L. kiju
  • 25
  • 3
  • why are you using `new[]`? Why do you expect a user to input a length? Why do you want to output a `std::string` char by char? [The C++ Programming Language](https://www.amazon.com/C-Programming-Language-4th/dp/0321563840/ref=cm_cr_arp_d_product_top?ie=UTF8) – Swordfish May 04 '19 at 06:41
  • @Swordfish where do you see the OP output a string char by char ? – bruno May 04 '19 at 06:50
  • @bruno misread the code. read `cin` for `cout`. – Swordfish May 04 '19 at 06:53
  • @Swordfish where ? in "cin >> intPass[i];" ? I do not think, notice _inPass_ is only initialized from "cin >> intPass[i];". You are wrong, and if you DV me because of that you are double wrong ... – bruno May 04 '19 at 06:57
  • @bruno it doesn't matter, it's bs anyway. – Swordfish May 04 '19 at 07:01
  • @Swordfish and it an array of _int_ rather than _char_ as you supposed. Probably the best is to remove all these remarks ? – bruno May 04 '19 at 07:05
  • `#include ` and `std::cin.ignore(std::numeric_limits::max(), '\n');` after `cin >> len;` and before `getline(cin, strPass);` to remove all extraneous characters from `stdin` before you call `getline()`. – David C. Rankin May 04 '19 at 07:12

1 Answers1

0

Why can not getline?

When you input the value for input length you enter a number and a newline, that newline is get by getline

If you entered for instance 123 aze qsd then strPass will be " aze qsd"

So you get a line, but not the expected one

If you want to flush all after the number on the same line you can double getline call

For instance also modifying your code to see more :

#include <iostream>
#include <string>
using namespace std;

int main()
{

  int len;
  cout << "input length : ";
  cin >> len;

  int* intPass = new int[len];
  int* intSolu = new int[len];

  string strPass;

  getline(cin, strPass);
  getline(cin, strPass);

  cout << "strPass is " << strPass << endl;

  cout << "intPass input :" << endl; // it is an input, not an output
  for (int i = 0; i < len; i++) {
    cin >> intPass[i];
  }

  cout << "intPass output is" << endl;
  for (int i = 0; i < len; i++) {
    cout << intPass[i] << endl;

  return 0;
}

Compilation and execution :

pi@raspberrypi:~ $ g++ -pedantic -Wall -Wextra c.cc
c.cc: In function ‘int main()’:
c.cc:13:8: warning: unused variable ‘intSolu’ [-Wunused-variable]
   int* intSolu = new int[len];
        ^~~~~~~
pi@raspberrypi:~ $ ./a.out
input length : 3
aze qsd
strPass is aze qsd
intPass input :
11
22
33
intPass output is
11
22
33
pi@raspberrypi:~ $ 

I encourage you to check if cin >> len success and if yes to check the value of len, for instance :

if (!(cin >> len) || (len < 0)) {
  cout << "invalid length" << endl;
  return -1.
}
bruno
  • 31,755
  • 7
  • 21
  • 36