Help with character array

Hello, I am seriously stuck on this part of my program in which I have to insert a character into the array and it then pushes the previous characters into the next element box also maintaining the pointer at that spot.




If you copy the code into a compiler and run it, It will function all but the insert function that I cannot seem to grasp

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <cstdlib>
#include <iostream>
#include <iomanip>

using namespace std;

const int size=5;

void initialize (char text [size], int& current);
void move (int& current, int distance);
void insert (char text [size], int& current, char ch);
void replace (char text [size], int current, char ch);
void print (char text [size], int current);



int main()
{
    
    
    char array[size];  
   
    char ch;
    int distance;
    int current=0;
    char character;
   
    initialize (array, current);
   
        
    do 
    {cout << "Enter p (print), m (move), i (insert), r (replace) or q (quit): ";
    cin >> character;
    cout << endl;
   
    
    
            if(character=='p')
            print (array, current);
            
            
            else if (character=='m')
            { cout << "Enter the distance to move: ";
            cin >> distance;
            move (current, distance);
            }
            
            
            else if (character=='i')
            {
            
            cout << "Enter the character to insert: ";
            cin >> ch;
            cout << endl;
            cout << "Character " <<ch<< " inserted at index " << current<< endl;
            cout << endl;
            insert (array, current, ch);
            
            
            
            }
            
            else if (character=='r')
            {  
            cout << "Enter the character to replace: ";
            cin >> ch;
            cout << endl;
            cout << "Character " <<ch<< " placed at index " <<current<< endl;
            replace (array, current, ch);
            cout << endl;
             }
            
             

}
    while (character!='q');
    return EXIT_SUCCESS;

}

    

void initialize (char text [size], int& current)
{
     current=0;
     
     int index;

     for (index=0; index<size; index++)
     {
         text[index]='*';
                  
                            }
               
               

}

void move (int& current, int distance)
{
     
     
     if (distance+current>size-1)
     {
      cout << "Error, new position "<<distance<<" is illegal!"<< endl;
      cout << endl;
      }
      else if (distance+current<0)
      {
      cout <<"Error, new position "<< distance<<" is illegal!"<< endl;
      cout << endl;
      }
      else      
      {
      (current=current+distance);
      cout <<"current index changed to: "<< distance<< endl;
      cout << endl;
      }
      
}
  
void replace (char text [size], int current, char ch)
{
    text[current]=ch;
    
       
 }

    
    
void insert (char text [size], int& current, char ch)
{
     int index;
     
     if (current<size-1)
        
        {
         for (index=size-1; index>current; index--);
             {
             
             text[index]=ch;
             text[current-1]=ch;
             current++;
             
             }
            
         
         }
        
        
     else
     cout << "Error, insertion is not allowed" << endl;             
            
     
 }
 
  
void print (char text [size], int current)
{
     int i;
     
     cout << "The current array is: " << endl;
     
     
     for (i=0; i<size; i++)
         cout << text[i];
      cout << endl;
     cout << setw(current+1)<<"^" << endl;
     cout << endl;
     
     
    

}
Last edited on
Please use [code] tags around your code to make it readable.

The first algorithm that comes to mind is to start at the last character in the array and move it to the right one space; repeat this process moving left until you are at the index to insert to. At that point, you'll have effectively moved all of the string to the right one space from where you want to insert and can just write the character in.
Thanks for the reply, I have also edited the post to make it more readable.

When the program is initiated the array outputs

*****

when replace is called and entered the letter 'K' the program outputs

K****
^

When insert is called and 'J' is entered the program is suppose to output

JK*** (the caret is now suppose to be under K)
^

But I can only get the program to output J***** like its acting as another replace function
Last edited on
Did you try to implement the algorithm I mentioned in my previous post? Or is there some other method you are trying to use? I ask because it'd probably help me see where you are going wrong with the implementation.
sorry for that. Im trying to use a for loop with the included if else statement that is in the code. I think I am messing up the for loop as the if else statements function correctly when I run the program. I am trying to get the index to be one less than array size because its a constant and I need it to work with any size array. A teacher aid only told me that my for loop was messed up and I've been sitting here for the past 6 hours trying to figure it out.

I can not seem to code that algorithm as i have been killing myself over figuring it out
Insert outputs J****
With pointer under J

I need the insert function to carry the K with it to look like JK***
With pointer under K
Last edited on
Help! Im still stuck another day later
bump
Topic archived. No new replies allowed.