0

I'm making a calculator.

I retrieve the user input from GUI and store it in std::vector<char> c

Now I need to take every char in c and add it to std::cin, this is because the calculator engine is based on std::cin and I just want to add a GUI layer on top.

I wrote some sample code to demonstrate my problem, this is not the actual application:

#include <iostream>
#include <vector>

int main()
{
    int length = 6;
    std::vector<char> in(length);

    in[0] = 'H';
    in[1] = 'e';
    in[2] = 'l';
    in[3] = 'l';
    in[4] = 'o';
    in[5] = '\0';

    for (int i = 0; i < length; ++i)
    {
        char a = in[i];
        std::cout << "a: " << a << std::endl;
        std::cin.putback(a);
    }

    char y = 0;
    while(std::cin >> y)
    {
        std::cout << "y: " << y << std::endl;
        if (y == '\0')
        {
            std::cout << "This is the end!" << std::endl;
        }
    }
}

My expected result was to get output from the while(std::cin >> y) loop.
The problem is there is no output.

Edit: Another way of thinking about my problem is. Let's say I made a program that depended on user input from std::cin, and the input could be any primary type. Now, if I wanted to test the program by giving it input without shellscripting, how would I do it (from within the program's source)?

snzm
  • 139
  • 1
  • 1
  • 10
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/187061/discussion-on-question-by-snzm-add-characters-into-stdcin). – Bhargav Rao Jan 21 '19 at 16:15
  • What do you expect this code to do, and what does it actually do? – Pete Becker Jan 21 '19 at 16:33
  • 1
    Note that `putback` often has a limit to the number of characters that you can put back, and that limit is often 1. – Pete Becker Jan 21 '19 at 16:36
  • @PeteBecker even if it was 1, it would have printed something. There is no output at all – snzm Jan 21 '19 at 19:02

1 Answers1

0

I'm not quite sure what you were going for, but here's my take.
I might be wrong in my understanding of your question, but this little snippet pushes your std::vector<char> into std::cin and then traverses it until you will encounter EOF.

#include <iostream>
#include <vector>

int main() {
  int length = 6;
  std::vector<char> in(length);

  in[0] = 'H';
  in[1] = 'e';
  in[2] = 'l';
  in[3] = 'l';
  in[4] = 'o';
  in[5] = '\0';

  for (auto a = in.crbegin(); a != in.crend(); ++a) {
    std::cout << "a: " << *a << std::endl;
    std::cin.putback(*a);
  }

  while (std::cin) {
    char y;
    std::cin >> y;
    std::cout << "y: " << y << std::endl;
    if (y == '\0') {
      std::cout << "This is the end!" << std::endl;
      break;
    }
  }
}

I'm guessing it didn't work for you since the first element of std::cin was EOF, since std::cin.pushback() operates on it as LIFO

  • the problem is there is no output from `while (std::cin)` – snzm Jan 21 '19 at 16:54
  • please read my answer. You start with '\0' and unless you intended to exit while loop immediately you need to think about the order of your chars – Pavlo Hrybok Jan 21 '19 at 16:58
  • even if you were to replace `'\0'` with another arbitrary `char` (say `'!'`), the problem would still persist. – snzm Jan 21 '19 at 17:05