I need some explaining.

Hey guys. Good mornin.

I usually don't get stuck on these type of exercises but I didn't really understand how to go forward and backwards with numbers.

3 3 ( The first 3 is how many lines in a for(), the second one is the current channel)
5
7 (5,7,2 are the channels It needs to get trough.
2

Answers:
9 (How many channels it went trough)
DDDDKKKKK (D - up, K - down)

(code)
using namespace std;
//---------------------------------------
void howmany ( int n, int bkan, int &a );
int main ()
{
int n, bkan, a, k;

ifstream df ("TV.txt");
df >> n >> bkan; // 3 3
for ( int i = 0; i < n; i++)
{
df >> a; // 5 7 2
}

cout << k; // how many times
return 0;
}
void howmany ( int n, int bkan, int &a )
{
int k = 0;
for ( int i = 0; i < n; i++)
{
if ( bkan < a ) cout << "D" << endl;
else ( bkan <= a) cout << "K" << endl;
k++;
}
}
}
(/code)

If I have 3 then, it checks if its lower than 5 and it prints out D. But how can I make it that it would remember the letters and move on to the next number?
Any help would be appreciated.
Last edited on
"To remember" means to store data in variable(s). Since data is characters, it would make sense to store the sequence in std::string.
We haven't gotten learning strings yet but it asks us to do it with a void() function.
So I don't really know how to do strings but I know what they do in some way.
Last edited on
The string would be relatively easy to use: http://www.cplusplus.com/reference/string/string/operator+=/

What containers have you learned already?
Possibly something like (without using a function or std::string):

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
#include <iostream>
#include <iomanip>
#include <sstream>

int main() {
	size_t N {};
	int init {};
	std::ostringstream oss;

	std::cin >> N >> init;

	for (size_t i {}; i < N; ++i) {
		int n {};

		std::cin >> n;

		if (n > init)
			oss << std::setw(n - init) << std::setfill('D') << 'D';
		else if (init > n)
			oss << std::setw(init - n) << std::setfill('K') << 'K';

		init = n;
	}

	std::cout << oss.str().size() << '\n' << oss.str() << '\n';
}



3 3
5 7 2
9
DDDDKKKKK


See http://www.cplusplus.com/reference/sstream/stringstream/
Last edited on
I don't really know how to do strings

https://www.learncpp.com/cpp-tutorial/an-introduction-to-stdstring/

There is also the C string, a char array.
https://www.learncpp.com/cpp-tutorial/c-style-strings/

And a reminder:
Please learn to use code tags, they make reading and commenting on source code MUCH easier.

How to use code tags: http://www.cplusplus.com/articles/jEywvCM9/

There are other tags available.

How to use tags: http://www.cplusplus.com/articles/z13hAqkS/

HINT: you can edit your post and add code tags.

Some formatting & indentation would not hurt either
A more in-depth look at std::string, starting at the beginning of Lesson 22 over at Learn C++:

https://www.learncpp.com/cpp-tutorial/stdstring-and-stdwstring/
Topic archived. No new replies allowed.