Here is the task:

Create a program that must receive a sequence of digits at the input, after which the program displays the digit whose sequence number the user entered.

And here is my code:

#include <iostream> #include <cstring> using namespace std; int main() { setlocale(LC_ALL, "Russian"); char string[100]; cout << "Введите последовательность цифр: "; cin >> string; int k; cout << "\nВведите порядковый номер цифры: "; cin >> k; if ((k - 1) < 0 || k > strlen(string)) cout << "\nНекорректный ввод порядкового номера" << endl << endl; else cout << "\nk-я цифра последовательности: " << string[k] << endl; return 0; } 

However, it does not work. Where is the mistake?

  • Try the debugger ... - Vladimir Martyanov
  • This code works . But not in all cases and, most likely, not quite as you expect. - D-side
  • Would you even replace n in the lines of output n with \n - Harry
  • you enter in an array of characters so cin >> string; your string is not std :: string ... - AR Hovsepyan
  • one
    It seems to work ... Although, perhaps, it is worth replacing with srting. - user281225 4:34 pm

4 answers 4

Perhaps I will write another answer. Since this question is marked as related to C ++, it would be better to replace the char array with the string.

 #include <iostream> #include <cstring> using namespace std; int main() { setlocale(LC_ALL, "Russian"); string string; cout << "Введите последовательность цифр: "; getline (cin, string); int k; cout << "nВведите порядковый номер цифры: "; cin >> k; if ((k - 1) < 0 || k > string.length()) cout << "nНекорректный ввод порядкового номера" << endl << endl; else cout << "nk-я цифра последовательности: " << string[k - 1] << endl; return 0; } 

Important! The result of the code execution will hardly change, only the maximum length of the sequence will increase! If you need to put it, initialize the string variable properly.

    Error in line:

     cout << "\nk-я цифра последовательности: " << string[k] << endl; 

    But you must:

     cout << "\nk-я цифра последовательности: " << string[k - 1] << endl; 

    The fact is that in arrays index counting starts from zero. Try, should earn. Although it is strange that the error is in this place.

    • I do not see anything strange ... - user281225
    • Daniil Chizhevsky, yes this is a mistake, but the code is not working because of this, because of this it will simply give the wrong answer - AR Hovsepyan
    • As wrong wrote the vehicle, the code works. It gives the wrong result, that's all. Although an interesting effect is observed with the char array, I will not remove it in this answer. - Daniel Chizhevsky
    • @ Daniil Chizhevsky, if you use the author’s code, it works for you, because you enter without separators, but try to insert a space after the first character ... - AR Hovsepyan
    • Then it should be different ... - Daniel Chizhevsky

    change input method

     for (size_t i = 0; i < strlen(string); ++i) std::cin >> string[i]; 

    I think you entered numbers and where you entered a space, because the code did not work, and it is better to enter in std :: string

    • 2
      1. i < strlen(string) - no need to do so - calling strlen at each iteration slows down the program significantly, it is necessary to save the length to a variable. 2. Something about entering a string of characters, in which you are measuring the length, is some kind of nonsense ... 3. How should this even help? - Qwertiy

    Error here:

     cout << "nk-я цифра последовательности: " << string[k] << endl; 

    Change the index to [k - 1] .