0

I am trying to store space ' ' as a recognized character directly into an array of chars by doing this:

char ** board = new char *[row];
for (int r = 0; r < row; r++) {
    board[r] = new char[col];
}

for (int r = 0; r < row; r++) {
    cout << "Enter input: " << endl;
    cin >> board[r];

}

But if I enter ' ' into the console it executes the Enter input line twice (when row is 33`) and then terminates. How would I go about storing input (including space character) directly into board?

Mahmud Adam
  • 3,489
  • 8
  • 33
  • 54
  • 2
    If you want the whole *line* then use [`std::string`](http://en.cppreference.com/w/cpp/string/basic_string) and [`std::getline`](http://en.cppreference.com/w/cpp/string/basic_string/getline) instead. – Some programmer dude Aug 30 '16 at 15:47
  • This isn't a [mcve]. What is the type of `board`? – Xirema Aug 30 '16 at 15:47
  • 2
    reopened as I think you need `get()` not `getline()` but the question needs more detail. – NathanOliver Aug 30 '16 at 15:48
  • I included the board type in my edit. – Mahmud Adam Aug 30 '16 at 15:50
  • 1
    I recommend using the I/O manipulator [noskipws](http://en.cppreference.com/w/cpp/io/manip/skipws) to prevent whitespace from being skipped. – Thomas Matthews Aug 30 '16 at 15:51
  • 1
    @MahmudAdam Any good reason why you aren't using a `std::vector board;`? Along with `std::getline()` it's all trivially solvable. – πάντα ῥεῖ Aug 30 '16 at 15:53
  • @ThomasMatthews if I do something like cin >> noskipws >> board[r] it just executes the entire program without allowing for user to enter input. – Mahmud Adam Aug 30 '16 at 15:58
  • @MahmudAdam: `noskipws` works fine. What you are not taking into account is that you will have to reset `cin` before you ask for input again. Use `cin.clear()` and `cin.ignore()` for that. See the example in my answer. – Remy Lebeau Aug 30 '16 at 21:57

3 Answers3

1

Try something more like this:

#include <iostream>
#include <iomanip>
#include <limits>

char ** board = new char *[row];
for (int r = 0; r < row; r++) {
    board[r] = new char[col];
}

for (int r = 0; r < row; r++) {
    std::cout << "Enter input: " << std::endl;
    std::cin >> std::noskipws >> std::setw(col) >> board[r];
    std::cin.clear();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}

However, as previously suggested in comments, you really should be using std::string and std::getline() instead. And if you can, change your array to std::vector<std::string>:

#include <iostream>
#include <vector>
#include <string>

std::vector<std::string> board(row);

for (int r = 0; r < row; r++) {
    std::cout << "Enter input: " << std::endl;
    std:getline(std::cin, board[r]);
}

If you cannot use std::vector, you can at least use std::string for reading the user's input and then copy its data into your char[][] array:

#include <iostream>
#include <string>
#include <cstring>

char ** board = new char *[row];
for (int r = 0; r < row; r++) {
    board[r] = new char[col];
}

for (int r = 0; r < row; r++) {
    std::cout << "Enter input: " << std::endl;
    std::string input;
    std::getline(std::cin, input);
    std::strncpy(board[r], input.c_str(), col-1);
    board[r][col-1] = '\0';
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Thanks! For some reason, using strncpy raises errors when I try to compile: "This function or variable may be unsafe. Consider using strncpy_s instead." However, strncpy_s doesn't seem to work either. – Mahmud Adam Aug 30 '16 at 21:46
  • 1
    `strncpy()` is a standard C function, `` wraps it inside the `std` namespace. `strncpy()` is "unsafe" because the output is not null-terminated if the input length is >= the specified destination length. `strncpy_s()` guarantees null-termination, but is a non-standard Microsoft function that `` does not wrap, so you would have to use `` instead and drop the `std::`. Or you can define `_CRT_SECURE_NO_WARNINGS` or use `#pragma warning` to disable the warning. See what happens when you mix C code with C++ code? You really should stop using `char*` strings in C++. – Remy Lebeau Aug 30 '16 at 21:55
0

Your problem is that the console doesn't recognize the ' ' as a valid input so it asks again. I don't know if get() or getline() instead of cin will work but you need to find a way so the console gets as input the whitespace, or you could create some sort of a filter so your program recognise a special char as a whitespace and stores is it like this. Hope that helps

Chris
  • 7
  • 1
  • Yeah. I understand that much. But I have no idea how to get the input so that it recognizes `' '` AND store that input into my board. – Mahmud Adam Aug 30 '16 at 17:16
  • You can set an if statement so if the input is a combination of characters it doesn't store that but a space – Chris Aug 31 '16 at 10:07
0

The only one works on my side is as follows:

Code:

#include <iostream>

using namespace std;

int main() {
    char msg1[10], msg2[10];
    cout << "Input message 1: ";
    cin.get(msg1, sizeof(msg1));
    cin.sync();
    cout << "Input message 2: ";
    cin >> msg2;
    cout << msg1 << endl;
    cout << msg2 << endl;

    return 0;
}

Result:

Input message 1: Hello World
Input message 2: Hello World
Hello Wor
Hello
n a
  • 47
  • 3