Need help! replaceing part of an null character array with a string

I got everything to work besides the replacing nouns function, which should switch out the words listed, if listed in the array with a random noun from a text file. I get no errors when compiling, but after the greeting function, the program is stuck and I have to force it to quit. What's wrong with the code in the replace nouns function

main.cpp

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
#include "moefables_function.h"
#include <iostream>
#include <fstream>
#include <ctime>
#include <cstdlib>
#include <cstring>
#include <string>
using namespace std;

int main()
{
  string nouns, nouns2, tale, tale2;
  char answer;
  Greeting();
  do
  {
    tale = fable();
    nouns = Nouns1();
    nouns2 = Nouns2();
    cout << tale;
    tale2 = replaceNouns(tale, nouns, nouns2);
    cout << tale2 << endl;
    MoeRant();
    Moral();
    cout << "\nDo you wish to hear another fable? (y/n)" << endl;
    cin >> answer;
  }while(answer == 'y' || answer == 'Y');
  cout << "\nThank you for listening, come back again.";
  return 0;
}



function.h

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
#ifndef MOEFABLES_FUNCTION_H
#define MOEFABLES_FUNCTION_H

#include <iostream>
#include <fstream>
#include <ctime>
#include <cstdlib>
using namespace std;

//Outputs a message
//Pre: none
//Post: outputs a message on the screen
void Greeting();

//gets the fable as a string
//Pre: none
//Post: brings back the value or string
string fable();

//25% chance to get a rant outputted
//Pre: none
//Post: outputs a rant on the screen at times
void MoeRant();

//gets a noun from a list
//Pre: none
//Post: returns the string
string Nouns1();

//gets a noun from a list
//Pre: none
//Post: returns the string
string Nouns2();

//get a random moral message outputted
//Pre: none
//Post: outputs a moral on the screen
void Moral();

//switches two nouns with two words throughout the fable
//Pre: need to have a value for the two nouns and fable
//Post: returns the new value for fable
string replaceNouns(string fable, const string noun, const string noun2);

#endif 




function.cpp


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
#include "moefables_function.h"
#include <iostream>
#include <fstream>
#include <ctime>
#include <cstdlib>
#include <cstring>
#include <string>
using namespace std;

void Greeting()
{
  cout << "Welcome to Moe's Fable creater" << endl;
  return;
}

string fable()
{
  ifstream fableFilein;
  int randNum;
  string fable;
  srand(time(NULL));
  randNum = rand() % 5;
  if(randNum == 0)
  {
    fableFilein.open("fable1.txt");
    getline(fableFilein,fable);//takes the words of text file and makes it a   string
    fableFilein.close();
  }
  else if(randNum == 1)
  {
    fableFilein.open("fable2.txt");
    getline(fableFilein,fable);
    fableFilein.close();
  }
  else if(randNum == 2)
  {
    fableFilein.open("fable3.txt");
    getline(fableFilein,fable);
    fableFilein.close();
  }
  else if(randNum == 3)
  {
    fableFilein.open("fable4.txt");
    getline(fableFilein,fable);
    fableFilein.close();
  }
  else if(randNum == 4)
  {
    fableFilein.open("fable5.txt");
 getline(fableFilein,fable);
    fableFilein.close();
  }
  return fable;
}

void MoeRant()
{
  int randNum2, a;
  string rant;
  ifstream rantsFilein;
  srand(time(NULL));
  randNum2 = rand() % 4;
  a = rand() % 6;
  if(randNum2 == 0)
  {
    rantsFilein.open("Moe-rants.txt");
    do
    {
      getline(rantsFilein, rant);
    }while(--a >= 0);//gets a random line from text file
    rantsFilein.close();//ends the stream for that certain text file
    cout << " " << rant << endl;
}
  else
  {
    cout << " ";
  }
  return;
}


string Nouns1()
{
  int a;
  string nouns;
  ifstream finNouns;
  srand(time(NULL));
  finNouns.open("list1.txt");
  a = rand() % 14;
  do
  {
    getline(finNouns,nouns);
  }while(--a >= 0);

  finNouns.close();
  return nouns;
}

string Nouns2()
{
  int a;
  string noun;
  ifstream finNoun;
  srand(time(NULL));
  finNoun.open("list2.txt");
  a = static_cast<int>(rand() % 24);
  do
  {
    getline(finNoun,noun);
  }while(--a >= 0);

  finNoun.close();
  return noun;
}

void Moral()
{
  ifstream finMoral;
  string moral;
  int a;
  srand(time(NULL));
  a = (rand() % 6) + 1;
  cout << "\nThe Moe-rale of this story is: ";
  finMoral.open("Moe-rals.txt");
  do
  {
    getline(finMoral, moral);
  }while(--a >= 0);
  finMoral.close();
  cout << moral << endl;
  return;
}

string replaceNouns(string fable, string noun, string noun2)
{
  int i = 0;
  fable.c_str();
  noun.c_str();
  noun2.c_str();
  while(&fable[i] != '\0')
  {
   if(strcpy(&fable[i],"boy") == 0 || strcpy(&fable[i],"ant") == 0 || strcpy(&fable[i],"fox") == 0 || strcpy(&fable[i],"man") == 0 || strcpy(&fable[i],"milkmaid") == 0)
    {
      fable[i] = noun[i];
    }
    else if(strcpy(&fable[i],"serpant") == 0 ||strcpy(&fable[i],"grasshopper")  == 0 || strcpy(&fable[i],"goat") == 0 || strcpy(&fable[i],"trees") ==0 ||strcpy(&fable[i],"milkpail") == 0)
    {
      fable[i] = noun2[i];
    }

  }
  return fable;
}
you cannot use strcpy() with std::string.
use the string function replace() instead. See:

http://www.cplusplus.com/reference/string/string/replace/

Why do you use strcpy() in an if clause? Maybe you want to find the strings within fable? See:

http://www.cplusplus.com/reference/string/string/find/
Topic archived. No new replies allowed.