Odd Cout Behavior - c++

I'm sorry for the initial post. This is tested and reproducible.
I'm trying to get cout to work within a fstream while loop while detecting each character its parsing, but it's exhibiting an odd behavior with the Text getting overrided by the first variable that I'm trying to put into cout.
main.cxx
#include <string.h>
#include <fstream>
#include <iostream>
#include <stdio.h>
using std::string;
using std::fstream;
using std::noskipws;
using std::cout;
int main(int argc, const char *argv[]){
char pipe;
string word; // stores the word of characters it's working on at the moment
string filename = "Directory.dat";
int type = 0; // 2 types counter, starts at 0
int newindicator = 0; // for detecting a new * for the data set
fstream fin(filename.c_str(), fstream::in);
while(fin >> noskipws >> pipe){
if(pipe == '*'){ // if the character is an asterisk
type++;
newindicator = 0;
word.clear();
}else if (pipe == '\n'){ // if the character is next line
if(newindicator == 0){ // tells the reader to know that it just finished reading a *, so it doesn't print anything.
newindicator = 1;
}else {
if(type == 1){
cout << "new word as: ";
cout << word << "\n";
}else if (type == 2){
cout << "new word as: ";
cout << word << "\n";
}
word.clear(); // clears the word string as it's reading the next line.
}
}else{
word+=pipe;
}
}
return 0;
}
Directory.dat
*
Chan
Johnathan
Joespeh
*
Betty
Lady Gaga
Output
Chanword as:
new word as: Johnathan
new word as: Joespeh
Bettyord as:
new word as: Lady Gaga
Note that how "Chan" is overriding the characters "new " on the first line, but it's fine after that. This seems to happen on every new type I'm doing, and when its recalling a new set of type. Same with Betty on the next set, which overrides "new w" with "Betty" on that cout.
Any feedback would be much appreciated. Thank you!

I suspect your input file has Windows line endings. These contain the carriage return character that's handled differently on Unix.
https://superuser.com/questions/374028/how-are-n-and-r-handled-differently-on-linux-and-windows

Thank you all for the comments and feedback. Made the changes as suggested:
Corrected
#include <string.h>
#include <fstream>
#include <iostream>
#include <stdio.h>
using std::string;
using std::fstream;
using std::noskipws;
using std::cout;
int main(int argc, const char *argv[]){
char pipe;
string word; // stores the word of characters it's working on at the moment
string filename = "Directory.dat";
int type = 0; // 2 types counter, starts at 0
int newindicator = 0; // for detecting a new * for the data set
fstream fin(filename.c_str(), fstream::in);
while(fin >> noskipws >> pipe){
if(pipe == '*'){ // if the character is an asterisk
type++;
newindicator = 0;
word.clear();
}else if (pipe == '\n'){ // if the character is next line
if(newindicator == 0){ // tells the reader to know that it just finished reading a *, so it doesn't print anything.
newindicator = 1;
}else {
if(type == 1){
cout << "new word as: ";
cout << word << "\n";
}else if (type == 2){
cout << "new word as: ";
cout << word << "\n";
}
word.clear(); // clears the word string as it's reading the next line.
}
}else{
if (pipe != '\r'){
word+=pipe;
}
}
}
return 0;
}
Output
new word as: Chan
new word as: Johnathan
new word as: Joespeh
new word as: Betty
new word as: Lady Gaga

Related

C++ program need lines words and characters count

