Out of range at memory location problem

when I use scanf and printf instead of using cin and cout, I have an error like "out of range at memory location" on

outputLetterList.push_back(letterList.at(alphabetLocation));

if I use cin and cout, the code works well. But I must use scanf and printf because of the rule that testprovider gave.

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <stdio.h>
#include <vector>
#include <iostream>

using namespace std;

int main() {

	int numberOfBooks;
	int numberOfChecking;


	scanf_s("%d", &numberOfBooks);
	scanf_s("%d", &numberOfChecking);

	char alphabet;
	vector<char> bookList;

	for (unsigned int i = 0; i < numberOfBooks; i++) {
		scanf_s("%c", &alphabet);
		bookList.push_back(alphabet);
	}

	int leftChecking;
	int rightChecking;
	int counting = 0;
	vector<int> countingList;
	int alphabetLocation = 0;

	vector <char> letterList;
	vector <char> outputLetterList;

	for (unsigned int a = 0; a < numberOfChecking; a++) {

		scanf_s("%d", &leftChecking);
		scanf_s("%d", &rightChecking);


		for (int b = (int)leftChecking - 1; b < rightChecking; b++) {
			if (b == leftChecking - 1) {
				counting += 1;
			}
			else {
				if ((bookList.at(b) == bookList.at(b - 1))) {
					counting += 1;
					if (b == rightChecking - 1) {
						counting += 1;
						countingList.push_back(counting);
						letterList.push_back(bookList.at(b));
					}

				}
				else {
					countingList.push_back(counting);
					letterList.push_back(bookList.at(b - 1));
					counting = 1;
					if (b == rightChecking - 1) {

						countingList.push_back(counting);;
						letterList.push_back(bookList.at(b));

					}
				}
			}
		}

		int currMax = 0;
		for (unsigned int c = 0; c < countingList.size(); c++) {
			if (c == 0) {
				currMax = countingList.at(c);
			}
			else if (currMax < countingList.at(c)) {
				currMax = countingList.at(c);
			}
		}


		for (unsigned int d = 0; d < countingList.size(); d++) {
			if (countingList.at(d) == currMax) {
				alphabetLocation = d;
				break;
			}

		}


		outputLetterList.push_back(letterList.at(alphabetLocation));


		counting = 0;
		countingList.clear();
		letterList.clear();

	}


	for (unsigned int i = 0; i < outputLetterList.size(); i++) {
		printf("%c", outputLetterList.at(i));
		printf("\n");
	}



	return 0;
}
It helps to read the manual page once in a while.
https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/scanf-s-scanf-s-l-wscanf-s-wscanf-s-l?view=vs-2019
char c;
scanf_s("%c", &c, 1);


scanf_s is not a 1:1 drop-in replacement for scanf.
You only get the "safety" implied by "_s" if you actually know how to use the functions to begin with.

But the same is true of the original scanf anyway.

> But I must use scanf and printf because of the rule that testprovider gave.
Are they checking code, or just results?
Last edited on
Hello ekdlsks7437,

I tired working with your program. I was so distracted trying to figure out what to enter I lost track of what I was doing.

It is so frustrating to see a blank screen and a flashing cursor and have no idea what you are waiting on. And follow that with having no idea what to enter.

The following code should give you an idea of what you should and can do:
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
int main()
{
	int numberOfBooks{};
	int numberOfChecking{};
	char junk{};
	char alphabet{};
	vector<char> bookList; // <--- Empty to start with. Does not need initialized.

	printf("\n How many books: ");
	scanf_s("%d", &numberOfBooks);

	printf(" Second prompt?: ");
	scanf_s("%d", &numberOfChecking);

	scanf_s("%c", &junk, 1); // <--- Eats the "\n".

	printf("\n");

	for (unsigned int i = 0; i < numberOfBooks; i++)
	{
		printf("   %d Enter a letter (choices if any): ", i + 1);
		scanf_s("%c", &alphabet,1);
		scanf_s("%c", &junk, 1); // <--- Eats the "\n".

		bookList.push_back(alphabet);

		std::cout << std::endl; // <--- Used as a break point for testing.

	}

Back in the day when I learned C we were taught to keep the variables all together and that is what I am use to. You can define your variables anywhere that works for you.

I have also come to learn that it is a good idea to initialize the variable when defined. If for no other reason than the peace of mind of knowing that they do not contain a garbage value.

The empty {}s are from ++11 standards on. If your compiler complains about them just set the "int"s "= 0" and the "char"s "='\0'". Another school of thought is that these variables receive a value so early in the program that you can get by without initializing these variables. std::string and std::vector are empty containers when defined and do not need initialized unless you need something there to start with.

The "junk" variable and as the comment on line 15 says it removes the stray "\n" after you input of "numberOfChecking". Also used in the for loop so that the vector contains the correct information and not storing "\n"s that you do not want.

Line 17 is used to add a blank line to the output. To me it makes the output on the screen easier to read. Also I tend to start the output one line off the top edge of the screen and one column in from the left edge. This makes the output easier for me to read, but you are free to do what you like. This is only a suggestion of what you could do.

The above code also shows how blank lines can make the code easier to read. This is more for people reading your code as the compiler does not care about white space or blank lines. It just ignores or skips over them.

I leave the rest of the "scanf_s"s for you to figure out a prompt for and anything else that may need fixed based on what I have showed you.

The last thing I would ask for is an idea ow what to enter for each "scanf_s" in the program. Having some known input and what the output should look like really helps.

Hope that helps,

Andy
Topic archived. No new replies allowed.