0
#include <iostream> #include <cstdlib> #include <cstring> #include <ctype.h> #include <cmath> using namespace std; int main(int argc, char *argv[]) { char buffer[100]= {}; int length = 0; cout << "Enter a string: "; do { cin >> buffer; } while(cin.eof()); length = strlen(buffer); int squareNum = ceil(sqrt(length)); cout << squareNum; cout << buffer; } 

Basically what I'm trying to do is fill a character array with the string I enter. However I believe it's only writing to the array until a space appears.

Ex. Input: this is a test Output: this Input:thisisatest Output:thisisatest 

Why is it stopping at the spaces? I'm pretty sure it has to the with the .eof loop

3 Answers 3

1
while(cin.eof()); 

It's not likely you are at eof() after reading one word. You want

while(! cin.eof()); 

or more properly a loop something like

while(cin >> buffer); 

Or, even better, dispense with the char arrays and use a string and getline.

Sign up to request clarification or add additional context in comments.

Comments

0

You can use std::getline() to get every line e.g

std::getline (std::cin,name)

By doing that your input will not be separated by white space delimiter

Comments

0

Instead of using cin.eof(), why don't you try something like:

std::string a; while (std::getline(std::cin, a)) { //... } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.