Incrementing 7 Contiguous Items in a List

I am working with Lego Mindstorms EV3 robots for my introductory computer science class. I have two arrays: one with 21 names, and another with 21 corresponding birthdays. I need to display 7 of these names and birthdays at a time on the screen, which I have done. I also need to increment these names and birthdays by 1 each time a button is pressed so that the screen changes from showing values 1 through 7 to values 2 through 8, and so on. I have managed to display values 2 through 8 and press another button to return back to 1 through 7, but I cannot go any further in either direction. Is there a way to generalize the code I have here so that the array increments every time I press the buttons? If anyone could help me out, I would really appreciate it! Thanks!

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
int = 1;
int t = 0;
int line = 1;
int count = 0;

string names[21]={"Gary Oldman", "Casey Affleck", "Leonardo DiCaprio", "Eddie Redmayne", "Matthew McConaughey", "Daniel Day-Lewis", "Jean Dujardin", "Forest Whitaker", "Jamie Foxx", "Sean Penn", "Denzel Washington", "Russell Crowe", "Jack Nicholson", "Tom Hanks", "Al Pacino", "Janet Gaynor", "Norma Shearer", "Helen Hayes", "Katharine Hepburn", "Claudette Colbert", "Bette Davis"};

long bdays[21]={580321, 750812, 741111, 820106, 690411, 570429, 720619, 610615, 671213, 600816, 541228, 640407, 370422, 400425, 061006, 841006, 020811, 001010, 071012, 030913, 080405};

task main ()
{
	for(l=1; l<14; l=l+2)
	{
		displayTextLine(l, "%i %i %s", line, bdays[t], names[t]);
		line++;
		t++;
	}
Loop:

if(getButtonPress(buttonUp) == 1)
{
	count=0;
	t=0 && t<25;
	line=1 && line <25;
	for(l=1; l<14; l=l+2)
		{
			displayTextLine(l, "%i %i %s", line+1+count, bdays[t+count], names[t+count]);
			line++;
			t++;
		}
			count=line-8;
			count++;
}
else if(getButtonPress(buttonDown) == 1)
{
	count=count-1;
	t=0 && t<25;
	line=1 && line <25;
	for(l=1; l<14; l=l+2)
		{
			displayTextLine(l, "%i %i %s", line-count, bdays[t-1-count], names[t-1-count]);
			line++;
			t++;
		}
			count=line-8;
			count++;
}

else
{
	waitForButtonPress();
}

goto Loop;	
}
}	
}
Last edited on
Something like this, perhaps:

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <string>
#include <iomanip>
#include <algorithm>

void display_text_line( int line_number, const std::string& name, long bday )
{
    // replace with printf if the EV3 implementation does not support C++ streams
    std::cout << std::setw(2) << line_number << ". " << std::setw(25) << name << "  "
              << std::setw(6) << std::setfill('0') << bday << std::setfill(' ') << '\n' ;
}

void display_lines( int start_line_number, const std::string name[], const long bday[],
                    int num_items, int lines_per_page = 7 )
{
    const int end_line_number = std::min( start_line_number+lines_per_page, num_items ) ;

    for( int i = std::max( start_line_number, 0 ) ; i < end_line_number  ; ++i )
        display_text_line( i+1, name[i], bday[i] ) ;
}

// simulated button press (for testing); replace these with the actual EV3 functions
#include <cctype>
static char button_pressed = 0 ;
void waitForButtonPress() { std::cin >> button_pressed ; button_pressed = std::toupper(button_pressed) ; }
bool getButtonPress( char c ) { return button_pressed == std::toupper(c) ; }
///////////////////////////////////////////////////////////////////////

int main()
{
    const int N_ITEMS = 21 ;
    const std::string names[N_ITEMS] = { "Gary Oldman", "Casey Affleck", "Leonardo DiCaprio",
        "Eddie Redmayne", "Matthew McConaughey", "Daniel Day-Lewis", "Jean Dujardin",
        "Forest Whitaker", "Jamie Foxx", "Sean Penn", "Denzel Washington", "Russell Crowe",
        "Jack Nicholson", "Tom Hanks", "Al Pacino", "Janet Gaynor", "Norma Shearer",
        "Helen Hayes", "Katharine Hepburn", "Claudette Colbert", "Bette Davis" };

    const long bdays[N_ITEMS] = { 580321, 750812, 741111, 820106, 690411, 570429, 720619,
        610615, 671213, 600816, 541228, 640407, 370422, 400425, 61006, 841006, 20811,
        1010, 71012, 30913, 80405 };

    const char BUTTONUP = 'U' ;
    const char BUTTONDOWN = 'D' ;

    int start_line_number = 0 ;
    bool quit = false ;

    while( !quit )
    {
        std::cout << "\n----------------------------------------------\n" ;
        display_lines( start_line_number, names, bdays, N_ITEMS ) ;

        std::cout << "\nenter 'U' for up, 'D' for down, anything else to quit: " ;
        waitForButtonPress() ;

        if( getButtonPress(BUTTONUP) ) --start_line_number ;
        else if( getButtonPress(BUTTONDOWN) ) ++start_line_number ;
        else quit = true ;
    }
}
Topic archived. No new replies allowed.