Click here to Skip to main content
15,400,381 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C++
#include <iostream>
#include <string>

int main() {
  std::string fullName;
  std::cout << "Type your full name: ";
  getline (std::cin, fullName);
  int n;
  std::cout<<"Enter which index(start from 0) word you want to change: ";
  std::cin>>n;
  std::cout<<"Enter what do you want to change in this index number "<<n<<":";
  std::cin >> fullName[n];
  std::cout << "Your name is: " << fullName;
  return 0;
}


What I have tried:

Answer:
Type your full name: Anika Tasnim
Enter which index(start from 0) word you want to change: 2
Enter what do you want to change in this index number 2:^[[3~
Your name is: Ana Tasnim


In this program, I want to omit characters from a string. So I take the index number from the user to which character to omit. But I mistakenly type DEL from the keyboard, after running the program I find indexes 2 and 3 characters were deleted. So my question is why does it happen? and What to do to delete index number 2?
Posted
Updated 22-Jun-22 13:44pm
v3
Comments
Richard MacCutchan 21-Jun-22 11:34am
   
Your code works correctly. Whatever you typed at the keyboard should be reflected in the displayed name before your code processes it.
Rick York 21-Jun-22 20:26pm
   
I would be using a debugger to solve this problem. It should show you exactly what is going on.

The problem is that you are using Unicode, which uses a variable number of bytes to store a character: some will be one byte, some will be 2, and so on. Alphabetic characters are generally one byte, but DEL is likely to be several.
So reading a string into a string at an index is ... going to be problematic.
Instead, read your replacement string into a new instance of std::string then use string::substr - C++ Reference[^] to extract both the leading and tailing data from your original input and then assemble a new string from teh three new pieces:
input:             ABCDE
Replace at index:      2
Replacement:           X
Break input
   S1:                AB
   S2:                DE
New string = S1 + Replacement + S2
           = ABXDE
   
Comments
Richard MacCutchan 21-Jun-22 11:30am
   
"problem is that you are using Unicode"
Er, std::cin, cout and string are all ASCII.
I believe this is the offending line.
C++
std::cin >> fullName[n];
The bitshift operator when used with std::cin expects a reference to a string and passing fullName[n] will copy the string you enter into the nth position and that copy will likely include the null character along with other character(s) you enter.

For debugging purposes I would display first character you enter as an integer and see if it is 127. It might be null instead, depending on the behavior of the bitshift operator with std::string. I don't know exactly because I rarely use cin and cout.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900