Wont Compile

closed account (z8q4izwU)
I cant figure out why it wont compile right. If anyone can help me with this please 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
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
#include <iostream>

using namespace std;
// constant

const int size = 10;

// function prototype

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 text [size];
           int function; 
           int current;
           int distance;
           char ch;
           
           //Calls the functions          
           
           initialize (text, current);
           
           move (current, distance);
           
           insert (text, current, ch);
           
           replace (text, current, ch);
           
           print (text, current);
           
           
 

while (function != 'q')

{
            cout << "Enter p (print) , m (move) , i (insert) , r (replace) or q (quit): ";
            cin >> function;



if (function == 'm')
   {
             cout << "How far do you want to move? ";
             cin >> distance ; 
             
             cout << "Current index changed to :" << current << endl;
             
             }
   
   if (function == 'p')
   { 
          cout << "The current array is :" << text << endl;
          }
   
   if (function == 'i')
   {
                cout << "Enter the charactter to insert :";
                cin >> ch; 
                cout << "Character" << ch << "placed at index " << current << endl; 
                
                }
   
   if (function == 'r')
   {
                cout << "Enter the character to replace :";
                cin >> ch;
                
                cout << "Character" << ch << "placed at index " << current << endl;  
                
                }
              
              }

    system("PAUSE");
    
    return 0;
    }
void initialize (char text [size], int& current)
     {
                current = 0;
              int z;       
     for (z = 0; z < size; z++)
         text [z] = '*';

                }


void move (int& current, int distance)
{
         
     if (distance < size)
     cout << "Current index changed to: " << current << endl;

     else if (distance - current > size)
     cout << "Error Outside of Boundary " << distance << endl;
     
     }          

void insert (char text [size], int& current, char ch)
{
     int i;
     for( i - size; i > current; i --)
     {
     text [i] = text[i--];
     text[current] = ch;
     }
 }


void replace (char text [size], int& current, char ch)
{ 
     text [current] =ch;
     cout << "Character" << ch << "placed at index " << current << endl; 
     current++;
     
      }

void print (char text [size], int current)
{
      int z;
     
     for (z = 0; z < size; z++)
         cout << text [z] << " ";
         }



         
     
Last edited on
If the compiler fails, it will give you error messages. Those messages tell you what's wrong with the code.

It's OK if you don't understand what they mean... but learning to read and understand compiler errors is an important part of coding.

Post the errors here so we can see them and then we can help show you what the errors mean and what's wrong with your code.
Line 33:
distance in an undeclared variable.


Line 55:
cin >> distance >> endl;

Should be:
cin >> distance;


Line 70:
cout << "Character" >> ch >> "placed at index " << current << endl;

should be:
cout << "Character" << ch << "placed at index " << current << endl;


main() has no closing brace '}'


Line 105:
Should be else if


In functions replace and insert, index is an undeclared variable.
Last edited on
closed account (z8q4izwU)
So i fixed all of your comments, but now it wont run. The error says

{linker error} undefined reference to 'print(char*, int&)'
Id returned 1 exit status.


I updated the original post with the fixed code.
closed account (z8q4izwU)
I figured that out now have to fix the program
closed account (z8q4izwU)
Can someone help with my move function? Before this line ("Enter p (print) , m (move) , i (insert) , r (replace) or q (quit):) prints the program is printing "Current index changed to: 0. I know my move function is wrong but im still trying to figure out what it is thats wrong. can someone figure out an easy way of fixing this? Also if i get rid of move function it just prints a blank screen.
Topic archived. No new replies allowed.