473,395 Members | 1,474 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Global function error

TimNick90
I am making a hangman program for school that uses a function for the player to input their guess and a seperate function to test whether or not their guessed letter is in the word. The program worked fine without the function to test the players guess but when i copied the code from the main program and turned it into the function i had to change most of the variables to global. All but one is working, my "soFar" string is supposed to change the randomly choosen word to "-" and it uses the ".size" function to do so. When I ran the program it didn't want to work any more. Any advice would be greatly appreciated. Here's my code:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cctype>
#include <conio.h>

using namespace std;

char enterGuess(string ask);
void testGuess(char playerGuess);

char guess;
vector<string> words; //collection of possible words to guess
const string THE_WORD = words[0];
double wordSize = THE_WORD.size();
string soFar(wordSize, '-');
string used = "";
int wrong = 0;

int main()
{
//setup
const int MAX_WRONG = 8; //maximum number of incorrect guesses allowed

words.push_back("GUESS");
words.push_back("HANGMAN");
words.push_back("DIFFICULT");

srand(time(0));
random_shuffle(words.begin(), words.end());




cout << "Welcome to Hangman. Good luck!\n";

//main loop
while ((wrong < MAX_WRONG) && (soFar != THE_WORD))
{
cout << "\n\nYou have " << (MAX_WRONG - wrong) << " incorrect guesses left.\n";
cout << "\nYou've used the following letters:\n" << used << endl;
cout << "\nSo far, the word is:\n" << soFar << endl;

guess = enterGuess("\n\nEnter your guess: ");

guess = toupper(guess); //make uppercase since secret word is in uppercase

while (used.find(guess) != string::npos)
{
cout << "\nYou've already guessed " << guess << endl;
cout << "Enter your guess ";
cin >> guess;
guess = toupper(guess);
}

used += guess;

testGuess(guess);

}

//shut down
if (wrong == MAX_WRONG)
cout << "\nYou've been hanged!";
else
cout << "\nYou guessed it!";

cout << "The word was " << THE_WORD << endl;

getch();

}

char enterGuess(string ask)
{
char playerGuess;
cout << ask;
cin >> playerGuess;
return playerGuess;
}

void testGuess(char playerGuess)
{
if (THE_WORD.find(guess) != string::npos)
{
cout << "That's right! " << guess << " is in the word.\n";

// update soFar to include the newly guessed letter
for (int i = 0; i < THE_WORD.length(); ++i)
if (THE_WORD[i] == guess)
soFar[i] = guess;
}
else
{
cout << "Sorry, " << guess << " isn't in the word.\n";
++wrong;
}
}
Dec 5 '07 #1
8 1828
gpraghuram
1,275 Expert 1GB
Hi,
There few issues in the code.
1)You are initializing the global variable as follows..
const string THE_WORD = words[0];

But the words is intialized only inside the main function.So as you try to run it you will get a sementation fault.

2)Second when i tried to run it after solving it it is entering into a infinite loop


Raghuram
Dec 6 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
And read this article: http://www.thescripts.com/forum/thread736028.html.
Dec 6 '07 #3
Hi,
There few issues in the code.
1)You are initializing the global variable as follows..
const string THE_WORD = words[0];

But the words is intialized only inside the main function.So as you try to run it you will get a sementation fault.

2)Second when i tried to run it after solving it it is entering into a infinite loop


Raghuram

Thank you, but when you say words is initialized only in the main function you are refering to when I am using .pushback to fill the vector? When I try to add this to global it is saying that I'm missing the semicolon before the period. As is:

words;.pushback("GUESS")

I thing i figured out the infinate loop, were you referring to the testGuess function right? Am I entering something wrong? Here's the code for the function:

void testGuess(char playerGuess)
{
if (THE_WORD.find(guess) != string::npos)
{
return
{
cout << "That's right! " << guess << " is in the word.\n";

// update soFar to include the newly guessed letter
for (int i = 0; i < THE_WORD.length(); ++i)
if (THE_WORD[i] == guess)
soFar[i] = guess;
}
}
else
{
return cout << "Sorry, " << guess << " isn't in the word.\n";
++wrong;
}
}
Dec 10 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
This is very odd. Now the article has a different thread. Try this:
http://www.thescripts.com/forum/thread737451.html.
Dec 11 '07 #5
gpraghuram
1,275 Expert 1GB
Thank you, but when you say words is initialized only in the main function you are refering to when I am using .pushback to fill the vector? When I try to add this to global it is saying that I'm missing the semicolon before the period. As is:

words;.pushback("GUESS")

I thing i figured out the infinate loop, were you referring to the testGuess function right? Am I entering something wrong? Here's the code for the function:

void testGuess(char playerGuess)
{
if (THE_WORD.find(guess) != string::npos)
{
return
{
cout << "That's right! " << guess << " is in the word.\n";

// update soFar to include the newly guessed letter
for (int i = 0; i < THE_WORD.length(); ++i)
if (THE_WORD[i] == guess)
soFar[i] = guess;
}
}
else
{
return cout << "Sorry, " << guess << " isn't in the word.\n";
++wrong;
}
}
Hi,
What i was referring to is you shuld initialize the variable THE_WORD inside the main function and after you have pushed the words to the vector.

