1

Here is the question. I need to read some names from a file, for example:

James Brown /n
Peter Lee /n
Chris Wu /n
Steven Huang /n
Kelly Yang /n

First, I need to know how long the longest name in the input stream is, then I need to create a dynamic 2-D char array. Finally, put the names into the dynamic array.(In this case,I need to create a 5*12 array since there are 12 letters in "Steven Huang".)

How can I read the input stream to know the number '12' but not extract them to put them into the array using cin>> in C++?

All suggestions will be appreciated.

Capt_Steel
  • 25
  • 7
  • 1
    I guess you will find useful information here: https://stackoverflow.com/questions/5343173/returning-to-beginning-of-file-after-getline – Damien Oct 06 '20 at 13:05
  • 1
    This looks like [The XY Problem](http://xyproblem.info/). I think you should read the file once and put things read to `std::vector` instead. If you really want char array, construct it from the vector. – MikeCAT Oct 06 '20 at 13:06
  • 1
    I need to know how long the longest name in the input stream is` apparently you are coding in `C` style that is why you have this problem. Just use `std::string` and there is no such problem this memory management is solved by library. – Marek R Oct 06 '20 at 13:10
  • but the names are from the keyboard typed by user,not a file :( – LEE_march_ten Oct 06 '20 at 13:38
  • @LEE In this case, the best is to follow the advice of MikeCAT and memorize everything in a `std::vector>` – Damien Oct 06 '20 at 13:46
  • 1
    The keyboard is a single-pass device. If you need to go through keyboard input more than once you have to store it somewhere.. – Pete Becker Oct 06 '20 at 13:49

2 Answers2

0

I guess the 'easiest' answer would be to just use the cin for getting out single characters. Even though it's not an efficient one it seems you are more interested in learning the basic language, so it might help to follow your line of thinking.

char c;
std::string s;
std::vector<std::string> vector_of_strings;
while(cin >> c)
{
  s = s + c;
  if ( c == '\n')
  {
    vector_of_strings.emplace_back(s);
    s = "";
  }
}

then you could just iterate through vector_of_strings, check each length and do your post processing.

another way of going there would be to try std::getline

#include <sstream>
std::string line;
std::vector<std::string> vector_of_strings;
std::getline(std::cin, line);
while(line.length() > 0) //stop when user enters empty line
{
    std::getline(std::cin, line);
    vector_of_strings.emplace_back(line);
}

and then again some postprocessing

0

You need to extract characters, then unextract them. To do that, you need to keep the characters read into account and unextract that amount of characters. Example:

int read = 20;
std::string data;

// Get 20 characters
for (int i = 0; i < read; i++) {
    data += std::cin.get();
}

// Unget 20 characters
for (int i = 0; i < read; i++) {
    std::cin.unget();
}

This code reads 20 characters from std::cin and stores them in variable data and then unextracts 20 characters, thus putting the stream into its previous state.

Akib Azmain Turja
  • 1,142
  • 7
  • 27