I have an assignment where i need to count lines, words and characters from a file. I'm having a problem counting the right amount of characters and words since if it gets doubled space it counts like a character and a word.
the output should be
Example
lines words characters filename
3 5 29 testfile
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <iomanip>
using namespace std;
int main(int argc, const char * argv[]) {
string lines, words, chars, file1, file2;
ifstream infile;
ofstream outfile;
char c;
int countLines = 0;
int countChars = 0;
int countWords = 0;
cout<< "Enter the file name" << endl;
cin >> file1;
infile.open(file1.c_str());
while(!infile.eof())
{
if(infile.peek() == 1)
break;
c = infile.get();
if(c != '\n')
countChars++;
else
countLines++;
if(c == ' '|| c =='\n')
countWords++;
}
// countChars = countChars - countWords;
cout << setw(12) << countLines << setw(12) << countWords << setw(12) << countChars << endl;
infile.close();
return 0;
}
Use getline for reading file line by line
while(getline(file,str))
{
countLines++;
countChars += str.length();
countWords += CountWords(str);
}
Which file is an iofstream object and str is a string. And for counting number of words(CountWords), you have several ways. One of them is:
#include <vector>
#include <string>
#include <boost/algorithm/string/split.hpp>
int countWords(std::string str) {
vector< std::string > result;
boost::algorithm::split_regex(result, str, regex( "\\s+" ));
return result.size();
}
I believe OP's purpose to ask this question is to find out why his/her code is not working, therefore I will answer in this perspective.
counting the right amount of words
C++ define EOF(end of file) as -1, so include a check for EOF too, or you will miss a word count.
if it gets doubled space it counts like a character and a word.
You can use a boolean test to solve this, if you encountered a space, turn on the boolean, and skip if next char is a space, too.
I suppose your character count doesn't count in punctuation? so check for c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z'. If your assignment count punctuation as character count too, ignore this point.
Below is a correct version code that is modified based on your code.
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <iomanip>
using namespace std;
int main(int argc, const char * argv[]) {
string lines, words, chars, file1, file2;
ifstream infile;
ofstream outfile;
char c;
bool findNextString = false;
int countLines = 0;
int countChars = 0;
int countWords = 0;
cout << "Enter the file name" << endl;
cin >> file1;
infile.open(file1.c_str());
while (!infile.eof())
{
if (infile.peek() == 1)
break;
c = infile.get();
// use the boolean to find next valid string
if (findNextString && c == ' ')
continue;
else
findNextString = false;
// there is a structure issue with your code.
// you should think of the priority of checking
// do not check by rejection, because you will count in punctuation too.
if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z')
{
countChars++;
}
else if (c == '\n')
{
countLines++;
countWords++; // <- add word too
}
else if (c == ' ' || c == EOF)
{
countWords++;
findNextString = true;
}
}
cout << setw(12) << countLines << setw(12) << countWords << setw(12) << countChars << endl;
infile.close();
return 0;
}

What could cause a method not to cout << a string that is in the same class?

I have 2 methods of a class HashTagger.
The first (void getTags(std::string line);) takes an input and produces a string formatted as I would like.
The other (void printTags();) should take that string and print it to console with a simple enough method.
However, the output never comes am I missing anything silly.
here is the main
#include "HashTagger.h"
#include <string>
#include <iostream>
using namespace hw02;
using namespace std;
int main() {
// Construct an object for extracting the hashtags.
HashTagger hashTagger;
// Read the standard input and extract the hashtags.
while (true) {
// Read one line from the standard input.
string line;
getline(cin, line);
cout << line << endl;
if (!cin) {
break;
}
// Get all of the hashtags on the line.
hashTagger.getTags(line);
}
// Print the hashtags.
hashTagger.printTags();
// Return the status.
return 0;
}
Update with source methods
#include "HashTagger.h"
#include <iostream>
using namespace std;
using namespace hw02;
void HashTagger::getTags(string line) {
int h = 0;
int i = 0;
//add a space after line, so we can check for end of line
line = line + " ";
// Loop over all characters in a line that can begin a hashtag
for (unsigned int j = 0; j < line.length(); ++j) {
char c = line.at(j);
// if "#" is found assign beginning of a hashtag to h
if (c == '#') {
h = j; //h is the beginning char of the hashtag
i = 1; //signifies that a hashtag has been begun
// checks that a hashtag has begun, then looks for a newline, ".", "?", or "!" and adds substring of the hashtag to hashtags_
} else if (i == 1 && (c == ' ' || c == '\r' || c == '.' || c == '?' || c == '!' )) {
hashtags_ = hashtags_ + "\n" + line.substr(h, j - h);
h = 0;
i = 0;
}
}
//TEST// cout << hashtags_ << endl;
}
After the last cycle through main loop, this is producing the output that I want as shown by the test in the last line above. However I would like that variable to carry into the printTags() method as output using the same cout << call.
void HashTagger::printTags() {
// print out hashtags_ to the console
cout << hashtags_ << endl;
}
and lastly header
#ifndef HASHTAGGER_H
#define HASHTAGGER_H
#include <string>
namespace hw02 {
class HashTagger {
public:
void getTags(std::string line);
void printTags();
std::string hashtags_;
};
}
#endif
Here is the test input
Test#of hash#tagging
#even!#when #starting? a line
or #containing a #comma,
and expected output
#of
#tagging
#even
#when
#starting
#containing
#comma,
I figured out why output was not coming out when I pressed return. Because the while(true) loop break had to hit an end of file signifier of some sort and had very strict rules in my IDE, it would only end the loop if I hit CTRL + D for EOF on my mac. But not just that, I had to enter the console window, exit it, and then enter it again due to some bug.
found the answer at Passing End of Transmission (Ctrl + D) character in Eclipse CDT console

