Want to change a char in a vector of chars

I don´t know what I´m doing wrong in the code. Need some help.

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
  #include <iostream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;

int main ()
{       
		std::cout << "Give a person a name: " << endl;

		//string inputString;

		string inputString;		
		cin >> inputString;	

	    std::string s(inputString);
		std::vector<char> myVector( s.begin(), s.end() );

		for ( char c : myVector )
		{
			std::cout << c << endl;
		}

		for (int i = 0; i < myVector.size(); i++)
		{
			std::cout << i << "\n";
		}

		std::cout << "Enter the index of the letter you want to change: " << endl;

		int index;

		cin >> index;
		
		if (index > myVector.size())
		{
			std::cout << "index is outside the vector" << endl;
		}
		else 
		{	
		   
			std::cout << "Enter the characters index: " <<  endl;
	
			char newChar;
			std::cout << "Type in a letter to be replaced with another: ";
			cin  >> newChar;
			for (unsigned int i = 0; i < myVector.size(); i++) 
			{
				if (myVector[i] == index) 
				{
					myVector[i] = newChar;

					std::cout << myVector[i] << " ";
				}	
			}
		}
					
  return 0;
 }
is this what you're trying to 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;

int main()
{
	std::cout << "Give a person a name: " << endl;

	//string inputString;

	string inputString;
	cin >> inputString;

	std::string s( inputString );
	std::vector<char> myVector( s.begin(), s.end() );

	for( int i = 0; i < myVector.size(); i++ )
	{
		std::cout << i << " - " << myVector[i] << "\n";
	}

	std::cout << "Enter the index of the letter you want to change: " << endl;
	int index;
	cin >> index;

	if( index > myVector.size() )
	{
		std::cout << "index is outside the vector" << endl;
	}
	else
	{

		char newChar;
		std::cout << "Type in a letter to be replaced with another: ";
		cin  >> newChar;
		myVector[index] = newChar;
	}


	for( int i = 0; i < myVector.size(); i++ )
	{
		std::cout << i << " - " << myVector[i] << "\n";
	}

	return 0;
}



Give a person a name: 
angstrom
0 - a
1 - n
2 - g
3 - s
4 - t
5 - r
6 - o
7 - m
Enter the index of the letter you want to change: 
4
Type in a letter to be replaced with another: h
0 - a
1 - n
2 - g
3 - s
4 - h
5 - r
6 - o
7 - m
Last edited on
1
2
3
4
5
6
7
8
9
10
        std::cout << "Type in a letter to be replaced with another: ";
        cin  >> newChar;
//      for (unsigned int i = 0; i < myVector.size(); i++) {
//          if (myVector[i] == index) {
//              myVector[i] = newChar;
//              std::cout << myVector[i] << " ";
//          }
//      }
        myVector[index] = newChar;
        std::cout << myVector[index] << " ";
I want to do like the green Picture above, exactly what i Want to do...The code doesnt seems to work.
do you mean the output in my post? I generated that exactly from the code above by clicking the little gear icon in the top-right of the code block.
Thank you. It seems problem with Visual, hardly anything have worked today. Strange. It works exaclty as above. Thank you very much :)
@patriic48 - Your if statement inside your last for loop is wrong. What you are doing there is checking if element at index i is equal to index (comparing char to int) that is why your program exits and you never assign new char. Also even though you are iterating through the entire vector to find if i == index, your cout will only print the element at index i and not the entire vector with new char at selected index.

You can modify your for loop to look like this:

1
2
3
4
5
6
7
8
9
for(int i = 0; i < myVector.size(); i++)
{
     if(i == index) //instead of myVector[i] == index
     {
          myVector[i] = newChar;
      }

      std::cout << myVector[i] << " ": //moved from if block
}
Topic archived. No new replies allowed.