Help with File I/O project

Pages: 12
I am trying to do a mad libs type of thing where I have a story(Story.dat) and other data files with the words (nouns.dat, verbs.dat, etc.). The words to be raplaced in the story are marked with an * at the beginning and end (ex. *noun*). My problem is when I try to remove the words that are marked and replace them with a random choice from one of the words. I can't get it to work. So far I have only written the code to replace nouns, here it is in pastebin. http://pastebin.com/rU10yy1D

Do you think you guys could point out what I am doing wrong?
Are you looking for *noun* or *nounp*?
nounp, here is my assignment page with the story. nounp is a placemarker for a plural noun.

http://web.mst.edu/~price/cs53/fs11/h8.htm

I did however open up the wrong file, but it should've just replaced it with a singular noun, instead of not replacing anything.
What's your output?
The exact same story with no changes, atleast that I can see anyway.
For the sake of argument, let's say you haven't tried actually using more than just one ifstream variable and attempting to open the files over the top of each other without closing them first, and go ahead and try what I just described.
...what did you just describe?
You have one ifstream variable,
fin
.
You are trying to stuff all of the file inputs into the same variable without closing the inputs.
I think you should use more than one ifstream variables (one for each concurrently open file).
You see, I have only had one lesson over this, I was not aware you could do that.
Well, the point of doing it is to learn, right? Now you know, try it out and see what happens.
You mean like this?

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

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




int main ()
{
  srand(time(NULL));
  char str[256];
  ifstream fin("Story.dat");
  ofstream fout("output.dat");        // open file
  short Rand;
  string adj = "*nounp*";
  Rand = ((rand() % 10) + 1);

  while (fin >> str)     // loop while extraction from file is possible
  {
    fout << str << endl;
  }

  fin.close();
  fin.open("output.dat");



  while (fin >> str)
  {
    cout << str << ' ';
  }
  cout << endl;

  while (fin >> str)
  {
    if (str == adj)
    {
      ifstream fin_nounp("nounp.dat");
      for (int i = 0;i < Rand; i++)
      {
        fin_nounp >> str;
      }
      cout << str;
      fin_nounp.close();
    }
    else
      cout << str << ' ';
  }

  return 0;
}


No, actually I was suggesting more like:
1
2
3
4
5
6
int main(){
    // Variables
    ifstream finStory("Story.dat"), finNoun("noun.dat"), finNounP("nounp.dat");
    // Do stuff
    return 0;
}


However, if you want to open and close noun or nounp only when it's needed, that's fine. Personally, if I'm going to be paging it multiple times, I'm just going to hold it open until I'm finished with it.
Alright, we have made progress. It works a little bit. Only a few words are being replaced. The code is below and the output is here: http://oi40.tinypic.com/ixfw47.jpg


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

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




int main ()
{
  srand(time(NULL));
  char str[256];
  ifstream finStory("Story.dat"), finOutput("output.dat"), finNoun("nouns.dat"), finNounP("nounp.dat");
  ifstream finVerbP("verbp.dat"), finAdj("adj.dat"), finVerb("verbs.dat");
  ofstream fout("output.dat");        // open file
  short Rand = ((rand() % 10) + 1);
  string nounp = "*nounp*", verbp = "*verbp", adj = "*adj*", noun = "*noun*", verb = "*verb*";

  while (finStory >> str)     // loop while extraction from file is possible
  {
    fout << str << endl;
  }

  finStory.close();


  while (finOutput >> str)
  {
    if (str == nounp)
    {
      Rand = ((rand() % 10) + 1);
      for (int i = 0;i < Rand; i++)
      {
        finNounP >> str;
      }
      cout << str << ' ';
      finNounP.close();
    }
    else if (str == verbp)
    {
      Rand = ((rand() % 10) + 1);
 for (int i = 0;i < Rand; i++)
      {
        finVerbP >> str;
      }
      cout << str << ' ';
      finVerbP.close();
    }
    else if (str == adj)
    {
      Rand = ((rand() % 10) + 1);
      for (int i = 0;i < Rand; i++)
      {
        finAdj >> str;
      }
      cout << str << ' ';
      finAdj.close();
    }
    else if (str == noun)
    {
      Rand = ((rand() % 10) + 1);
      for (int i = 0;i < Rand; i++)
      {
        finNoun >> str;
      }
      cout << str << ' ';
      finNoun.close();
    }
    else if (str == verb)
    {
      Rand = ((rand() % 10) + 1);
      for (int i = 0;i < Rand; i++)
      {
        finVerb >> str;
      }
      cout << str << ' ';
      finVerb.close();
    }
    else
      cout << str << ' ';
  }
  cout << endl;
  return 0;
}
It looks to me, from first glance, that you don't have *nouns* defined, and your definition of *verbp* doesn't have a closing asterisk. There may be other minor overlooked errors of similar type.
Alright, I fixed those. I figured out that it is only replacing words of a certain type once, and then ignoring them in the rest of the story.
That's because you close all of the input files after the first iteration of everything. Put the close commands at the last line just before return 0;
By the way, when you get finished with the project, link a copy of some of the mad lib outputs. I love reading those things.
Still isn't quite working.

http://oi40.tinypic.com/2zec8y8.jpg

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

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




int main ()
{
  srand(time(NULL));
  char str[256];
  ifstream finStory("Story.dat"), finOutput("output.dat"), finNoun("nouns.dat"), finNounP("nounp.dat");
  ifstream finVerbP("verbp.dat"), finAdj("adj.dat"), finVerb("verbs.dat");
  ofstream fout("output.dat");        // open file
  short Rand = ((rand() % 10) + 1);
  string nounp = "*nounp*", verbp = "*verbp*", adj = "*adj*", noun = "*nouns*", verb = "*verb*";

  while (finStory >> str)     // loop while extraction from file is possible
  {
    fout << str << endl;
  }

  finStory.close();

  while (finOutput >> str)
  {
    if (str == nounp)
    {
      Rand = ((rand() % 10) + 1);
      for (int i = 0;i < Rand; i++)
      {
        finNounP >> str;
      }
      cout << str << ' ';
    }
    else if (str == verbp)
    {
      Rand = ((rand() % 10) + 1);
      for (int i = 0;i < Rand; i++)
      {
 finVerbP >> str;
      }
      cout << str << ' ';
    }
    else if (str == adj)
    {
      Rand = ((rand() % 10) + 1);
      for (int i = 0;i < Rand; i++)
      {
        finAdj >> str;
      }
      cout << str << ' ';
    }
    else if (str == noun)
    {
      Rand = ((rand() % 10) + 1);
      for (int i = 0;i < Rand; i++)
      {
        finNoun >> str;
      }
      cout << str << ' ';
    }
    else if (str == verb)
    {
      Rand = ((rand() % 10) + 1);
      for (int i = 0;i < Rand; i++)
      {
        finVerb >> str;
      }
      cout << str << ' ';
    }
    else
      cout << str << ' ';
  }
  cout << endl;

  //closing streams
  finNounP.close();
  finVerbP.close();
  finAdj.close();
  finNoun.close();
  finVerb.close();
  finOutput.close();
 return 0;
}

Just curious, what's the point of the rand? My instinct says it's not necessary at all; you may be reaching the end of your input files before you actually get anything useful from them.
well the assignment is to randomly pick a replacement word from the data files
Pages: 12