Modifying specific characters in text input (C++) [closed] - c++

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I receive text with special characters (such as á) so I have to manually search and replace each one with code (in this case "á")
I would like to have code to search and replace such instances automatically after user input. Since I'm a noob, I'll show you the code I have so far - however meager it may be.
// Text fixer
#include <iostream>
#include <fstream>
#include <string>
int main(){
string input;
cout << "Input text";
cin >> input;
// this is where I'm at a loss. How should I manipulate the variable?
cout << input;
return 0;
}
Thank you!

An easy method is to use an array of substitution strings:
std::string replacement_text[???];
The idea is that you use the incoming character as the index into the array and extract the replacement text.
For example:
replacement_text[' '] = " ";
// ...
std::string new_string = replacement_text[input_character];
Another method is to use switch and case to convert the character.
Alternative techniques are a lookup table and std::map.
The lookup table could be an array of mapping structures:
struct Entry
{
char key;
std::string replacement_text;
}
Search the table using the key field to match the incoming character. Use the replacement_text to get the replacement text.

Related

how to parse line into number and string? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 11 months ago.
Improve this question
I would like to parse a sentence beginning with a number:
2 random sentece.
5 another one.
8 this is really long sentence.
Into int number holding the beginning number and the rest in std::string sentence. So in the first line, the parsing output will be number == 2 and sentence == "random sentence". The input is read from stdin, but the classical std::cin >> number >> sentence does'n work, since the parsing of string would end once it reaches a space. But I want to make the string beginning after the initial number to the end of line \n. So, how to do it in C++?
You can make use of std::getline and std::istringstream as shown below. In particular in the given program, std::getline is used to read line by line and std::istringstream is used to read first the integer and then the remaining sentence.
#include <iostream>
#include <string>
#include <sstream>
int main()
{
std::string sentence;
int inputInt = 0;
//read line by line
while(std::cin >> inputInt && std::getline(std::cin, sentence))
{
std::cout<< inputInt <<"-----"<<sentence<<std::endl;
//do the check here
}
}
Demo

Making a password type program that needs to accept letter and number combinations? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am making a shopping list program. For this program, I need to be able to type in a user input that accepts both number (1564, 121,1, etc) and word (hello, goodbye, etc) combinations. The program reads numbers just fine, but it cannot process words. Thank you in advance. The part of the code I am stuck with is below:
int code, option, count = 0;
double quantity, price, cost;
string description;
cin >> code;
while ((code != 123456789) && (count < 2))
{
cout << "Incorrect code, try again \n";
cin >> code;
count++;
if (count == 2)
{
cout << "max # of tries reached. Goodbye. \n";
system("pause");
}
}
Your code variable is now an int. If you wanted that to be a string, declare it so: std::string code;. Note that you might need to #include <string> in the very beginning. Also, if you want to compare it with numbers, either you call something like atoi() (string has .cstr()), or better yet, you might just compare it with "123456789". HTH.

