I
I
Ivan Antonov2016-02-09 21:01:43
C++ / C#
Ivan Antonov, 2016-02-09 21:01:43

The reason for the endless output of unrelated characters?

Just started learning C++
Compilation runs without errors, when executing an exe, an infinite number of unrelated characters go to the console with a Windows warning sound:

#include <iostream>
#include <cstdlib>

using namespace std;

string *addressSearch(int count)
{
  string name;
  string *address[count];
  
  for (int i = 0; i < count; i++) {
    cout << "Напишите точное название " << (i + 1) << " хар-ки: ";
    cin >> name;
    address[i] = &name; // TODO: написать функция поиска ячейки памяти по значению переменной name
  }
  
  return *address;
}

int main()
{
  int count;
  
  setlocale(0, "");
  cout << "Стадия 1. Поиск ячеек памяти с необходимыми хар-ми" << endl;
  cout << "Максимальное количество характеристик: "; 
  cin >> count;
  
  cout << "Для начала выполните первое обновление камня" << endl;
  system("pause");
  
  string *address = addressSearch(count);
  
  cout << "Первая: " << address[0] << endl;
  
    return 0;
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
res2001, 2016-02-09
@antonowano

There are many reasons:
1. In the addressSearch function in the loop, you read the user's input into name in the loop, then assign the address name to the array. Here the error is that you always have the same variable name and the entire array will be filled with the same value - the address name. On each iteration of the loop after the input, name will have its value, but in the next iteration this value will be overwritten by the next input. Thus, in address at the exit of the function, you will have in each element the address of name, which refers to the same object containing the last user input.
2. In the addressSearch function, the name and address variables are declared as local, after exiting the function, these variables no longer exist, and you return the address of a non-existing variable from the function. Therefore, the mess in the output.

G
GavriKos, 2016-02-09
@GavriKos

Incorrect work with pointers - cout cannot find the end of the line, and displays a bunch of garbage from memory. For some reason it seems to me that return *address; should be replaced with return address; but I may be wrong.

S
Stanislav Makarov, 2016-02-09
@Nipheris

You have complete hell in terms of working with memory. In general, it is not clear why this pandemonium with a pointer to a string and an array, it is not even clear what you want to return from the search function - a pointer to a string or to an array of string. Anyway, both are local variables, and trying to work with pointers to them after the function has completed is a gross error (more precisely, a mistake to try to return a pointer to a local variable from a function and think that it will work).
Specify what you want to do, because it is difficult to even understand the problem being solved (apparently, to find something somewhere?).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question