Read blank line C++ - c++

I am in a situation where i had a loop and everytime it reads a string but I dont know how to read blank input i.e if user enter nothing and hit enter, it remains there.
I want to read that as string and move to next input
below is the code
int times = 4;
while(times--)
{
string str;
cin>>str;
---then some other code to play with the string---
}

You would need to read the entire line using getline(). Then you would need to tokenize the strings read.
Here is a reference on using getline and tokenizing using stringstream.

char blankline[100];
int times = 4;
while(times--)
{
//read a blank line
cin.getline(blankline,100);
---then some other code to play with the string---
}

Related

How Do I use input to pass a string into a char array from a Text File

Hey I know I can just use a string to read from a text file. However I need to use a char array. Like If I was using a string I would do this
while (!input.eof()){
input >> s;
}
I am unsure how I would go about this if I don't know the length of the string. I know I can use getLine, but I'll prefer to use input.
I'm thinking that maybe I can use a loop to check until it reaches "\0"?
Anyway I have a feeling this question has been asked before, but if it has I can't find it. So sorry if that is the case.
You can consider istream::getline. Note that it can be use for C++ string and it must have a length limit for C string.
I think you should avoid check eof directly in while condition. It only returns true it reach end-of-file. So if you have multiple line, you read it, then do some calculate, the consequence can be unexpected when it reach the end-of-file right at reading step. So the check of EOF should be placed right after reading from stream like my example.
int main()
{
ifstream input("filename.txt");
const int MAX = 10000;
char characters[MAX];
while (true) {
input.getline(characters, MAX - 1, '\n');
if (input.eof())
break;
}
}

C++: Getline stops reading at first whitespace

Basically my issue is that I'm trying to read in data from a .txt file that's full of numbers and comments and store each line into a string vector, but my getline function stops reading at the first whitespace character so a comment like (* comment *) gets broken up into
str[0] = "(*";
str[1] = "comment";
str[2] = "*)";
This is what my codeblock for the getline function looks like:
int main() {
string line;
string fileName;
cout << "Enter the name of the file to be read: ";
cin >> fileName;
ifstream inFile{fileName};
istream_iterator<string> infile_begin {inFile};
istream_iterator<string> eof{};
vector<string> data {infile_begin, eof};
while (getline(inFile, line))
{
data.push_back(line);
}
And this is what the .txt file looks like:
101481
10974
1013
(* comment *) 0
28292
35040
35372
0000
7155
7284
96110
26175
I can't figure out why it's not reading the whole line.
This is for the very simple reason that your code is not using std::getline to read the input file.
If you look at your code very carefully, you will see that before you even get to that point, your code constructs an istream_iterator<string> on the file, and by passing it, and the ending istream_iterator<string> value to the vector's constructor, this effectively swallows the entire file, one whitespace-delimited word at a time, into the vector.
And by the time things get around to the getline loop, the entire file has already been read, and the loop does absolutely nothing. Your getline isn't really doing anything, with the current state of affairs.
Get rid of that stuff that involves istream_iterators, completely, and simply let getline do the job it was intended for.

Issue reading multiple lines from .txt file in C++

I'm trying to create a student database system for a school project. I'm trying to create a function that will search a .txt file for the student id and return all of the other variables on the string. This is working great if I search for the id of the student on the first line of the txt file but isn't capturing anything if I search for a student on another line. Am I missing something obvious?
The student data is 16 strings delimited by commas on each line. The student ID is the first string.
Thanks for any assistance!
StudentType findStudent(int studentToFind)
{
ifstream inFile;
inFile.open("students.txt");
string currentLine;
string dataRead[16];
istringstream is;
int currentStudent;
if (inFile)
{
while (getline(inFile, currentLine))
{
is.str(currentLine);
for (int i = 0; i < 16; i++)
{
getline(is, dataRead[i], ',');
}
currentStudent = stoi(dataRead[0]);
if (currentStudent == studentToFind)
{
/*
Do stuff here
*/
inFile.close();
return foundStudent;
}
cin.ignore(); // Not sure if this is needed but I was trying to
// clear the \n char if that was causing the issue
}
}
}
First : you aren't using cin, so get rid of cin.ignore().
Second : you should make sure you ALWAYS close infile at the end... so I would suggest not returning early or closing early, but using a break statement to exit your loop and then have a single return of whether you found it or not.
Third: Now that you removed all the 'gorp' we can finally hone in on the problem ... effectively the question is do we read all the lines?
Well let's check that, try printing out currentLine each time at the beginning of the while loop, if you know currentLine is updated properly, is is getting updated each time? yes...
ok then look at your next loop let's print out currentStudent each time... does currentStudent print the right value for each line? i.e. is the getline write into dataRead[i] actually writing what you think it should be to the right space?
Did you find the problem yet?
This is the kind of problem you need to learn how to solve yourself using print statements and a debugger. That what its for. If you are in visual studio run in debug mode and step through it... if not, use gdb. learn it and get used to it, you'll be using it a lot!
good luck

Using cin.getline() and cin.get()

I'm having some trouble understanding how to use cin.getline and cin.get. I believe I understand the problem, just trying to figure out how to use them in conjunction possibly to solve a problem.
I'm reading from a text file that's read in through cin through command line.
I created a vector of vectors called spaceStation and I want to load it with characters.For example, here's a small portion of the file
M
4
2
//Possible comments
....
#...
E#..
#...
For this, i read in the first three characters properly just using cin>> to load into a variable. Now I need to create a loop to read these multiple characters in on the same line. 1) I'm supposed to ignore all comments 2) I want to run the while loop until a new line is reached that contains no more information
I created a string s so getline(cin,s) should load the entire lines. My question is should i create a cstring s so i can access the individual characters to load or is there a way to use cin.get() to extract the individual characters of the line received by s.
string s;
vector<string> v; // this is the best choice you can iterate like this to get char by char:
for (int i=0;i<v.size();i++)
for (int j=0;j<v[i].size();j++)
v[i][j];
// or like this to get strings
for (int i=0;i<v.size();i++)
v[i];
getline(cin, s); // this is to read the \n (new line) after int in the 3rd line
while (getline(cin,s))// till end of file
{
if (s.find_first_not_of(".#E") == -1)
{
v.push_back(s);
}
}
for (int i=0;i<v.size();i++)
cout<<v[i]<<endl;
You also can take advantage of the string class functions.

Checking for incoming data type from file

I'm reading in from a .txt file that looks something along the lines of:
int string string string
int string string
int string string string string string
where the number of string types after each int is unknown. Each line represents a new group of values and each group needs to be into their own array value or whatever (don't know if I've worded that correctly but I hope you'll understand what I mean).
Is there a check I can perform so see if the incoming data from the file is an int so that if this is true and can tell my program its a new group of data?
I've tried
int check
if(check = file1.peek()){//start new group assignment}
but this doesn't appear to work. I need to be able to use the int value once I have found that it is the next data type being read in.
Thanks in advance.
There are several ways to do this, however I would suggest that maybe your groups are on separate lines, and the spaces within a line delimit items within a group correct?
So I would read the file, line-at-a-time and then split each line on spaces.
Let me know if the above fits and then we can explain how to do it.
Ok so you can use getline:
while (cin.good()) // reading from standard input until EOF
{
String line_str;
getline(cin, line_str); // get next line from standard input
istringstream line(line_str); // put line into string stream
if (line.good()) // read from string stream until EOF
{
int x;
line >> x; // read an integer from string stream
while (line.good()) // read from string stream until EOF
{
string s;
line >> s; // read strings from string stream
/// process s
}
}
}