To make the post shorter, the new problem example is a few posts
below.
Thanks guys!
Printable View
To make the post shorter, the new problem example is a few posts
below.
Thanks guys!
Hmmmmmmm... 25 views and no replies... I guess this is going to
be difficult to implement! ;)
I'll try something tonight to see what I come up with.
Thanks.
You should more clearly define the problem. Level of detalization in examples suggests complexity of problem, but the part I understood is that you just need to count ".NAME" substrings. And there evidently is something more, but it's not clear what exactly.
I don't know exactly what your meaning is,
but I can definitely say that this is very easy.
When confused me in your problem is:
"Now I want to run the program, and make it ask the user
how many entries it has (he should input 4 since he took an entry out)".
~~why is it necessary here to ask the user to input something like 4?
as the entry is four here, even if the user input something else, e.g, 5,
the entries is still 4.
Quote:
Originally Posted by eagle1
Just one minute..
Something happened to my browser.
Hello guys.
Thanks for posting and thanks
for saying this can be done easily
(at least this keeps me trying).
Let me see if I can explain a little better.
I have a text file.
The program will ask the user for a text file to use.
The program will ask the user for a text file for output.
After this, the program should ask:
How many entries are in the text file:
And according to what the user inputs, the output
text file will have the updated numbers.
A visual example.
Let's say we have this below:
TEST1 TST.STRING.NUMBER "3"
TEST1 TST.STRING.NUMBER[1].NAME "TEXTO_TRES"
TEST1 TST.STRING.NUMBER[1].STRING2 "1"
TEST1 TST.STRING.NUMBER[1].STRING2[1].LINE "More TEXT"
TEST1 TST.STRING.NUMBER[2].NAME "TEXTO_CUATRO"
TEST1 TST.STRING.NUMBER[2].STRING2 "1"
TEST1 TST.STRING.NUMBER[2].STRING2[1].LINE "EVEN more text"
TEST1 TST.STRING.NUMBER[3].NAME "TEXTO_SIETE"
TEST1 TST.STRING.NUMBER[3].STRING2 "2"
TEST1 TST.STRING.NUMBER[3].STRING2[1].LINE "Two Lines"
TEST1 TST.STRING.NUMBER[3].STRING2[2].LINE "of text."
The user MANUALLY alters this text above to be the following:
TEST1 TST.STRING.NUMBER "3"
TEST1 TST.STRING.NUMBER[1].NAME "TEXTO_TRES"
TEST1 TST.STRING.NUMBER[1].STRING2 "1"
TEST1 TST.STRING.NUMBER[1].STRING2[1].LINE "More TEXT"
TEST1 TST.STRING.NUMBER[3].NAME "TEXTO_SIETE"
TEST1 TST.STRING.NUMBER[3].STRING2 "2"
TEST1 TST.STRING.NUMBER[3].STRING2[1].LINE "Two Lines"
TEST1 TST.STRING.NUMBER[3].STRING2[2].LINE "of text."
He should now run the program and this should be what comes up:
Program: How many entries are in the file:
User: 2
Program Output:
TEST1 TST.STRING.NUMBER "2"
TEST1 TST.STRING.NUMBER[1].NAME "TEXTO_TRES"
TEST1 TST.STRING.NUMBER[1].STRING2 "1"
TEST1 TST.STRING.NUMBER[1].STRING2[1].LINE "More TEXT"
TEST1 TST.STRING.NUMBER[2].NAME "TEXTO_SIETE"
TEST1 TST.STRING.NUMBER[2].STRING2 "2"
TEST1 TST.STRING.NUMBER[2].STRING2[1].LINE "Two Lines"
TEST1 TST.STRING.NUMBER[2].STRING2[2].LINE "of text."
It can happen the other way around.
Let's say that the file originaly has this:
TEST1 TST.STRING.NUMBER "2"
TEST1 TST.STRING.NUMBER[1].NAME "TEXTO_TRES"
TEST1 TST.STRING.NUMBER[1].STRING2 "1"
TEST1 TST.STRING.NUMBER[1].STRING2[1].LINE "More TEXT"
TEST1 TST.STRING.NUMBER[2].NAME "TEXTO_SIETE"
TEST1 TST.STRING.NUMBER[2].STRING2 "2"
TEST1 TST.STRING.NUMBER[2].STRING2[1].LINE "Two Lines"
TEST1 TST.STRING.NUMBER[2].STRING2[2].LINE "of text."
and I manually alter it to have the following:
TEST1 TST.STRING.NUMBER "2"
TEST1 TST.STRING.NUMBER[1].NAME "TEXTO_TRES"
TEST1 TST.STRING.NUMBER[1].STRING2 "1"
TEST1 TST.STRING.NUMBER[1].STRING2[1].LINE "More TEXT"
TEST1 TST.STRING.NUMBER[2].NAME "TEXTO_SIETE"
TEST1 TST.STRING.NUMBER[2].STRING2 "2"
TEST1 TST.STRING.NUMBER[2].STRING2[1].LINE "Two Lines"
TEST1 TST.STRING.NUMBER[2].STRING2[2].LINE "of text."
TEST1 TST.STRING.NUMBER[4].NAME "TEXTO_SEIS"
TEST1 TST.STRING.NUMBER[4].STRING2 "1"
TEST1 TST.STRING.NUMBER[4].STRING2[1].LINE "Yep, keep going"
TEST1 TST.STRING.NUMBER[5].NAME "TEXTO_SIETE"
TEST1 TST.STRING.NUMBER[5].STRING2 "2"
TEST1 TST.STRING.NUMBER[5].STRING2[1].LINE "Two Lines"
TEST1 TST.STRING.NUMBER[5].STRING2[2].LINE "of text."
Then the user can run the program and the following should result.
Program: How many entries are in the file:
User: 4
Program Output:
TEST1 TST.STRING.NUMBER "4"
TEST1 TST.STRING.NUMBER[1].NAME "TEXTO_TRES"
TEST1 TST.STRING.NUMBER[1].STRING2 "1"
TEST1 TST.STRING.NUMBER[1].STRING2[1].LINE "More TEXT"
TEST1 TST.STRING.NUMBER[2].NAME "TEXTO_SIETE"
TEST1 TST.STRING.NUMBER[2].STRING2 "2"
TEST1 TST.STRING.NUMBER[2].STRING2[1].LINE "Two Lines"
TEST1 TST.STRING.NUMBER[2].STRING2[2].LINE "of text."
TEST1 TST.STRING.NUMBER[3].NAME "TEXTO_SEIS"
TEST1 TST.STRING.NUMBER[3].STRING2 "1"
TEST1 TST.STRING.NUMBER[3].STRING2[1].LINE "Yep, keep going"
TEST1 TST.STRING.NUMBER[4].NAME "TEXTO_SIETE"
TEST1 TST.STRING.NUMBER[4].STRING2 "2"
TEST1 TST.STRING.NUMBER[4].STRING2[1].LINE "Two Lines"
TEST1 TST.STRING.NUMBER[4].STRING2[2].LINE "of text."
So as you can see is "Basically a replace" code.
If the line has a .NAME in it, it would mean that the number
should increase.
But it's proving to be difficult for me to implement.
I was using something called istreamstring to separate each
line with whatever type I wanted and making what is in [] (i.e. the number)
an INT parameter.
But I don't know if I'm over-thinking this!
Any help, or snippets welcome.
Thanks Again.
P.S. The format of the text file is just like you see above with the periods and all of that stuff! :p
I still wonder why you ask the user to input the count of entries.
because I think the count can be obviously figured out from the file.
If there are only 3 entries,
What do you want to do if the user input 10?
Oh ok.
Now I get what your saying.
The thing is that this line:
TEST1 TST.STRING.NUMBER "3"
needs to be updated by the user.
So even if there 3 entries and the user wants this line to display
10, then it will have to display 10:
TEST1 TST.STRING.NUMBER "10"
Hey guys.
This is the partial code I have right now:
What this can do is that if I have the following from the text file I will read:Code:#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
string infile, outfile, line;
int n, lastn, index, pos1, pos2, Entries;
cout << "Input File : ";
cin >> infile;
cout << "Output File : ";
cin >> outfile;
cout << "# of Entries : ";
cin >> Entries;
cout << infile << endl;
cout << outfile << endl;
cout << Entries << endl;
ifstream fin(infile.c_str());
ofstream fout(outfile.c_str());
n = 0;
getline(fin, line);
while (! fin.eof())
{
if (n == 0)
{
pos1 = line.find('"');
fout << line.substr(0, pos1 + 1);
fout << Entries;
fout << line.substr(pos1 + 2);
fout << endl;
lastn = ++n;
index = 1;
}
else
{
pos1 = line.find('[');
pos2 = line.find(']');
istringstream iss;
iss.str(line.substr(pos1 + 1, pos2 - pos1 - 1));
iss >> n;
if (n != lastn)
{
lastn = n;
index++;
}
fout << line.substr(0, pos1 + 1);
fout << index;
fout << line.substr(pos2);
fout << endl;
}
getline(fin, line);
}
}
TEST1 TST.STRING.NUMBER "2"
TEST1 TST.STRING.NUMBER[1].NAME "TEXTO_TRES"
TEST1 TST.STRING.NUMBER[1].STRING2 "1"
TEST1 TST.STRING.NUMBER[1].STRING2[1].LINE "More TEXT"
TEST1 TST.STRING.NUMBER[2].NAME "TEXTO_SIETE"
TEST1 TST.STRING.NUMBER[2].STRING2 "2"
TEST1 TST.STRING.NUMBER[2].STRING2[1].LINE "Two Lines"
TEST1 TST.STRING.NUMBER[2].STRING2[2].LINE "of text."
TEST1 TST.STRING.NUMBER[4].NAME "TEXTO_SEIS"
TEST1 TST.STRING.NUMBER[4].STRING2 "1"
TEST1 TST.STRING.NUMBER[4].STRING2[1].LINE "Yep, keep going"
TEST1 TST.STRING.NUMBER[5].NAME "TEXTO_SIETE"
TEST1 TST.STRING.NUMBER[5].STRING2 "2"
TEST1 TST.STRING.NUMBER[5].STRING2[1].LINE "Two Lines"
TEST1 TST.STRING.NUMBER[5].STRING2[2].LINE "of text."
the text file output will have:
TEST1 TST.STRING.NUMBER "4"
TEST1 TST.STRING.NUMBER[1].NAME "TEXTO_TRES"
TEST1 TST.STRING.NUMBER[1].STRING2 "1"
TEST1 TST.STRING.NUMBER[1].STRING2[1].LINE "More TEXT"
TEST1 TST.STRING.NUMBER[2].NAME "TEXTO_SIETE"
TEST1 TST.STRING.NUMBER[2].STRING2 "2"
TEST1 TST.STRING.NUMBER[2].STRING2[1].LINE "Two Lines"
TEST1 TST.STRING.NUMBER[2].STRING2[2].LINE "of text."
TEST1 TST.STRING.NUMBER[3].NAME "TEXTO_SEIS"
TEST1 TST.STRING.NUMBER[3].STRING2 "1"
TEST1 TST.STRING.NUMBER[3].STRING2[1].LINE "Yep, keep going"
TEST1 TST.STRING.NUMBER[4].NAME "TEXTO_SIETE"
TEST1 TST.STRING.NUMBER[4].STRING2 "2"
TEST1 TST.STRING.NUMBER[4].STRING2[1].LINE "Two Lines"
TEST1 TST.STRING.NUMBER[4].STRING2[2].LINE "of text."
As you can see it's working partially because the index is actually
changing the number inside the brackets ... BUT!!!!!!
If I add another entry to the file, lets say:
TEST1 TST.STRING.NUMBER "4"
TEST1 TST.STRING.NUMBER[1].NAME "TEXTO_TRES"
TEST1 TST.STRING.NUMBER[1].STRING2 "1"
TEST1 TST.STRING.NUMBER[1].STRING2[1].LINE "More TEXT"
TEST1 TST.STRING.NUMBER[2].NAME "TEXTO_SIETE"
TEST1 TST.STRING.NUMBER[2].STRING2 "2"
TEST1 TST.STRING.NUMBER[2].STRING2[1].LINE "Two Lines"
TEST1 TST.STRING.NUMBER[2].STRING2[2].LINE "of text."
TEST1 TST.STRING.NUMBER[3].NAME "TEXTO_SEIS"
TEST1 TST.STRING.NUMBER[3].STRING2 "1"
TEST1 TST.STRING.NUMBER[3].STRING2[1].LINE "Yep, keep going"
TEST1 TST.STRING.NUMBER[3].NAME "TEXTO_SEIS"
TEST1 TST.STRING.NUMBER[3].STRING2 "1"
TEST1 TST.STRING.NUMBER[3].STRING2[1].LINE "Yep, keep going"
TEST1 TST.STRING.NUMBER[3].NAME "TEXTO_SEIS"
TEST1 TST.STRING.NUMBER[3].STRING2 "1"
TEST1 TST.STRING.NUMBER[3].STRING2[1].LINE "Yep, keep going"
TEST1 TST.STRING.NUMBER[4].NAME "TEXTO_SIETE"
TEST1 TST.STRING.NUMBER[4].STRING2 "2"
TEST1 TST.STRING.NUMBER[4].STRING2[1].LINE "Two Lines"
TEST1 TST.STRING.NUMBER[4].STRING2[2].LINE "of text."
as you can see, I repeated the 3rd entry 2 times.
If I run the program, the output wont be correctly BECAUSE I still have not implemented
that the program should increment the number inside the brackets if a .NAME tag is found.
Any solutions?