How to combine/sort text from an email and a .txt doc

Hello.

For part of my job, I receive emails with rows of numbers (PM's) that look like this:



35287385

35287387

35287389



Usually there's a lot more than just 3

I have a text document with templates that looks like this:



1)PM#
______________________

SAP ID
___________________________

LAT/LONG
________________________________

ADDRESS:
_____________________________________



2)PM#
______________________

SAP ID
___________________________

LAT/LONG
________________________________

ADDRESS:
_____________________________________



3)PM#
______________________

SAP ID
___________________________

LAT/LONG
________________________________

ADDRESS:
_____________________________________


How can I make those numbers (PM#s) fill like this:


1)PM#
35287385

SAP ID
___________________________

LAT/LONG
________________________________

ADDRESS:
_____________________________________



2)PM#
35287387

SAP ID
___________________________

LAT/LONG
________________________________

ADDRESS:
_____________________________________



3)PM#
35287389

SAP ID
___________________________

LAT/LONG
________________________________

ADDRESS:
_____________________________________



I want the order to be top to bottom just like they show on the email.
(the numerical value is irrelevant)

Our computers aren't allowed programming software so I can only use an online compiler, which is allowed.

I took an intro C++ course years back but don't really remember anything.
Been looking over my old notes and they don't help much.

It's no big deal but I'd rather not copy/paste 10 or 20 times for a set of orders.

What's the code that can do this?
Thanks
What you described is basically mail merge.
https://support.microsoft.com/en-us/topic/how-to-use-the-mail-merge-feature-in-word-to-create-and-to-print-form-letters-that-use-the-data-from-an-excel-worksheet-d8709e29-c106-2348-7e38-13eecc338679

Maybe you already have office installed (or libreoffice), in which case you have what you need.
Ok, so mailings is a no go. Long story short: you can't replace multiple text instances in word with text from multiple cells in excel in one document. Also, it takes too long to open and start messing with mailings in word every time a set of PMs is assigned to me as opposed to an online C++ compiler.

Anyway, here's the good news:

#include <iostream>
using namespace std;
int main()
{
string x, y, z;
cin >> x >> y >> z;
string a[3] = {x, y, z};
string output[] =
{
"",
"",
"1)PM#",
x,
"",
"SAP ID ",
"___________________________",
"",
"LAT/LONG ",
"________________________________",
"",
"ADDRESS: ",
"_____________________________________",
"",
"",
"",
"2)PM# ",
y,
"",
"SAP ID ",
"___________________________",
"",
"LAT/LONG ",
"________________________________",
"",
"ADDRESS: ",
"_____________________________________",
"",
"",
"",
"3)PM# ",
z,
"",
"SAP ID ",
"___________________________",
"",
"LAT/LONG ",
"________________________________",
"",
"ADDRESS: ",
"_____________________________________" ,
"END"};

int i = 0;
while (output[i] != "END")
{
cout << output[i++] << endl;
}

return 0;
}

Here's the last issue, though:
the user will select a set of PMs (usually 20, sometimes 10) that looks like this:

35287385
35287387
35287389
......
...

(there's actually no spaces between them unlike in my original post)
then copy them directly from the email
then r-click paste into the black output screen, not individually, but the whole set of 20 or 10 PMs at once.

I need the program to assign however many PMs have been pasted into individual elements in the array.
That way the user doesn't have to tell the program how many PMs they're inputting each time.
I am not sure if I miss sth. but your code looks awfully complicated.
I think you need only one template and one loop to process all the numbers.
Make sure you compile with -std=c++11 or higher
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <string>

const std::string txt = R"(
1)PM#",

"SAP ID ",
"___________________________",
"",
"LAT/LONG ",
"________________________________",
"",
"ADDRESS: ",
"_____________________________________",
)";

int main()
{
	
	std::string line;
	std::string output(txt); // make copy of template
	while(std::getline(std::cin, line)) // read numbers, one at a time
	{
		// TODO: insert line after PM# in output
		// print output
	}
}
 
Last edited on
I can't make that code work at all, sorry.

The line after 1)PM# is inside quotes so it's just text.
I don't understand how any number will go there when the user pastes it in.
Try to run this code and see how it works:
There seems to be a problem with the windows console when you paste multiple lines of text - at least on my Windows 8.1.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <string>

const std::string txt = R"(PM#,
SAP ID,
___________________________,
LAT/LONG,
________________________________,
ADDRESS: ,
_____________________________________,
)";

int main()
{

	std::string line;
	int number = 1;

	while (std::getline(std::cin, line)) // read numbers, one at a time
	{
		std::string output(txt); // make copy of template
		auto input_pos = output.find("PM#");
		if (input_pos != std::string::npos) 
                {
			output.insert(input_pos + 4, std::string("\n") + line + "\n");
			std::cout << number++ << ')';
			std::cout << output << "\n";
		}
	}
}
Forget the previous post.
This code works better.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <string>
#include <sstream>

const std::string txt = R"(PM#,
SAP ID
___________________________
LAT/LONG
________________________________
ADDRESS:
_____________________________________
)";

void process_line(const std::string& line, int num, std::ostream& os)
{
	std::string output(txt); // make copy of template
	auto input_pos = output.find("PM#");
	if (input_pos != std::string::npos) 
	{
		output.insert(input_pos + 4, std::string("\n") + line + "\n");
		os << num << ')';
		os << output << "\n";
	}
}

int main()
{
	std::string line;
	int number = 1;
	std::ostringstream oss;

	while (std::getline(std::cin, line)) // read numbers, one at a time
	{
		process_line(line, number++, oss);
	}
	std::cout << oss.str() << "\n";
}

Output

35287385
35287387
35287389
^Z
1)PM#,
35287385

SAP ID
___________________________
LAT/LONG
________________________________
ADDRESS:
_____________________________________

2)PM#,
35287387

SAP ID
___________________________
LAT/LONG
________________________________
ADDRESS:
_____________________________________

3)PM#,
35287389

SAP ID
___________________________
LAT/LONG
________________________________
ADDRESS:
_____________________________________
Last edited on
Doesn't do anything, sorry
You need to do better than "doesn't work".
What doesn't work?
- it doesn't compile - if not, post your error messages
- it doesn't run - show us what you tried
- the output is wrong - again, how is that different to your expectation.

It works for me.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
$ cat input.txt 
35287385
35287387
35287389

$ ./a.out < input.txt 
1)PM#,
35287385

SAP ID
___________________________
LAT/LONG
________________________________
ADDRESS:
_____________________________________

2)PM#,
35287387

SAP ID
___________________________
LAT/LONG
________________________________
ADDRESS:
_____________________________________

3)PM#,
35287389

SAP ID
___________________________
LAT/LONG
________________________________
ADDRESS:
_____________________________________


No error codes, it just didn't do anything when I ran it.
The program didn't react to any inputs
Ran it where?
You mentioned "online compiler", which one?

Seriously, up your game with the information you provide.
We're not going to play 20 questions every time.
Here's my best progress so far

#include <iostream>

//#include <string>

using namespace std;



int

main ()

{

int counter = 1;

int n, x;

cout << "how many PMs?" << endl;

cin >> n;

cout << "paste them now" << endl;

while (counter < n)

{

cin >> x;

cout << counter << ")PM#" <<endl<<

x <<endl<<

""<<endl;

counter = counter + 1;

}



return 0;

}


The problem, though, is the output looks shuffled up most of the time.
Like this:

how many PMs?
3
paste them now
35287385
352871)PM#
335287385
87
3
5282)PM#
35287387

7389


I'll try your code, salem c
Topic archived. No new replies allowed.