ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
$POINT ID = 600001
I went to college
$POINT ID = 600002
I don't go to college.
It is tedious program
$POINT ID = 6000003
Shridhar Ram Rahim
$POINT ID = 6000004
Very Easy Program
$POINT ID = 6000005
Life is nice
$POINT ID = 6000006
$POINT ID = 600001Shri
I went to college
$POINT ID = 600002
I don't go to college.
It is tedious program
$POINT ID = 6000003
Shridhar Ram Rahim
$POINT ID = 6000004
Very Easy Program
$POINT ID = 6000005
Life is nice
$POINT ID = 6000006
BUT I want output.txt as
Code:
$POINT ID = 600001Shri
I went to college
$POINT ID = 600002Shri
I don't go to college.
It is tedious program
$POINT ID = 6000003Shri
Shridhar Ram Rahim
$POINT ID = 6000004Proactive
Very Easy Program
$POINT ID = 6000005Proactive
Life is nice
$POINT ID = 6000006Proactive
Please help me as I almost done the correct program but I think Only problem I could not repeat the loop in file for other similar strings.
After capturing numeric input from the stream, undoubtedly you forgot/neglected to also capture the newline that is still sitting in the stream's queue. This will not affect a subsequent attempt to capture another numeric input, but it will affect your ability to capture a string.
My suggestion is that you never accept raw numeric input; always read input as a string. For situations where you require numeric input, convert the inputted string into the desired format, and then verify to ensure the input is valid.
A very simple example:
Code:
#include <iostream>
#include <sstream>
#include <string>
int main()
{
int age;
std::string firstName;
std::string genericInput;
// prompt and get input
std::cout << "Enter your age: ";
std::getline(std::cin, genericInput);
// convert input to number and validate
std::stringstream ss(genericInput);
ss >> age;
if (ss.fail())
{
std::cerr << "\nFailed to capture number." << std::endl;
return -1;
}
// prompt and get string input
std::cout << "Enter your first name: ";
std::getline(std::cin, firstName);
if (std::cin.fail())
{
std::cerr << "\nFailed to capture first name." << std::endl;
return -1;
}
std::cout << "Hello " << firstName << "! So you are " << age << " years old?" << std::endl;
}
P.S. Format your code using better indentation, and then maybe I will peruse it more carefully.
Last edited by dwhitney67; 09-21-2011 at 06:16 AM.
I am formatting my code again properly. Please check it.
If I do not use File Management section from the program, i.e. if I remove File input/output section from the program , I got the correct output with cout<<search<<replace<<endl;. But as soon as I enter the File Section for find and replace, I am getting only one correct answer i.e. $POINT ID 6000001Shri. But for other numbers I won't get.
$POINT ID = 600001
I went to college
$POINT ID = 600002
I don't go to college.
It is tedious program
$POINT ID = 6000003
Shridhar Ram Rahim
$POINT ID = 6000004
Very Easy Program
$POINT ID = 6000005
Life is nice
$POINT ID = 6000006
$POINT ID = 600001Shri
I went to college
$POINT ID = 600002
I don't go to college.
It is tedious program
$POINT ID = 6000003
Shridhar Ram Rahim
$POINT ID = 6000004
Very Easy Program
$POINT ID = 6000005
Life is nice
$POINT ID = 6000006
BUT I want output.txt as
$POINT ID = 600001Shri
I went to college
$POINT ID = 600002Shri
I don't go to college.
It is tedious program
$POINT ID = 6000003Shri
Shridhar Ram Rahim
$POINT ID = 6000004Proactive
Very Easy Program
$POINT ID = 6000005Proactive
Life is nice
$POINT ID = 6000006Proactive
Please check my program so that I can proceed further.
Sigh... the point of using CODE tags is to preserve whatever indentation you may have used during the course of developing the software. Assuming that you have used some level of indentation, here's what your code would look like:
Code:
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <cassert> // ADDED
using namespace std;
struct NVH
{
char name[200];
int points[200];
};
int main()
{
ifstream file;
file.open("input.txt");
ifstream pch;
pch.open("input1.txt");
ofstream pchout;
pchout.open("output.txt");
struct NVH n[200];
long int i, j, c, p, x, y;
char nam;
string line;
file >> nam;
file >> c;
file >> nam;
file >> nam;
for (i = 0; i < c; i++)
{
file >> n[i].name;
string str("$POINT ID = ");
string replace;
replace = n[i].name;
for (p = 0; p < 2; p++)
{
file >> n[i].points[p];
}
x = n[i].points[0];
y = n[i].points[1];
for (j = x; j < y; j++)
{
std::stringstream tc;
tc << j;
string integer = tc.str();
string search;
search = str + integer;
while (!pch.eof())
{
getline(pch, line);
assert(search != replace);
string::size_type pos = 0;
while ((pos = line.find(search, pos)) != string::npos)
{
line.replace(pos + search.size(), search.size(), replace);
pos++;
}
pchout << line << endl;
}
}
}
file.close();
pch.close();
pchout.close();
return 0;
}
If you examine the code above, you will note that I had to include <cassert> so as to include the function prototype for assert(), which you use in your program.
And now, onto the issue of your application... the "input.txt" file that you provided earlier contains data that 1) seems incorrect, or 2) cannot be parsed using this code:
Code:
file >> nam;
file >> c;
file >> nam;
file >> nam;
Can you please clarify what your "input.txt" file is supposed to contain, and if what you posted earlier is correct, then please explain how do you expect the code above to function properly? Your OP indicated that your program was generating output, albeit incorrectly. Based on the code you posted, my assertion is that no output is being produced.
Sorry I did not put char nam[300] that is why output is not generating. Now with that modification and one more output1.txt which explains that if I do not use File Section i.e. the section from while(!pch.eof()) then I am getting correct answer but I could not able to reproduce it the file section. I am giving my program again
$POINT ID = 600001
I went to college
I am a disco dancer
$POINT ID = 600001
I like coconut
$POINT ID = 600002
I don't go to college.
$POINT ID = 600002
It is tedious program
$POINT ID = 6000003
Shridhar Ram Rahim
$POINT ID = 6000003
Ram Krishna Shiva Brhma
$POINT ID = 6000004
Very Easy Program
$POINT ID = 6000004
Finish the program
$POINT ID = 6000005
Life is nice
$POINT ID = 6000005
I am swiming
$POINT ID = 6000006
Sorry this is the end of the program
$POINT ID = 6000006
$POINT ID = 600001:Shri
I went to college
I am a disco dancer
$POINT ID = 600001:Shri
I like coconut
$POINT ID = 600002
I don't go to college.
$POINT ID = 600002
It is tedious program
$POINT ID = 6000003
Shridhar Ram Rahim
$POINT ID = 6000003
Ram Krishna Shiva Brhma
$POINT ID = 6000004
Very Easy Program
$POINT ID = 6000004
Finish the program
$POINT ID = 6000005
Life is nice
$POINT ID = 6000005
I am swiming
$POINT ID = 6000006
Sorry this is the end of the program
$POINT ID = 6000006
But I want output as
$POINT ID = 600001:Shri
I went to college
I am a disco dancer
$POINT ID = 600001:Shri
I like coconut
$POINT ID = 600002:Shri
I don't go to college.
$POINT ID = 600002:Shri
It is tedious program
$POINT ID = 6000003:Shri
Shridhar Ram Rahim
$POINT ID = 6000003:Shri
Ram Krishna Shiva Brhma
$POINT ID = 6000004:Proacive
Very Easy Program
$POINT ID = 6000004:Proacive
Finish the program
$POINT ID = 6000005:Proacive
Life is nice
$POINT ID = 6000005:Proacive
I am swiming
$POINT ID = 6000006:Proacive
Sorry this is the end of the program
$POINT ID = 6000006:Proacive
With my file1<<search<<replace<<endl;
I am getting correct output1.txt as
$POINT ID = 600001:Shri
$POINT ID = 600002:Shri
$POINT ID = 600003:Shri
$POINT ID = 600004:Proactive
$POINT ID = 600005:Proactive
$POINT ID = 600006:Proactive
Please check it from yor side. I just can reproduce correct output.txt as
$POINT ID = 600001:Shri
I went to college
I am a disco dancer
$POINT ID = 600001:Shri
But for remaining part I could not reproduce it.
Thank in advance
Last edited by somshridhar; 10-02-2011 at 11:54 AM.
Reason: CODE TAG Not Correct
Sorry I did not put char nam[300] that is why output is not generating. Now with that modification and one more output1.txt which explains that if I do not use File Section i.e. the section from while(!pch.eof()) then I am getting correct answer but I could not able to reproduce it the file section. I am giving my program again
$POINT ID = 600001
I went to college
I am a disco dancer
$POINT ID = 600001
I like coconut
$POINT ID = 600002
I don't go to college.
$POINT ID = 600002
It is tedious program
$POINT ID = 6000003
Shridhar Ram Rahim
$POINT ID = 6000003
Ram Krishna Shiva Brhma
$POINT ID = 6000004
Very Easy Program
$POINT ID = 6000004
Finish the program
$POINT ID = 6000005
Life is nice
$POINT ID = 6000005
I am swiming
$POINT ID = 6000006
Sorry this is the end of the program
$POINT ID = 6000006
$POINT ID = 600001:Shri
I went to college
I am a disco dancer
$POINT ID = 600001:Shri
I like coconut
$POINT ID = 600002
I don't go to college.
$POINT ID = 600002
It is tedious program
$POINT ID = 6000003
Shridhar Ram Rahim
$POINT ID = 6000003
Ram Krishna Shiva Brhma
$POINT ID = 6000004
Very Easy Program
$POINT ID = 6000004
Finish the program
$POINT ID = 6000005
Life is nice
$POINT ID = 6000005
I am swiming
$POINT ID = 6000006
Sorry this is the end of the program
$POINT ID = 6000006
But I want output as
$POINT ID = 600001:Shri
I went to college
I am a disco dancer
$POINT ID = 600001:Shri
I like coconut
$POINT ID = 600002:Shri
I don't go to college.
$POINT ID = 600002:Shri
It is tedious program
$POINT ID = 6000003:Shri
Shridhar Ram Rahim
$POINT ID = 6000003:Shri
Ram Krishna Shiva Brhma
$POINT ID = 6000004:Proacive
Very Easy Program
$POINT ID = 6000004:Proacive
Finish the program
$POINT ID = 6000005:Proacive
Life is nice
$POINT ID = 6000005:Proacive
I am swiming
$POINT ID = 6000006:Proacive
Sorry this is the end of the program
$POINT ID = 6000006:Proacive
With my file1<<search<<replace<<endl;
I am getting correct output1.txt as
$POINT ID = 600001:Shri
$POINT ID = 600002:Shri
$POINT ID = 600003:Shri
$POINT ID = 600004:Proactive
$POINT ID = 600005:Proactive
$POINT ID = 600006:Proactive
Please check it from yor side. I just can reproduce correct output.txt as
$POINT ID = 600001:Shri
I went to college
I am a disco dancer
$POINT ID = 600001:Shri
But for remaining part I could not reproduce it.
Thank in advance
--------------------------------------------------------------------------------
Sorry I did not put char nam[300] that is why output is not generating. Now with that modification and one more output1.txt which explains that if I do not use File Section i.e. the section from while(!pch.eof()) then I am getting correct answer but I could not able to reproduce it the file section. I am giving my program again
$POINT ID = 600001
I went to college
I am a disco dancer
$POINT ID = 600001
I like coconut
$POINT ID = 600002
I don't go to college.
$POINT ID = 600002
It is tedious program
$POINT ID = 6000003
Shridhar Ram Rahim
$POINT ID = 6000003
Ram Krishna Shiva Brhma
$POINT ID = 6000004
Very Easy Program
$POINT ID = 6000004
Finish the program
$POINT ID = 6000005
Life is nice
$POINT ID = 6000005
I am swiming
$POINT ID = 6000006
Sorry this is the end of the program
$POINT ID = 6000006
$POINT ID = 600001:Shri
I went to college
I am a disco dancer
$POINT ID = 600001:Shri
I like coconut
$POINT ID = 600002
I don't go to college.
$POINT ID = 600002
It is tedious program
$POINT ID = 6000003
Shridhar Ram Rahim
$POINT ID = 6000003
Ram Krishna Shiva Brhma
$POINT ID = 6000004
Very Easy Program
$POINT ID = 6000004
Finish the program
$POINT ID = 6000005
Life is nice
$POINT ID = 6000005
I am swiming
$POINT ID = 6000006
Sorry this is the end of the program
$POINT ID = 6000006
But I want output as
$POINT ID = 600001:Shri
I went to college
I am a disco dancer
$POINT ID = 600001:Shri
I like coconut
$POINT ID = 600002:Shri
I don't go to college.
$POINT ID = 600002:Shri
It is tedious program
$POINT ID = 6000003:Shri
Shridhar Ram Rahim
$POINT ID = 6000003:Shri
Ram Krishna Shiva Brhma
$POINT ID = 6000004:Proacive
Very Easy Program
$POINT ID = 6000004:Proacive
Finish the program
$POINT ID = 6000005:Proacive
Life is nice
$POINT ID = 6000005:Proacive
I am swiming
$POINT ID = 6000006:Proacive
Sorry this is the end of the program
$POINT ID = 6000006:Proacive
With my file1<<search<<replace<<endl;
I am getting correct output1.txt as
$POINT ID = 600001:Shri
$POINT ID = 600002:Shri
$POINT ID = 600003:Shri
$POINT ID = 600004:Proactive
$POINT ID = 600005:Proactive
$POINT ID = 600006:Proactive
Please check it from yor side. I just can reproduce correct output.txt as
$POINT ID = 600001:Shri
I went to college
I am a disco dancer
$POINT ID = 600001:Shri
You have included the processing of the entire input1.txt file within the loop over point numbers within the loop over names.
Once you have read the entire input1.txt file once and written the entire output, pch.eof() will be true for the remainder of the program, so there will be nothing to process for later point number or names.
The correct logic for the program is to process all of input.txt before you process any of input1.txt.
After all of input.txt has been read into internal data structures, you need a loop through all the lines of input1.txt.
Within each iteration of that loop, you must find the right part of the data (read earlier from input.txt) to make the correct change (if any) to that line before writing it out.
I tried with your suggesstion with little modifications as follows but I can get output in output1.txt correctly. But as I want to do the same thing with file section I could not.
Quote:
#include<iostream>
#include<string>
#include<fstream>
#include<iomanip>
#include<sstream>
#include <cassert>
using namespace std;
struct NVH
{
char name[200];
int points[200];
};
int main()
{
ifstream file;
file.open("input.txt");
ifstream pch;
pch.open("input1.txt");
ofstream pchout;
pchout.open("output.txt");
ofstream file1;
file1.open("output1.txt");
struct NVH n[200];
long int i,j,c,p,x,y;
char nam[300];
string line;
string search;
string replace;
file>>nam;
file>>c;
file>>nam;
file>>nam;
for(i=0;i<c;i++)
{
file>>n[i].name;
string str("$POINT ID = ");
My output is output1.txt as
$POINT ID = 600001:Shri
$POINT ID = 600002:Shri
$POINT ID = 600003:Shri
$POINT ID = 600004:Proactive
$POINT ID = 600005:Proactive
$POINT ID = 600006:Proactive
I tried with your suggestion. I read input.txt separately.
I obtained the output.txt with my requirements.
But I could not use the same thing in File Management section to find and replace. My question is How can I find strigs in input1.txt which are same as that in output.txt and replace them at the same location with that string and Name required and create output1.txt with the required chnages. that means
to find $POINT ID = 600001 from input1.txt and replace it with
$POINT ID = 600001:shri which I want to take from output.txt.
Quote:
#include<iostream>
#include<string>
#include<fstream>
#include<iomanip>
#include<sstream>
using namespace std;
struct NVH
{
char name[200];
int points[200];
};
int main()
{
ifstream file;
file.open("input.txt");
ifstream pch;
pch.open("input1.txt");
char input2[300],name2[300];
ofstream pchout;
pchout.open("output.txt");
ofstream file1;
file1.open("output1.txt");
struct NVH n[200];
long int i,j,k,l,m,p,a,b,c,x,y,z,r,s,d,f;
float e;
long int g,h,t;
char nam[300],name[300],input[300],name1[300];
string line;
string replace;
string search;
$POINT ID = 600001
I went to college
I am a disco dancer
$POINT ID = 600001
I like coconut
$POINT ID = 600002
I don't go to college.
$POINT ID = 600002
It is tedious program
$POINT ID = 6000003
Shridhar Ram Rahim
$POINT ID = 6000003
Ram Krishna Shiva Brhma
$POINT ID = 6000004
Very Easy Program
$POINT ID = 6000004
Finish the program
$POINT ID = 6000005
Life is nice
$POINT ID = 6000005
I am swiming
$POINT ID = 6000006
Sorry this is the end of the program
$POINT ID = 6000006
Do you still have the extra digit (one more 0 in the middle) of 6000003- 6000006 that you don't have in 600001-600002?
If you fixed that, I think this "corrected" program is roughly what you were trying to do. I do not intend this code to be an example of good coding nor anything like the way I might code a similar project. I attempted to get a basically correct program only by rearranging lines of code that you had written.
After reading all of input.txt into data structures, it processes input1.txt one line at a time. For each line of input1.txt it looks at all the data that was read from input.txt and uses whatever part of that input.txt data applies to that line of input1.txt.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.