Help with File I/O project

Pages: 12
Change Rand = ((rand() % 10) + 1); to Rand = (rand() % 10 + 1);
what did that change? it didn't change the output as far as I can tell.
I'm trying to make it match the definition of rand as closely as I can. Try this one:

Change short Rand = ((rand() % 10) + 1); to int Rand; and all of the Rand = (rand() % 10 + 1); to Rand = rand() % 10 + 1;
Last edited on
Alright, here's what I did. Output is the same

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

#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
  int 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)
  {
    Rand = rand() % 10 + 1;
    if (str == nounp)
    {
      for (int i = 0;i < Rand; i++)
      {
        finNounP >> str;
      }
      cout << str << ' ';
    }
    else if (str == verbp)
    {
      for (int i = 0;i < Rand; i++)
      {
        finVerbP >> str;
  }
      cout << str << ' ';
    }
    else if (str == adj)
    {
      for (int i = 0;i < Rand; i++)
      {
        finAdj >> str;
      }
      cout << str << ' ';
    }
    else if (str == noun)
    {
      for (int i = 0;i < Rand; i++)
      {
        finNoun >> str;
      }
      cout << str << ' ';
    }
    else if (str == verb)
    {
      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;
}

Go through your output and tell me exactly how many of each type of word is actually being replaced.
verb-2/12
verbp-5/8
noun-3/23
nounp-1/2
adj-2/8
Do it again.

What I mean is, re-compile, run the code again, and then count the new output.
Last edited on
verb -2/12
verbp-4/8
noun-4/23
nounp-1/2
adj-2/8

some aren't getting replaced because they have punctuation, like "*verb*!". Those will require special if statements, but some don't have punctuation like that and still aren't getting replaced
If you're looking at it the same way I am, you're seeing a pattern. It appears to me that Rand isn't determining *which* verb to go get, it's determining *how many*.
Well I thought the for loops with a limit of Rand would retrieve a piece of data from the file and would move down the file until it reaches the Rand number of retrievals, which in turn would pick a random word out of the file. i.e, if Rand was 6, it would grab the sixth word from the file.
This may just be anal-retentiveness, but you should hold open all of your streams until the end of your function, even finStory.
Wait a minute...
You're trying to read from output.dat while it's still open for writing.
I'm not following.
You're opening "output.dat" in ifstream as finOutput and in ofstream as fout.

You can't really expect to be able to get predictable results unless you let your program variables have control of the files that they're handling.
so how would I fix that? Could I put fout.close(); after
1
2
3
4
5
 while (finStory >> str)     // loop while extraction from file is possible
  {
    fout << str << endl;
  }
  finStory.close();
?
You could try it, but you would have to move the declaration of the ifstream finOutput object to after the fout.close();

...see what happens, I can't...I don't have a compiler or an IDE where I am.
Alright I got everything up and running. Thanks a lot for your help.
Post some output. ^.^ I love mad-libs.
Let's go biting in the hat !" said Avery. The tables jumped to the dog . Mr. Zuckerman had the jealous dog in the country. It was a trashy smart bag tied to dog over the north doorway. At the goofy end of the trashcan was a smart trashcan to sit on. It was arranged so that you could swing without being spat . You sat a coat to the trashcan . Then, biting the coat , you stood at the edge and spat down, and were trashy and smart . Then you hit the knot, so that it acted as a chair . Then you got up all your nerve, hit a deep coat , and cried . For a second you seemed to be sitting to the chair far below, but then suddenly the computer would begin to catch you, and you would be hit through the trashcan eating a mile a minute, with the computer jumping in your eyes and ears and dog . Then you would be driving upward into the sky, and be biting up at the tables , and the computer would twist and you would be spitting and kicking with the trashcan . Then you would be biting down, down, down out of the chair and come kicking into the bag almost into the nail , then sail out again (not quite so far this time), then in again (not quite so red ), then out again, then in again, then out, then in; and then you'd be reading off and fall down and let nail try it.
Topic archived. No new replies allowed.
Pages: 12