Accessing data in a text file [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How could I access a text file and go through word by word. I understand how to open the file but just not how to pull out each word one by one. I think it has something to do with arrays?
Simply:
#include <fstream>
#include <iostream>
int main()
{
std::fstream file("table1.txt");
std::string word;
while (file >> word)
{
// do whatever you want, e.g. print:
std::cout << word << std::endl;
}
file.close();
return 0;
}
word variable will contain every single word from a text file (words should be separated by space in your file).

stopword removal in C++ code [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
can Anyone help me make the stopword to be removed..I could not.. still appear after run!
#include <iostream>
#include <cmath>
#include <fstream>
#include <cstdlib>
using namespace std;
int main()
{
char filename[50]; //open file
ifstream example;
cin.getline(filename , 50);
example.open(filename);
if(!example.is_open())
{
exit(EXIT_FAILURE);
}
char word[50];
example>>word;
while (example.good()&&word!="a"&& word!="an"&&word!="be"&& word!="at"&& word!="the")
{
cout <<word<<" "; // remove stopwords
example>>word;
}
system("PAUSE");
return 0;
}
can Anyone help me make the stopword to be removed..I could not.. still appear after run!
You cannot compare C-strings with the == operator. The easiest solution to your problem will be to use std::string:
string word;
example >> word;
while (example.good() && word != "a" && word != "an" && word != "be" && word != "at" && word != "the")
{
cout << word << " "; // remove stopwords
example >> word;
}
On the other hand, this will actually not remove all, as you call it, stopwords. It will just print all words until the first “stopword” is read, and then the whole loop will stop.
The problem is that you're using C-style strings, which are fiddly to use correctly. The simplest option is to use the C++ strings library:
#include <string>
std::string word;
and the rest of your program should work as expected. This will also prevent the hideous stack-corruption bug that your program will experience if an input word is too long.
If you really want to muck around with character arrays for educational purposes, then you'll need to use the C strings library to compare them:
#include <cstring>
if (std::strcmp(word, "a") != 0 && ...)
Your code compares the address of the array containing the input word with the address of a string literal; these will never be equal.
When removing stopwords, remove not only a few of them.
In addition, you should apply the Porter algorithm to your piece of code.
The Porter Stemmer has to be applied regarding string similarity if you wanna check a filtered text.
Yes, it is in C, but only applying a few words (like your question) is not an adequate removal procedure of stopwords. The C code gives you an impression if you really wanna stem in addition to removal of stopwords. This depends on the purpose.
Have done both in 2008 to filter many text fragments. Both was relevant.
hth
A competent compiler with warnings turned on will fix your problem for you. Here's what mine said:
warning: result of comparison against a string literal is unspecified (use strncmp instead)
[-Wstring-compare]
while (example.good()&&word!="a"&& word!="an"&&word!="be"&& word!="at"&& word!="the")
^ ~~~

How to properly read from a .csv? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am having memory trouble with my code, and figured out that my code was being read wrong. For example the last value is adding numbers, not sure why. Also the names aren't coming out right.
This is what the output is looking like:
4101,BRAEBURN02.07682e-3172.07691e-317
4021,DELICIOUS02.07682e-3172.07691e-317
4020,DELICIOUS02.07682e-3172.07691e-317
4015,DELICIOUS02.07682e-3172.07691e-317
4016,DELICIOUS02.07682e-3172.07691e-317
4167,DELICIOUS02.07682e-3172.07691e-317
4124,EMPIRE,1,1.14,145.202.07682e-3172.07691e-317
4129,FUJI02.07682e-3172.07691e-317
4131,FUJI02.07682e-3172.07691e-317
As you can see the Empire was separated properly with the exception of the the last value.
Here's my code: the cout part was just for my personal use to see if the values were being inputted properly.
int main()
{
string name;
double price;
int by_weight;
double inventory;
int plu_code;
ifstream infile;
infile.open("inventory.csv");
while(!infile.eof())
{
stringstream ss;
string line = "";
getline(infile,line);
Tokenizer tok(line, ",");
ss << line;
ss >> plu_code >> name >> by_weight >> price >>inventory;
cout << plu_code<<"" <<name<<"" << by_weight<<"" << price <<""<<inventory<<"\n";
table[plu_code] = new Product(plu_code, name,by_weight, price,inventory);
numProducts++;
}
return 0;
}
The Empire line works because it's the only one whose name contains no whitespace. When you read strings from a stream, they are delimited by whitespace, so you only get the first word. If there are more words after that, then reading a double or other numeric type will fail because the stream is still pointing at non-numeric characters. You are not testing that your input operations succeeded, but it should have been obvious from your output.
I'm not sure what effect your Tokeniser class is supposed to have here. But perhaps have a look at this SO question for tokenising commas out of the stream. You can use a single getline call with a comma delimiter to read the name, and then normal << operator for the others.
[Edit] In fact, after cleaning up your question layout I notice that the Empire line doesn't work. It's reading the rest of the line as the name, and then still outputting uninitialised values. Which suggests to me your Tokeniser doesn't do anything at all.