How to parse this data of unknown size

I have a simple text file containing instructions per a line. e.g
A 1 1
B 2 1 A
C 3 1 A
D 4 1 B C
Basic syntax is Letter, Num, Num, Letter(s)
I just don't know what function I should be calling to parse the data, and how to parse it in the given syntax. I feel like there's so many ways to do it.
The following C++ example shows one of possible way to read single characters from file, controlling end of line:
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
using namespace std;
int main(void)
{
ifstream inpFile("test.txt");
string str;
char c;
while (inpFile.good()) {
// read line from file
getline(inpFile, str);
// make string stream for reading small pieces of data
istringstream is(str);
// read data ingnoring spaces
do
{
is >> c; // read a single character
if (!is.eof()) // after successful reading
cout << c << " "; // output this character
} while (is.good()); // control the stream state
cout << "[End of line]" << endl;
}
cout << "[End of file]" << endl;
}
Here istringstream is used to process single line that is got by getline.
After reading a char with is >> c value in c can be checked for content, e.g.:
if (!is.eof()) // after successful reading
{
// analyze the content
if ( isdigit(c) )
cout << (c - '0') << "(number) "; // output as a digit
else
cout << c << "(char) "; // output as a non-number
}
Note: if file can contain not single characters / digits, but numbers and words, type of c should be appropriate (e.g. string)
In C++, read an entire line and make a stream from it, then read from that stream with >>.
Example:
std::ifstream file(filename);
std::string line;
while (file.getline(line))
{
std::istringstream in(line);
char letter;
int number1;
int number2;
std::vector<char> letters;
if (in >> letter >> number1 >> number2)
{
char letter2;
while (in >> letter2)
{
letters.push_back(letter2);
}
}
}
This is C example that read lines, and then goes (using pointer) from the beginning to output readable characters (with code greater than 32):
#include <stdio.h>
#include <ctype.h>
#define MAX_LINE_LEN 80
int main(void)
{
FILE * inpFile = fopen("test.txt", "r");
char buf[MAX_LINE_LEN];
char *p;
while (!feof(inpFile))
{
// read a line from file
if (fgets(buf, MAX_LINE_LEN, inpFile) != NULL)
{
p = buf; // start from the beginning of line
// reading data from string till the end
while (*p != '\n' && *p != '\0')
{
// skip spaces
while (isspace(*p) && *p != '\n') p++;
if (*p > 32)
{
// output character
printf("%c ", *p);
// move to next
p++;
}
}
}
printf("[End of line]\n");
}
printf("[End of file]\n");
return 0;
}
To extract numbers and words from the line you can do something like:
// reading data from string till the end
while (*p != '\n' && *p != '\0')
{
// skip spaces
while (isspace(*p) && *p != '\n') p++;
if (*p > 32)
{
int num;
char word[MAX_LINE_LEN];
// trying to read number
if (sscanf(p, "%i", &num))
{
printf("%i(number) ", num);
}
else // read string
{
sscanf(p, "%s", word);
printf("%s(string) ", word);
}
// move to next space in the simplest way
while (*p > 32) p++;
}
}

Error implementing Caesar Cipher Using C++

