[try Beta version]
Not logged in

 
How to get program to distribute input by alternating between two, multi-line string inputs

Nov 12, 2024 at 8:08am
Hello



I need to get this output:


1)
type a:
1a
type b:
1b

2)
type a:
2a
type b:
2b

3)
type a:
3a
type b:
3b

4)
type a:
4a
type b:
4b


from these 2 texts:


1a
2a
3a
4a

1b
2b
3b
4b


by copy-pasting each set (all 4 lines at a time) into the compiler.

So far I have:


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
#include <iostream>
#include <fstream>
using namespace std;
int main ()
{
	ofstream outfile;
	outfile.open("text.txt");
	int counter = 1;
	int quantity;
	string a;
	string b;
	cout << "how many type a's and type b's?" << endl;
	// for this example, just enter 4
	cin >> quantity;
	cout << "paste type a's and hit enter, then paste type b's and hit enter" << endl;
	while (counter <= quantity)
	{
		cin >> a >> b;
	//	cin.ignore();
		outfile << counter << ")" << endl << "type a:" << endl << a << endl 
		        << "type b:" << endl << b << endl << endl;
		counter = counter + 1;
	}
	return 0;
}



But that gives me this output:


1)
type a:
1a
type b:
2a

2)
type a:
3a
type b:
4a

3)
type a:
1b
type b:
2b

4)
type a:
3b
type b:
4b

Btw, I'm using onlinegdb.com
Nov 12, 2024 at 8:57am
Do one thing at a time. First read all the inputs, then write all the output.

You can use a container such as std::vector to store the inputs.
Last edited on Nov 12, 2024 at 8:58am
Nov 12, 2024 at 9:56pm
One line at a time, or one set of multi-line text at a time?
I need the user to paste sets of multi-line texts and it can't be one line at a time unfortunately.
Nov 13, 2024 at 9:24am
If input is:
1a
2a
3a
4a

1b
2b
3b
4b

do you notice any "patterns" on it? For example,
"four lines with text, one empty line, then four lines of text"?

Read lines of text and store them to one place until you get an empty line.
Don't store the empty, but continue reading lines of text and store then to another place.
Nov 13, 2024 at 11:41am
You actually need 3 loops:

1. Read the .a values and store them [in a vector]
e.g.
1
2
3
4
5
	for(int i = 0; i < quantity; ++i)
	{
		cin >> a;
		a_store.push_back(a);
	}

2. Read the .b values and store them [in a vector]
3. Write the combination of a and b
e.g.
1
2
3
4
5
	for(int i = 0; i < quantity; ++i)
	{
		outfile << i+1 << ")" << endl << "type a:" << endl << a_store[i] << endl 
		        << "type b:" << endl << b_store[i] << endl << endl;
	}

Nov 13, 2024 at 12:47pm
You can do it with just 1 loop to read the data. Consider:

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
#include <iostream>
#include <map>
#include <fstream>
#include <string>

int main() {
	std::ifstream ifs("type.txt");

	if (!ifs.is_open())
		return (std::cout << "Cannot open input file\n"), 1;

	std::map<int, std::string> data;

	for (std::string line; std::getline(ifs, line); )
		if (char* end {}; line.size() >= 2)
			if (const auto num { std::strtol(line.data(), &end, 10) }; *end)
				if (const auto ret { data.find(num) }; ret != data.end())
					ret->second.push_back(*end);
				else
					data.insert(std::make_pair(num, std::string(1, *end)));

	for (const auto& [n, vch] : data)
		for (std::cout << '\n' << n << ")\n"; const auto ch : vch)
			std::cout << "type " << ch << ":\n" << n << ch << '\n';
}


which given input file of:

1a
2a
3a
4a

1b
2b
3b
4b


gives the required output of:


1)
type a:
1a
type b:
1b

2)
type a:
2a
type b:
2b

3)
type a:
3a
type b:
3b

4)
type a:
4a
type b:
4b

Topic archived. No new replies allowed.