Good day to all.

There is an initial array in which the user will enter a text, there is an array under the letter to be inserted into the word, and there is a result array in which there will be a word and the inserted letter in the word.

As the teacher told me that Word can be declared as a variable, not an array, but when the user enters a letter, the application simply crashes with the error window (it costs windows xp).

The most basic problem is that during transplantation the first letter of the source text disappears! In fact, it is important that after the user chooses a letter, the first letter in the source text disappears, he checked it himself.

The main thing for me is to understand why the letter disappeared. Thanks in advance for the answers and attention!

Below is a commented out loop, i.e. entering letters before position and after.


Do not pay attention to copying in a loop with 1, I checked that the symbol disappears, I know that you need to enter with 0. I solved the task myself, thank you all!

#include <conio.h> #include <string.h> #include <iostream> using namespace std; main(){ char text[10]; //наш исходный массив, предполагаемый текст - helloworld char word[1]; //(можно ли объявлять его не как массив?) наша буква char test[11]; //наш результирующий массив int position = 0, i; //позиция и счетчик printf("Enter text: "); gets(text); //вводим текст printf("%s", text); //проверяем, правильно ли введен (+правильно) printf("\nEnter word: "); fgets(word, 2, stdin); //вводим букву printf("%c", word[0]); //проверяем букву cout << "\nEnter position: "; cin >> position; cout << "\nPosition: " << position; position --; //мы считаем позицию с 0, пользователь с 1 for(i = 0; i < 11; i++){ //перенос букв из исходного массива в результирующий, переносит правильно, а первая буква пропадает test[i] = text[i]; printf("%c", test[i]); //отлавливаем, попадают ли буквы в результирующий массив getch(); //if(i == position) не работает //попытка вставить заданную букву на позицию результирующего массива, а после неё продолжить вставлять буквы из исходного массива //test[i] = 0; } /*for(i = 1; i < 6; i++){ printf("%c", test[i]); getch(); }*/ /*for(i = 0; i > position; i++){ test[i] = text[i]; printf("%c", test[i]); getch(); }*/ getch(); } 

    1 answer 1

    First, do not use gets , never. Imagine that a user has entered 100 characters - what do you think will happen? (This is an important question, make sure you understand this exactly.) And yes, if the function is available, it does not mean that it is a good idea to use it.

    Secondly, arrays in C (well, in C ++) are numbered starting from zero. Therefore, your copy cycle is incorrect.

    Thirdly, yes, it is quite possible to select a variable of type char for one character. At the same time, entering it with the help of fgets (and even gets ) will be wrong, since these functions append the lines \0 to the tail. Your code is now also incorrect, since it selects only one character, and thus does not take into account the final \0 (this is distracting from the fact that the user of the program can enter as many characters as possible before).

    Fix it and look further.


    Another note: you write on a wild mixture of C and C ++. In C ++, arrays of characters are practically not used as strings; instead, std::string . Now it probably does not matter, but it will become important later. If you are told that C is a subset of C ++, and the C code is the correct C ++ code, you can immediately note inside that this person does not understand both C and C ++ well. C is not a subset of C ++, but its generic trauma: for the sake of a purely marketing idea of ​​compatibility with C in C ++, at the syntax level, many things in C ++ are implemented terribly.


    By the way, for string operations in C there are quite a few built-in functions like strcpy and strcat . But it’s better to work with strings manually first, these functions do exactly what you are doing.

    • one
      By the way, purely academic interest: C code is not C ++ code, for example, because C does not require headers if you use some basic functions (a la printf), and C ++ requires. Respectively, that on C, not necessarily will gather on With ++. But are there any other differences? - Arkady
    • I did not understand why my first symbol disappears. Actually, the teacher got to me a grief ... All the same also did not understand, through what function it will be logical to enter one character? Since in C ++ many things are implemented horribly, then in what language should C or C ++ be written? - DrummerIF
    • @smallFish: there are small differences. For example, pointer conversion: C , C ++ . Also, there is restrict in C99, but not in C ++. - VladD
    • @DrummerIF: For starters, take and read a book on C ++ (google search on the site, questions about the books were asked repeatedly). Regarding the missing symbol, did you fix the copy cycle? Make it from 0. For input, fgets (but not gets ) and scanf can come in handy. And also getline , if your platform supports it. It seems to me that it is worth knowing both languages, C and C ++, and understanding their “ideology”. The question of which languages ​​should be taught is large and complex (and it seems to have been discussed here too). - VladD