C++: File input stream and skipping to next line? - c++

I'm trying to write a program that takes inputData from two files for a season of some sport (i.e.: football) and writes an output listing rankings each week. In the input file with the scores for each game, every week is separated by a line of '-' characters. I have an if, else loop set up where the program peeks at the first character of each line. If it sees a character other than '-', it reads normally. However, when it reads '-', the program will begin the output cycle.
The thing is, being that this is peek, I need to figure out how to get to the next line without creating new input and not cause a crash. All I can think of is using inStream.find( !'-' ); or inStream.seekg( !'-' );. Are there any other options I can use?
Also, for reference, the code is listed here: https://coderpad.io/475356. Look for line 80 for the problem area. Just don't make any edits please.
Thank you for your time.
P.S.: If anyone can find any other crashes, though, feel free to mention it.

How about just using ignore() to skip the line?
inStream.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
Make sure you have:
#include <limits>
If you prefer not to have std::, just put using std::numeric_limits; at the top of your file, and then drop std:: from the expression above.

Related

How to take this kind of a STDIN inputs in c++?

In lots of hackathon they provide testcases. So in those test cases they provide inputs in different ways through STDIN. This is one of the situation I struggle at all.
This is one kind of a input: (This is one testcase)
Mike
John
Ahmed
Sangha
Daniel
Ann
So here I have 6 inputs. But they don't provide number of inputs. So I can't use for loop or while loop to take this input. Because I don't know how many iteration I should do. So I have to take inputs until end. But this is not a input from a file. So I can't use EOF as well. This input is from STDIN. So how can I take this kind of input and store it in an array? Also I don't know how many elements should create in array. Because I don't know how many number of inputs are there. (different testcases may contain different number of inputs). How can I solve this kind of a problem?
Thank you so much for the help!
You should be able to use EOF, because stdin is a file (at least on linux)
The input here clearly shows you how many entries are presented. Note that each entry is on a newline so, considering it as a delimiter, you can parse your input stream and get the entries separated and also get the count of entries.
C++ getline() does this exactly:
istream& getline( istream& is, string& str, char delim );
Last param to getline is optional because it uses '\n' as default.
If you want to do it the raw way, you could setup a file pointer and do a lookahead for '\n' and read everything before it into new item of a variable array(vector) ...do this repeatedly until u reach EOF of stream(or filestream)

Fixing text in a .txt file

I've got a homework assignment that I have no idea how to even start. The instructions are to have an input .txt file with text that contains some mistakes. I have to fix that in the output .txt file, meaning, only 1 space between words, no space before a comma/punctuation and exactly 1 space after those. Capital letters at the beginning of a sentence. It also says that I don't have to use the ASCII table, because of the fact the capital letters are coded before lower case letters?
Input text example:
jaMEs , mY neIgHBor , Is A dOcTor . he SPoke eaSIlY , CLEarly And eloQuENtly.
Output:
James, my neighbor, is a doctor. He spoke easily, clearly and eloquently.
All we did in class was go over ifstream/ofstream and inputing/changing data in a .txt file, so I have no idea where to even begin. Is there a way to solve it, so it fixes any incorrect input text, or do I have to manually change every mistake in this particular text? No need to solve it for me. An example or some tips to get me started would be greatly appreciated!
Break the problem into pieces. First, read in the data from a file. Store it however you want, probably a string, then move on to the next part. Check each character and see if it is correct. If it is, move on. If not, make it correct and then move on. When you hit the end of the input, you are done.
To check if a character is correct, you just need to check if it is and should be lower case and if it should be a character. If it should be and isn't, fix it, otherwise move on.
Inspect each character as you read it. If it's a full-stop, then remember to upcase the next alphanumeric, otherwise to downcase it. If it's a space, then just remember that you've seen a space - don't print it until you see a word character.
Something like:
#include <algorithm>
#include <cctype>
#include <iostream>
#include <iterator>
int main()
{
int(*t)(int) = std::toupper;
char const*last = "";
std::for_each(std::istreambuf_iterator<char>{std::cin},
std::istreambuf_iterator<char>{},
[&](char c){if(std::isspace(c))last=" ";
else if(std::isalnum(c=t(c)))std::cout<<last<<c,last="",t=std::tolower;
else if(c==',')std::cout<<c,last=" ";
else if(c=='.')std::cout<<c,last=" ",t=std::toupper;});
}

c++ delete specific line number in a text file

I've been having some trouble achieving this functionality. I haven't been able to find code resolving this specific issue anywhere.
Thank you for taking time to help me, it means a lot
I made my own getline() with while() in it with an integer that increases with every line, and the number that i entered ignores the number of that line, writes to a new file, removes the original file, and renames the temporary file to the original, many thanks

How can I input 2 different variables on the same line while using setw for formatting?

Im using MSVS 2015 C++
this is what im working with. I got everything done except when i run it, after entering the seafood type, it jumps down to next line instead of going to the right for me to input the ppp under that heading. Ive tried everything i can think of, but I am fairly new to all of this. Any suggestions?
link to img of C++ code
When applied to an istream (such as cin), the semantic of the manipulator setw is to set the maximum number of characters that can be accepted for the next input.
Thus getline(cin,type) >> setw(23) >> pricepp doesn't move the cursor 23 characters to the right as you expected: it enforces the constraint that at most 23 characters shall be read from cin into ppp.
The I/O libraries are filled with such subtle details, and take some time to master. See here for a tutorial that will give you more examples.

Retrieving file from .dat via getline() w/ c++

I posted this over at Code Review Beta but noticed that there is much less activity there.
I have the following code and it works just fine. It's function is to grab the input from a file and display it out (to confirm that it's been grabbed). My task is to write a program that counts how many times a certain word (string) "abc" is found in the input file.
Is it better to store the input as a string or in arrays/vectors and have each line be stored separately? a[1], a[2] ect? Perhaps someone could also point me to a resource that I can use to learn how to filter through the input data.
Thanks.
input_file.open ("in.dat");
while(!input_file.eof()) // Inputs all the lines until the end of file (eof).
{
getline(input_file,STRING); // Saves the input_file in STRING.
cout<<STRING; // Prints our STRING.
}
input_file.close();
Reading as much of the file into memory is always more efficient than reading one letter or text line at a time. Disk drives take a lot of time to spin up and relocate to a sector. However, your program will run faster if you can minimize the number of reads from the file.
Memory is fast to search.
My recommendation is to read the entire file, or as much as you can into memory, then search the memory for a "word". Remember, that in English, words can have hyphens,'-', and single quotes, "don't". Word recognition may become more difficult if it is split across a line or you include abbreviations (with periods).
Good luck.