Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I want to write a tool to change the word to another one, as I give! It matches that already some day and all the time there is a problem that my word is cut off or something is wrong. then I want a better program with the use of std :: regex but I can't cope with this problem anymore ..
#include <iostream>
#include <fstream>
void display_members()
{
std::string oldWord;
std::string newWord;
std::string tmp;
std::string getcontent;
std::ifstream openfile ("test", std::ios::in | std::ios::app);
std::cin >> oldWord;
std::cin >> newWord;
// if(openfile.is_open())
// {
while(! openfile.eof())
{
getline(openfile,tmp);
while((tmp.find(oldWord)) != std::string::npos)
{
tmp.replace(tmp.find(oldWord),newWord.length(),newWord);
}
std::cout << ","<<tmp << " " << " ";
// openfile >> getcontent;
// std::cout << getcontent<< " ";
}
// }
openfile.close();
}
int main()
{
display_members();
}
Try something more like this instead:
#include <iostream>
#include <fstream>
void display_members()
{
std::string oldWord, newWord, tmp;
std::string::size_type pos;
std::cin >> oldWord;
std::cin >> newWord;
std::ifstream openfile("test");
while (getline(openfile, tmp))
{
pos = 0;
while ((pos = tmp.find(oldWord, pos)) != std::string::npos)
{
tmp.replace(pos, oldWord.length(), newWord);
pos += newWord.length();
}
std::cout << "," << tmp << " " << " ";
}
}
int main()
{
display_members();
}
Related
This question already has an answer here:
How can I validate an integer input [duplicate]
(1 answer)
Closed 2 years ago.
I am writing a code to take an integer ID from the user which is later validated from a file.
Now when I don't enter a valid input in the cin it doesn't work (obviously). I have attached a sample code below like this:
std::cout << "Enter ID : " << std::endl;
std::cin >> id;
It's simple as that.
Now if I enter an input like a or any other character, I can check for any error using cin.fail()
But if I enter an input like 11a that doesn't seem to work. I have searched a lot on the internet but couldn't find any suitable solution.
Welcome to Stack firstly. Try this
#include <iostream>
#include <string>
int main() {
std::string theInput;
int inputAsInt;
std::getline(std::cin, theInput);
while(std::cin.fail() || std::cin.eof() || theInput.find_first_not_of("0123456789") != std::string::npos) {
std::cout << "Error" << std::endl;
if( theInput.find_first_not_of("0123456789") == std::string::npos) {
std::cin.clear();
std::cin.ignore(256,'\n');
}
std::getline(std::cin, theInput);
}
std::string::size_type st;
inputAsInt = std::stoi(theInput,&st);
std::cout << inputAsInt << std::endl;
return 0;
}
I made an output to visualize it. The variable "inputAsInt" would be the one you have to further work with.
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 4 years ago.
Improve this question
Write a program that “bleeps” out words that you don’t like; that is, you read in words using cin and print them
again on cout. If a word is among a few you have defined, you write out BLEEP instead of that word. Start with
one “disliked word” such as
string disliked = “Broccoli”;
When that works, add a few more.
So i was thinking on how i could create a code that would do that with a set of words using a vector, but all i could come up with was
int main()
{
vector<string> disliked = { "damn","stupid","fat" };
string word = "";
while (cin >> word) {
bool bad = false;
for (string x : disliked) {
if (x == word)
bad = true;
}
if (bad)
cout << "Bleep\n";
else
cout << word << "\n";
}
return 0;
}
i feel like that code can be shortened with taking out one of the if statements but i can't find a working way to do it.
overall it seems like more code than it should be for this simple check, also could the for part be done better? doing a whole loop of the whole vector seems to be too resource intensive in a case where the vector has lets say a 1000 words, maybe separating it by an if statement checking for a-d, f-j... etc, and then only running a for loop would be less heavy?
Sort your vector and use std::binary_search:
#include <iostream>
#include <vector>
#include <algorithm>
int main()
{
std::vector<std::string> disliked = { "damn","stupid","fat" };
sort(std::begin(disliked), std::end(disliked));
std::string word = "";
while (std::cin >> word)
{
if ( binary_search(std::begin(disliked), std::end(disliked), word))
{
std::cout << "Bleep ";
}
else
{
std::cout << word;
}
}
}
or use std::set instead of vector:
#include <iostream>
#include <set>
#include <algorithm>
int main()
{
std::set<std::string> disliked = { "damn","stupid","fat" };
std::string word = "";
while (std::cin >> word)
{
if ( disliked.find(word) != std::end(disliked) )
{
std::cout << "Bleep ";
}
else
{
std::cout << word;
}
}
}
Both of these solutions have logarithmic complexity for the word lookup instead of linear.
Use the std::find operation on a vector.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<string> disliked = { "damn","stupid","fat" };
string word = "";
while (cin >> word) {
if ( std::find(disliked.begin(), disliked.end(), word) != disliked.end() )
{
cout << "Bleep ";
}
else
{
cout << word;
}
}
return 0;
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I currently have this code and all of the formatting is correct... I just can't seem to get it to create rows in the output text file... How would I do this? I've tried to do a for loop in the function and in the main() but it seems to not work if I do that so I am very confused right now.
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
void output(string flightnumber, int arrival1, int arrival2, int realarrival1, int realarrival2, char dummy1, char dummy2)
{
ofstream flight;
flight.open("flightdata.dat");
if (flight.fail())
{
cout << "Error..." << endl;
exit(1);
}
cout << "Enter the flight number: ";
cin >> flightnumber;
if (flightnumber == "end")
{
exit(1);
}
flight << flightnumber << setw(4);
cout << "Enter the scheduled/actual arrival times (hh:mm hh:mm):";
cin >> arrival1 >> dummy1 >> arrival2 >> realarrival1 >> dummy2 >> realarrival2;
flight << arrival1 << dummy1 << arrival2 << setw(4) << realarrival1 << dummy2 << realarrival2;
flight << ('\n');
}
int main()
{
string flightnumber;
int arrival1, arrival2, realarrival1, realarrival2;
char dummy1, dummy2;
output(flightnumber, arrival1, arrival2, realarrival1, realarrival2, dummy1, dummy2);
return 0;
}
You are not appending your file whenever you write it truncates and creates a new file, add appending flag and open your file as.
flight.open("flightdata.dat", ios_base::app);
You are using uninitialized variables in main. They serve no purpose in there anyway. Remove variable declarations from main and put them in output:
void output()
{
string flightnumber;
int arrival1, arrival2, realarrival1, realarrival2;
char dummy1, dummy2;
ofstream flight;
flight.open("c:\\test\\___flightdata.txt", ios::app);
...
}
int main()
{
output();
return 0;
}
You may want to add ios::app flag as pointed out in the other answer.
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
I have
std::vector<string> x;
std::cout << "Please enter in values ";
std::getline(std::cin, numbers);
numbers.push_back(x)
Let's say the user inputted
3.9823 m/s 34.0 km/s 222 m/s
I was wondering how I'm able to only grab only the numbers in the string and disregard the units?
I want the values 3.9823, 34.0, and 222
A simple workaround
std::vector<string> x {"3.9823 m/s", "34.0 km/s", "222 m/s"};
for (int i=0; i<x.size(); i++)
{
stringstream ss(x[i]);
float t;
ss >> t;
cout << static_cast<int>(t) << endl;
}
Output
3
34
222
Well, no comment on the robustness of handling input this way, and your question's too self-inconsistent to answer exactly, but to give you an idea:
std::vector<double> results;
double n;
for (int i = 0; i < 3 && std::cin >> n; ++i)
{
results.push_back(n);
std::string units;
std::cin >> units;
}
Here is a way; (using strtof or strtol if you literally wanted only the integers.)
#include <iostream>
#include <vector>
#include <cstdlib> // strtof()
int main(){
std::vector<std::string> x;
x.push_back("3.9823 m/s");
x.push_back("34.0 km/s");
x.push_back("222 m/s");
char *pEnd;
float numbers[x.size()];
for(int i = 0; i<x.size(); ++i){
numbers[i] = strtof(x[i].c_str(), &pEnd);
}
for(int i = 0; i<x.size(); ++i){
std::cout<< numbers[i] << '\n';
}
}
Output;
3.9823
34
222
In the general case, you can use std::stringstream with error checking:
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
int main()
{
std::string line;
std::cout << "Please enter in values ";
std::getline(std::cin, line);
std::stringstream is( line );
std::vector< double > x;
double temp;
while (1) {
is >> temp;
while ( !is.eof() && (is.bad() || is.fail()) ) {
is.clear(); // clear the error flag.
is.ignore(256, ' '); // ignore upto space.
is >> temp; // try to read again.
}
if (is.eof()) {
break;
}
x.push_back( temp );
}
for ( int i = 0; i < x.size(); i++ )
std::cout << x[i] << " ";
return 0;
}
This question already has answers here:
Line by line reading in C and C++?
(7 answers)
Closed 8 years ago.
Im using the folowing code to read a file with first and last names in it.
firstname lastname
firstname lastname
do
{ in >> tmp2;
cout << tmp2;
} while(tmp2 != '\n');
this however doesn't detect the end of the line so I cannot progress as I get a infinite loop. Note tmp2 is a char.
how can I fix this.
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream fin("file");
string first, last, comment;
while (fin >> first >> last) {
cout << first << ' ' << last << endl;
getline(fin, comment); // get the rest annoying strings
}
fin.close();
return 0;
}
One solution: use a stringstream
std::stringstream sstrm;
std::string instr;
while (std::getline(std::cin, instr)) {
sstrm.str(instr);
std::string fname, lname;
sstrm >> fname >> lname;
std::cout << fname << ' ' << lname << '\n';
}
This discard anything after the first two tokens in a line.