Jesse Martinez
Jesse Martinez

Reputation: 403

Is my input being written properly to my array?

#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

Upvotes: 0

Views: 69

Answers (3)

MinGW
MinGW

Reputation: 87

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

std::string a;

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

Upvotes: 0

Sarp Kaya
Sarp Kaya

Reputation: 3794

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

Upvotes: 0

Duck
Duck

Reputation: 27572

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.

Upvotes: 1

Related Questions