I am trying to implement Caesar Cipher using C++. The directions are to use this file which is already encrypted:
5
Asi ymj rtrjwfymjx tzylwfgj.
Aqq rnrxd bjwj ymj gtwtlwtajx
Dni ldwj fsi ldrgqj ns ymj bfgj.
Tbfx gwnqqnl fsi ymj xnymjd ytajx
The number 5 represents the shift that is applied to the text. I have to decode the Caesar ciphered text and reverse the lines as in put line 4 in line 1's position and line 3 in line 2's. The first letter of each line does not need to be decoded (the uppercase letters).
The text should look like this after running the program:
Twas brillig and the sithey toves
Did gyre and gymble in the wabe.
All mimsy were the borogroves
And the momerathes outgrabe.
As of right now, I have this code:
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
using namespace std;
char decipher (char c, int shift);
int main(){
//declare variables
char c;
string deciphered = "";
int shift;
vector <string> lines;
//ask for filename and if not found, keep trying
ifstream inFile;
string filename;
cout << "What is the name of the file? ";
cin >> filename;
inFile.open(filename);
while (!inFile){
cout << "File not found. Try again: ";
cin >> filename;
inFile.open(filename);
}
//find shift from file
inFile >> shift;
//get lines from file
inFile >> noskipws;
while (inFile >> c){
char decipheredChar = decipher (c, shift);
deciphered += decipheredChar;
}
cout << deciphered;
}
char decipher (char c, int shift){
string letters = "abcdefghijklmnopqrstuvwxyz";
if (c == 'T'){
return c;
}
else if (c == 'D'){
return c;
}
else if (c == 'A'){
return c;
}
else if (c == ' '){
return c;
}
else {
int currentPosition = letters.find(c);
int shiftedPosition = currentPosition - shift;
if (shiftedPosition < 0){
shiftedPosition = 26 + shiftedPosition;
}
char shifted = letters[shiftedPosition];
return shifted;
}
}
The result I'm getting is this:
uAnd the momerathes outgrabeuuAll mimsy were the borogrovesuDid gyre and gymble in the wabeuuTwas brillig and the sithey tovesu
How do I get rid of the u's and also separate the words by line? I have an idea of reversing the lines using a vector and using a loop counting backwards but I'm not sure how to get to there yet. Please help. Thank you.
To answer your question, the 'u's are the newlines. You read them in and decipher them, so they change and the result is pulled from letters. You should be able to add another case to decipher() to leave newlines alone:
char decipher (char c, int shift){
string letters = "abcdefghijklmnopqrstuvwxyz";
if(c == '\n'){ // do not modify new lines.
return c;
}
else if (c == 'T'){
return c;
}
// ...
}
Probably the cleanest way to reverse the lines is parse them while you read the characters. You can them pop them from the vector in reverse order. A working (but not robust) example would be to add the following to your while loop:
while (inFile >> c){
char decipheredChar = decipher (c, shift);
deciphered += decipheredChar;
if(decipheredChar=='\n'){ //if full line
lines.push_back(deciphered); //push line
deciphered = ""; //start fresh for next line
}
}
lines.push_back(deciphered+'\n'); //push final line (if no newline)
while(!lines.empty()){
cout << lines.back(); //prints last line
lines.pop_back(); //removes last line
}
I say not robust because there are minor things you may still need watch out for. For instance, this reads stores newline from after 5, and if the file ends in a newline I've added an empty one on the end... I'll leave you minor details to clear up.

Removing words between < and > signs in a string c++

I'm not sure where to go from here. I know something needs to go after ifstr.get(c). It copies the exact words that I have in my text file called project.txt but I just need to remove any words that have the chars < or >?
Any help would be great. Thanks:)
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
string line;
char c;
ifstream ifstr("project.txt");
ofstream ofstr("past.txt");
if(ifstr.fail()){
cout<<"error!"<<endl;
} // if
ifstr.get(c);
while(!ifstr.eof()) {
cout<<c;
ifstr.get(c);
ofstr<<line<<endl;
} // while
cout<<endl<<"copy complete"<<endl;
ifstr.close();
ofstr.close();
system ("pause");
return 0;
} // main
Pseudo-code (iostream-esque conditions) for the question in title (also removes the angle brackets):
char c;
while (read_char_succeeded(&c))
if (c == '<')
while (read_char_succeeded(&c) && c != '>')
;
else
write_char(c);
Just another shot in the dark:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
ifstream ifstr("project.txt");
ofstream ofstr("past.txt");
if(ifstr.fail()){
cout << "error!" << endl;
} // if
bool skipOutput = false;
do
{
string word;
ifstr >> word;
if(!word.empty() && word[0] == '<')
{
skipOutput = true;
}
if(!skipOutput)
{
ofstr << word << " ";
// replicate the output to stdout
cout << word;
}
if(word[word.length()-1] != '>')
{
skipOutput = false;
}
} while(!ifstr.eof());
cout << endl << "copy complete" << endl;
ifstr.close();
ofstr.close();
//system ("pause"); Doesn't compile with my system
return 0;
} // main
If you're really just want to filter out words enclosed within '<' and '>' characters this should be sufficient. If you have more complex parsing rules for your <> tags you should elaborate your question.
I'm not sure, that this is what you wanted. Please take a look at the code!
//we create a boolean value, to know if we started skipping
bool skipStarted = false;
while(ifstr.get(c))
{
//if its a '<' and we havent started skipping jet,
//then we start skipping, and continue to the next char.
if(c=='<' && !skipStarted)
{
skipStarted = true;
continue;
}
//if its a '>' and we started skipping,
//then we finish skipping, and continue to the next char.
if(c=='>' && skipStarted)
{
skipStared = false;
ifstr.get(c);
if(c==' ')
continue;
}
//if we are skipping, then we go to the next char.
if(skipStarted)
continue;
//otherwise we simply output the character.
ofstr<<c;
}