Raghuram
Dec 12 '07 #6
Hi,
What i was referring to is you shuld initialize the variable THE_WORD inside the main function and after you have pushed the words to the vector.

Raghuram
If I were to initialize THE_WORD inside the main then soFar wouldn't work because if refers to THE_WORD and soFar has to be global.

TimNick
Dec 12 '07 #7
gpraghuram
1,275 Expert 1GB
If I were to initialize THE_WORD inside the main then soFar wouldn't work because if refers to THE_WORD and soFar has to be global.

TimNick
Check this code....
I have made the cahanges and it is compiling.
As you wish the variables you have used are global but initialized inside the main.
Expand|Select|Wrap|Line Numbers
  1. //Your code......
  2.  
  3. char guess;
  4. vector<string> words; //collection of possible words to guess
  5. //const string THE_WORD = words[0]; commented by ME
  6. string THE_WORD;
  7. //double wordSize = THE_WORD.size();commented by ME
  8. double wordSize;
  9. //string soFar(wordSize, '-'); Commented by ME
  10. string soFar;
  11. string used = "";
  12. int wrong = 0;
  13.  
  14. int main()
  15. {
  16.     //setup
  17.     const int MAX_WRONG = 8; //maximum number of incorrect guesses allowed
  18.  
  19.     words.push_back("GUESS");
  20.     words.push_back("HANGMAN");
  21.     words.push_back("DIFFICULT");
  22.  
  23.     srand(time(0));
  24.     random_shuffle(words.begin(), words.end());
  25.  
  26.     //ME  - initializing the values here
  27.     THE_WORD=words[0];
  28.     wordSize = THE_WORD.size();
  29.     string temp(wordSize, '-');
  30.     soFar=temp;
  31. //your remaining code
  32.  

Thanks
Raghuram
Dec 13 '07 #8
Check this code....
I have made the cahanges and it is compiling.
As you wish the variables you have used are global but initialized inside the main.
Expand|Select|Wrap|Line Numbers
  1. //Your code......
  2.  
  3. char guess;
  4. vector<string> words; //collection of possible words to guess
  5. //const string THE_WORD = words[0]; commented by ME
  6. string THE_WORD;
  7. //double wordSize = THE_WORD.size();commented by ME
  8. double wordSize;
  9. //string soFar(wordSize, '-'); Commented by ME
  10. string soFar;
  11. string used = "";
  12. int wrong = 0;
  13.  
  14. int main()
  15. {
  16.     //setup
  17.     const int MAX_WRONG = 8; //maximum number of incorrect guesses allowed
  18.  
  19.     words.push_back("GUESS");
  20.     words.push_back("HANGMAN");
  21.     words.push_back("DIFFICULT");
  22.  
  23.     srand(time(0));
  24.     random_shuffle(words.begin(), words.end());
  25.  
  26.     //ME  - initializing the values here
  27.     THE_WORD=words[0];
  28.     wordSize = THE_WORD.size();
  29.     string temp(wordSize, '-');
  30.     soFar=temp;
  31. //your remaining code
  32.  

Thanks
Raghuram
It works as it should. Thank you so much.

TimNick
Dec 13 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: WhyteWolf | last post by:
I'm trying to set a object as global for access through out the rest of my script ... {a basic SQL accessing object} however if I try calling the object from with in another object it acts as if it...
1
by: Andr? Roberge | last post by:
I have the following two files: #--testexec.py-- def exec_code(co): try: exec co except: print "error" #-- test.py--
4
by: Andrew V. Romero | last post by:
I have been working on a function which makes it easier for me to pull variables from the URL. So far I have: <script language="JavaScript"> var variablesInUrl; var vArray = new Array(); ...
2
by: Thomas Matthews | last post by:
Hi, I'm getting linking errors when I declare a variable in the global scope, but not inside a function. The declarations are the same (only the names have been changed...). class Book {...
2
by: Tony Johansson | last post by:
Hello! I'm reading a book about C++ and there is something that I don't understand so I ask you. Below I have the text from the book and the code from the file where main is located and some...
17
by: MLH | last post by:
A97 Topic: If there is a way to preserve the values assigned to global variables when an untrapped runtime error occurs? I don't think there is, but I thought I'd ask. During development, I'm...
2
by: tshad | last post by:
I am trying to set up some shared functions in my Global.asax file and can't seem to get it to work. In my Global.asax: Public Class myUtils inherits System.Web.HttpApplication public...
9
by: CDMAPoster | last post by:
About a year ago there was a thread about the use of global variables in A97: http://groups.google.com/group/comp.databases.ms-access/browse_frm/thread/fedc837a5aeb6157 Best Practices by Kang...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
20
by: teddysnips | last post by:
Weird. I have taken over responsibility for a legacy application, Access 2k3, split FE/BE. The client has reported a problem and I'm investigating. I didn't write the application. The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.