char array task...where am i going wrong?

Jun 1, 2013 at 8:46pm
Write your question here. : hello everybody...just finished this exercise...but for some reason something is not working... i should write a program to eliminate a given word in a given sentence...compiling fine..but nothing is working...help..thanks!

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
  #include <iostream>
using namespace std;

char space = ' ';
int counting = 0, counting_2 = 0;
bool word_found = false;
int lenght = 0, note = 0;
char sentence [1000], word [50];

void eliminate_word (char sentence[], char word[]);

int main (){
cout<<"Insert sentence..."<<endl<<endl;
for (int i = 0; i < 1000; i++){
cin >> sentence [i];}

cout<<"insert word.."<<endl<<endl;
for(int s = 0; s < 50; s++){
cin>> word[s];} ;

eliminate_word ( sentence, word);

}

void eliminate_word (char sentence[], char word[]){

for (int i=0; i<50 ; ){while (word [i] != 0 || word [i] != space)
                              {i++; counting ++;}
}

for(int a = 0; a < 1000;) { if (sentence [a]==word[0] && sentence[a-1] == space)
                                  { note = a; lenght = (note + counting) /* to help check if at least lenght is equal*/ ;}


for (int k = note; k < lenght;) {if (sentence[k] != 0 || sentence [k]!= space) {k++; counting_2 ++; }

if (counting_2 == counting){ for(int i = 0; i<counting;){ if (word[i]== sentence [note+i]) word_found = true;}

for (int i = 0; i< counting; i++){sentence[note+i] = space;}
               } 
   }
 }
}


Jun 1, 2013 at 9:22pm
cin does not keep track of spaces. Use getline instead. Also, use strings, not character arrays (you can treat strings like character arrays by parsing through using an iterator, but there's not much need to since it includes a plethora of functions that make that usually unnecessary).
Topic archived. No new replies allowed.