Guessing game help.

In the follow program, I am having an error when I write to an ouputfile. When my program asks the user for the name of the output file, it first says "Output file did not open", and THEN immediately after send to the output file. Can you please tell me how to fix this. Thanks. (Dont worry about the ID function. it has my name so i removed the function definition)

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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
  
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <cctype>
#include <fstream>
#include <string>

using namespace std;

void playGame (int random);
void getGuess(int guessArray[], int numElements, int& guess, int attemptNumber);
int searchList(const int list[], int numElems, int value);
bool playAgain();
void selectionSort(int array[], int size);
void writeOutput(int numElems, int outputArray[]);
bool openOutputFile(ofstream &outfile);
void idFunction(int hwNum);

int main ()
{
    const int MAX = 100; // max value of random generated number
    const int MIN = 1; // min number of random generated number
    int random; // the randomly generated number the user is trying to guess
    int gameNumber = 0; // the number of games the user has played so far
    int answerArray[MAX]; // the array holding the multiple randomly generated numbers

    idFunction(6);

    srand(time(0));
    do{
    random = ((rand() % (MAX-MIN+1)) + MIN);
    answerArray[gameNumber] = random;
    gameNumber++;
    playGame(random);
    }while (playAgain() == true && gameNumber < 50);

    if (gameNumber > 49)
        cout << "You can play a maximum of 50 games." <<endl;
        cout << "Thank You for playing!" << endl << endl;


    selectionSort(answerArray, gameNumber);


    return 0;

}

void playGame (int random)
{
    int attemptNumber = 0; const int maxAttempts = 19; int guessArray[maxAttempts]; int guess;
    do
    {
        getGuess (guessArray, attemptNumber, guess, attemptNumber);
        if (guessArray[attemptNumber] < random)
            cout << "Your number is too low." << endl;
        else if (guessArray[attemptNumber] > random)
            cout << "Your number is too high." << endl;
        else
        {cout << "Congratulations, you got the number!" << endl;
            cout << "You guessed " << random << " in " << attemptNumber + 1 << " tries" << endl;
        }
        ++attemptNumber;
    }while(attemptNumber <= maxAttempts && guess != random);

    if (attemptNumber > maxAttempts)
        cout << "Sorry, you ran out of tries. The number was " << random << " ." << endl;

}

void getGuess(int guessArray[], int numElements, int& guess, int attemptNumber)
{
    bool invalidGuess; // this is true if the user entered a guess out of the range
    do
    {
    invalidGuess = false;
    cout << "Please guess a number from 1 to 100. " << endl;
    cin >> guess;
    if (guess < 1 || guess > 100)
    {
        cout << "That guess is out of range. " << endl;
        invalidGuess = true;
    }
    if (searchList(guessArray, numElements, guess) != -1)
    {
        cout << "You already guessed that number." << endl;
        invalidGuess = true;
    }
    }while (invalidGuess == true);

    guessArray[attemptNumber] = guess;

}


int searchList(const int list[], int numElems, int value)
{
   int index = 0;       // Used as a subscript to search array
   int position = -1;   // To record position of search value
   bool found = false;  // Flag to indicate if the value was found

   while (index < numElems && !found)
   {
      if (list[index] == value)  // If the value is found
      {
         found = true;           // Set the flag
         position = index;       // Record the value's subscript
      }
      index++;                   // Go to the next element
   }
   return position;
        // Return the position, or -1
}

bool playAgain()
{
    char wantsToRepeat; // keeps track of whether or not the user wants to play again
    bool invalidInput; // this is true if the user enters a value other than y or n (yes or no)

    do
    {
    cout << endl << endl;
    cout << "Do you want to play another game? Enter y for yes or n for no." << endl;
    cin >> wantsToRepeat;
    wantsToRepeat = tolower(wantsToRepeat);
    invalidInput = false;
    cout << endl << endl;
    if (wantsToRepeat == 'y')
        return true;
    else if (wantsToRepeat == 'n')
        return false;
    else
    {
      invalidInput = true;
      cout << "Please enter y for yes or n for no." << endl;
    }
    }while (invalidInput == true);


}

void selectionSort(int array[], int size)
{


   int startScan, minIndex, minValue;

   for (startScan = 0; startScan < (size - 1); startScan++)
   {
      minIndex = startScan;
      minValue = array[startScan];
      for(int index = startScan + 1; index < size; index++)
      {
         if (array[index] < minValue)
         {
            minValue = array[index];
            minIndex = index;
         }
      }
      array[minIndex] = array[startScan];
      array[startScan] = minValue;
   }
   writeOutput(size, array);
}


void writeOutput(int numElems, int outputArray[])
{

    ofstream outputFile; // The random numbers are listed in numerical order in an outside file

    openOutputFile(outputFile);

    if (openOutputFile(outputFile) == true)
    {

    outputFile << "Your randomly generated numbers in \n"
     << " numerical order are as follow: " << endl;
    }

   for (int i=0; i < numElems ; i++)
   {
       outputFile<< outputArray[i] << " ";
       if ((i+1)%6==0)
        outputFile << endl;
   }
}

bool openOutputFile(ofstream &outfile)
{
    string filename; // The name of the file the user wants to send the random numbers to

    cout<<"Enter output filename: " ;
    getline(cin, filename);
    outfile.open(filename.c_str());
    if(outfile.fail()){
        cout<<"Unable to open output file\n";
        return false;
    }
    return true;
} // end openOutputFile

void idFunction(int hwNum)
{
    cout<<"CIS 22A Programming Homework # 6" << hwNum << endl;
    cout <<"My name"<< endl << endl;
}
On line 173 you are calling openOutputFile and then you call it again on line 175 as part of your if statement, you probably want to delete line 173. Your for loop on line 182 is getting called regardless of the result of the if statement above it, I think you want to take a look at the } on line 180 you might want that after the for loop.
i tried deleting lina 173 before posting, but if i do that it just says "Unable to open output file" so i thought it didnt actually perform the command in line 175.
Last edited on
No it does execute it in line 175, but it cant actually open the file. The only things I can think of checking are, Does the file you are trying to open actually exist? If you are using an IDE is the file in the current working directory of the project?
for some reason if i delete line 173, it doesnt give me the option to enter a file name. it outputs the following:



Please guess a number from 1 to 100.
50
Your number is too low.
Please guess a number from 1 to 100.
75
Your number is too low.
Please guess a number from 1 to 100.
90
Your number is too high.
Please guess a number from 1 to 100.
80
Your number is too low.
Please guess a number from 1 to 100.
85
Congratulations, you got the number!
You guessed 85 in 5 tries


Do you want to play another game? Enter y for yes or n for no.
n


Thank You for playing!

Enter output filename: Unable to open output file

Process returned 0 (0x0) execution time : 13.909 s
Press any key to continue.





Enter output filename: AND Unable to open output file pop up at the same time.
Last edited on
Every time you use a cin >> in your program it leaves a "\n" in the input buffer, when you call the getline on line 195 the first thing it sees is "\n" which is allready in the buffer so it takes that as your input and then tries to open a file with no name. What you need to do is use cin.ignore in a way that removes all the "\n" s in the buffer.
Topic archived. No new replies allowed.