C++ cin only reads the first word
Using >>
on a stream reads one word at a time. To read a whole line into a char
array:
cin.getline(str, sizeof str);
Of course, once you've learnt how to implement a string, you should use std::string
and read it as
getline(cin, str);
It would also be a very good idea to get a compiler from this century; yours is over 15 years old, and C++ has changed significantly since then. Visual Studio Express is a good choice if you want a free compiler for Windows; other compilers are available.
Getline always ignoring first word of input c++
cin >> fullName;
reads the first word of the full name.
getline(cin, fullName);
reads the rest of the name over top of the first word. Computer programs do exactly what you tell them to do and show somewhat less than zero mercy if that's the wrong thing to do.
So given John Jacob Jingleheimer Schmidt
cin >> fullName; // reads John into fullname
getline(cin, fullName); // reads Jacob Jingleheimer Schmidt into fullname,
// replacing John
cout << fullName << endl; // prints Jacob Jingleheimer Schmidt
Solution
Remove the cin >> fullName;
getline(cin, fullName); // reads John Jacob Jingleheimer Schmidt into fullname,
cout << fullName << endl; // prints John Jacob Jingleheimer Schmidt
C++ file output only accepts the first word
The extraction operator truncates on whitespaces. Use std::getline() to read the entire line.
Instead of:
cin >> mystring, "\n\n";
Make it:
std::getline( std::cin, mystring );
Getline only reads the first word
The problem is that you're using >>
to extract the string. >>
for strings normally stops on whitespace.
To read a whole line, do what you're already doing with cin
—use getline
:
cout << "Loading file..." << endl;
ifstream loadText(newTextFile);
getline(loadText, loadedText);
loadText >> loadedText;
loadText.close();
cout << loadedText;
How to only print the first word of a string in c++
try this:
const int SIZEB = 10;
char word[SIZEB];
cout << " Provide a word, up to 10 characters, no spaces. > " << endl;
cin.getline(word, SIZEB);
std::string input = word;
std::string firstWord = input.substr(0, input.find(" "));
cout << " The word is: " << firstWord << endl;
cout << endl;
You need to do:
#include <string>
File Input cuts off first word
Your problem is inData >> fileName;
after inData.open(inFile.c_str());
It will read the first word into fileName and move the current file position.
Related Topics
How to Do an Integer Log2() in C++
What Can Happen If Printf Is Called With a Wrong Format String
How to Read and Manipulate CSV File Data in C++
Using Unicode in C++ Source Code
What Does It Mean When a Numeric Constant in C/C++ Is Prefixed With a 0
Template Member Function of Template Class Called from Template Function
String Literal Matches Bool Overload Instead of Std::String
What's the Difference Between Size_T and Int in C++
Why Doesn't a Derived Template Class Have Access to a Base Template Class' Identifiers
Where to Put Default Parameter Value in C++
Prevent Static Initialization Order "Fiasco", C++
Conversion Constructor Vs. Conversion Operator: Precedence
Multiple Definition in Header File
Behavior of Post Increment in Cout