C++ :: For Loop In Hangman Game - Read A Word From Text File Randomly And Matches It With Definition

Jun 26, 2014

Ok here I have a program that reads a word from a text file randomly and matches it with the definition. The user has to guess what the word is according to the definition.

I'm having trouble with my for loop, I'm not getting any errors. But I just know something is off.

Here's my code:

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;
int main(){
int number;
int count = 0;
int numOfGuess = 0;

[Code] ...

This is words.txt:

apple#the usually round, red or yellow, edible fruit of a small tree
boat#a vessel for transport by water
horse#a solid-hoofed plant-eating domesticated mammal with a flowing mane and tail, used for riding
television#a system for transmitting visual images and sound that are reproduced on screens
soup#a liquid dish, typically made by boiling meat, fish, or vegetables, etc.
bottle#a container, typically made of glass or plastic and with a narrow neck
barber#a person who cuts hair
toast#sliced bread browned on both sides by exposure to radiant heat
radar#a system for detecting the presence, direction, distance, and speed of aircraft, ships, and other objects
red#of a color at the end of the spectrum next to orange and opposite violet

View 1 Replies


ADVERTISEMENT

C++ :: Hangman Definition Game - Extract Word Randomly Form File And User Guess The Word

Jun 25, 2014

Basically I have a text file called words. I'm supposed to extract a word randomly form the file and have the user guess the word. If they guess the word correctly in x number of tries they will receive the definition.

I'm having trouble receiving that random word and I'm getting the definitions from the file.

Here's my code:

#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;

[Code] ....

This is what is in the words.txt file
apple#the usually round, red or yellow, edible fruit of a small tree
boat#a vessel for transport by water
horse#a solid-hoofed plant-eating domesticated mammal with a flowing mane and tail, used for riding
television#a system for transmitting visual images and sound that are reproduced on screens
soup#a liquid dish, typically made by boiling meat, fish, or vegetables, etc.
bottle#a container, typically made of glass or plastic and with a narrow neck
barber#a person who cuts hair
toast#sliced bread browned on both sides by exposure to radiant heat
radar#a system for detecting the presence, direction, distance, and speed of aircraft, ships, and other objects
red#of a color at the end of the spectrum next to orange and opposite violet

View 3 Replies View Related

C++ :: Match Word To Definition Hangman Game

Jun 25, 2014

Basically I have a text file called words. I'm supposed to extract a word randomly from the file and have the user guess the word according to the definition.

I'm having trouble matching the definition to the word from the text file.

Here's my code:

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;
int main(){
int number;
int count = 0;
string word;

[Code] ....

Here is my text file
apple#the usually round, red or yellow, edible fruit of a small tree
boat#a vessel for transport by water
horse#a solid-hoofed plant-eating domesticated mammal with a flowing mane and tail, used for riding
television#a system for transmitting visual images and sound that are reproduced on screens
soup#a liquid dish, typically made by boiling meat, fish, or vegetables, etc.
bottle#a container, typically made of glass or plastic and with a narrow neck
barber#a person who cuts hair
toast#sliced bread browned on both sides by exposure to radiant heat
radar#a system for detecting the presence, direction, distance, and speed of aircraft, ships, and other objects
red#of a color at the end of the spectrum next to orange and opposite violet

View 1 Replies View Related

C++ :: Hangman Game With Word Bank Input From TXT File

Oct 31, 2013

I am trying to pull my words randonly from a wordbank .txt file and so far i have this

#include <iostream>
#include <cctype>
#include <fstream>
using namespace std;
string word;
char knownLetters[]; // The word could be of any length
int stages = 6; // Our hangman has 6 stages, so the player starts with 6 lives

[Code] .....

View 1 Replies View Related

C++ :: Dictionary - Display Definition Of Searched Word From Text File

Oct 20, 2014

I am now making a Dictionary using C++. My goal is to search the word and display the definition of the searched word from the text file. I have a code but it is not working. How can I search a word and display the definition of the word from a text file?

void searchFile(string word) {
ifstream ifile;
ofile.open("dictionary.txt", ios::in);
string line;
string search;

[Code] ....

View 1 Replies View Related

C/C++ :: Read All Song Titles From A Text File - Randomly Select And Output?

Apr 19, 2015

So far I have managed to sort songs by their string length in main. The void function is there because i learned how to create it, but it's not being used because I dont know how to use it.

Expanding on main, how would I go about selecting a random song from the array?

#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
struct Song {
string song;

[code]....

View 2 Replies View Related

C++ :: Hangman Game - Reading One Line From TXT File

Dec 23, 2013

My question is how can i read one line from a ".txt" file? To be more clear, i'm trying to write a hangman game. Therefore I'm trying to get words from a "wordlist.txt" file i created. I know a little bit about ifstream, ofstream and fstream. So my function i created to get a random line from this txt (not-completed yet since i don't know how to get that randomized line);

void grw() {
int line;
srand (time(NULL));
line = (rand() % 7972) + 1;
ifstream wordlist;
wordlist.open("wordlist.txt");
}

I created a variable called 'line' for the line number and randomized it (there are 7972 words in ".txt" file). So what i want to do now is to get the word on that line.

View 4 Replies View Related

C :: Create A Hangman Game With Timer

Mar 21, 2013

I want to create a Hangman game with timer.. If the user cannot guess the word with in 1:30 min the game will stop and the word will be printed.

View 5 Replies View Related

C++ :: Algorithm For Letters Used In Hangman Game?

Jun 2, 2013

designing an algorithm forHangman game.

I've got everything I need that is expected of my program except this.

When the user starts the game they will have these letters available to them:

"abcdefghijklmnopqrstuvwxyz"

Now if they were to guess 'a' it should look like this:

"-bcdefghijklmnopqrstuvwxyz"

then if they were to guess 'k':

"-bcdefghij-lmnopqrstuvwxyz"

and so forth.

View 3 Replies View Related

C++ :: Replacing String In Hangman Game?

Mar 5, 2014

I'm making a hangman game. All i need to do is after the user guesses the letter correctly it replaces the stars with that letter. I have been trying for the past 2 hours and cant get it. So basically I have to do this. DisplayWord.replace(0, theWord(0)) but I don't know how to put it in.

This is my code:

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

[Code].....

View 2 Replies View Related

C/C++ :: How To Keep Track Of Used Letters In Hangman Game

Oct 1, 2014

i am trying to make a hangman game. The game works fine so far but i cannot seem to keep track of the letters used. I have to prevent the user from using a letter that has already been entered by the user at some point in the game before. But can't seem to work my way around it. I have included the code below

#include<iostream>
#include<cstring>
#include<cstdlib>
#include<ctime>

[Code].....

View 3 Replies View Related

C++ :: Take User Input As String And Test To See If Matches A Word

Dec 4, 2014

I'm creating a program that takes user input in the form of a string and tests to see if it matches a word. Each correct word will increase their score by one. Here is a portion of the code that is not working.

...
else if(s == 32) {
if(!currentLetter.empty()) {
currentLetter.erase(currentLetter.length() - 2, currentLetter.length() - 1);
} if(currentLetter.compare(oWord) == 0) {

[Code] .....

To me, this looks like it should do a very simple task as intended-take a String, compare it to another, and reset the word if they match or output incorrect if not. But, I'm not sure if there is some quirk in C++ with Strings, because this code always outputs Incorrect. Please try again. and the score never increases. I also tested this by literally setting the strings equal in the code, which still resulted in it not doing what it's supposed to.

View 3 Replies View Related

C++ :: Hangman Game - Generating Random Number

Sep 12, 2013

I am recreating a hangman game. I'm trying to generate a random number to choose which word from my words list to use and I've done it a billion times before just like this. So, here's my code:

#include <iostream>
#include <cstdlib> //standard library.
#include <ctime> //for random.
#include <fstream> //for reading and writing to a file.
#include <string>
using namespace std;
int main() {
int num_words;
string word[num_words];

[Code] .....

I have the text file "words.txt" info below.

5
dog
cat
horse
pig
bird

If I run the program as it is, I get a segmentation fault core dumped error, however, if I take all the stuff that reads in the words from the words.txt file, like so:

#include <iostream> //every program has this.
#include <cstdlib> //standard library.
#include <ctime> //for random.
#include <fstream> //for reading and writing to a file.
#include <string>

[Code] ....

it works correctly and generates a random number.

I'm using Linux Mint.

View 7 Replies View Related

C++ :: Creating A Text File For Randomly Generated Numbers

Apr 28, 2015

My code compiles, but it doesn't get past this:

Here's the code:
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;

//ofstream random_numbers("randIntContainer.txt");
ofstream random_numbers("D:/StudentData/Documents/DataStructures/SortingAlgorithms/randIntContainer.txt");
void generateNumbers();

[Code] ....

View 9 Replies View Related

C++ :: Read Multiple Text Files And Count Occurrences Of Word

Jun 8, 2013

I am supposed to read(scan) data from a folder with multiple(21578) text files, and file names are numbered from 1 to 21578,and read each word that occurs in a text file, and count the number of times it occurs in the entire folder,i.e; in all the files how do i go about it? I've tried this so far..but it isn't working..

#include <iostream>
#include <map>
#include <string>
using namespace std;
void incrementString(map<string, int> &theMap, string &theString) {
if(theMap.count(theString)) {

[Code] ....

View 1 Replies View Related

C++ :: How To Move A Ghost In PacMan Game Randomly Around The Maze

Oct 1, 2014

I have tried rand() for x and y positions of ghost but does not work. it moves but not randomly it follows same path every time I run the code.

View 2 Replies View Related

C++ :: How To Read A Specific Word From A File

Aug 12, 2014

How would one go about doing this? I am very fond with Windows I/O, but not so much with this subject. I am decent with fstream, probably not as skilled at it as I should be, but I assume <fstream> would be a better choice, since it isn't locked to Windows.

Anyway, say I have a .txt file like this:

Bill is a cow.
Bill likes meat.
Bob is a pig.
Bob and Bill are friends.

And I want to count the number of times "Bill" is written. How would I do that?

View 5 Replies View Related

C++ :: Remove Word From A Text File?

May 24, 2014

I have a C++ program that performs calculations on numbers in a text file. However, suppose we have additional words in the text file such as "Title". In this instance, the program will not calculate the numbers in the file because of the text.

How do we remove text from a file using the C++ program? e.g. if I wanted to specify the program to remove any instances of the word "Title", how would I do it?

View 1 Replies View Related

Visual C++ :: Getting A Word From A Text File?

Apr 26, 2013

im trying to have the nickanme set to be a random name from a text fille full of nickanmes i dont know how to do it heres my code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluorineFx;
using FluorineFx.Messaging.Adapter;

[code].....

View 3 Replies View Related

Visual C++ :: Error When Trying To Read A Random Word From A File

May 8, 2015

I am currently writing a password generator in Microsoft Visual Studios 2010 Professional. The section I am having a problem with is the practical password. It is suppose to randomly read 3 words from a text file and then display it in the text box. The program will compile and run but when I hit generate I get "True True True" and not three random words. Then this warning shows up:

Warning C4800: 'char *' : forcing value to bool 'true' or 'false' (performance warning)

//Code is from the form1.h file. I can post the rest of the code if need be but I just included my includes and the problematic section

Code:
#pragma once
#include <cstdlib>
#include <ctime>
#include <stdlib.h>
#include <cmath>
#include <iostream>
#include <iomanip>

[Code] ....

Screenshot of the warnings: [URL] .....

Picture of the output: [URL] ....

View 3 Replies View Related

C/C++ :: Sort Word Length From A Text File

Mar 23, 2014

I was asked to create a program to find word frequencies, word length in a text file. I am able to use map to do the frequencies part but don't know how to sort the the output accordingly to the length of the words. The example output that I should get is

1 a. 362
i. 157

2 an. 122
at. 201

3 add. 2
age. 1

int main(void) {
static const char* file = "demo.txt";
map<string, unsigned int> wcount; {
ifstream fileStream(file);

[Code] .....

View 10 Replies View Related

C++ :: Function To Find Sum Of Certain Word In Text File?

Jul 26, 2014

I've written a function that has to receive a word as parameter, and then has to find the amount of times that word (uppercase & lowercase) occurs in a text file:

For example, if the word "the" is sent as a parameter, the total amount of times the words "the" and "THE" occurs in the text file, has to be calculated.

However, my function is not displaying the correct amount, what is wrong with my function:

int WordCount :: countWords(string wrd)
{
int counter=0;
string temp = "";

[Code].....

View 1 Replies View Related

Visual C++ :: Counting A Word From Text File

Nov 24, 2012

The user will first enter the file name to be processed. The program can do the following 5 tasks:

1. Count the number of words
2. Count the number of alphabets (without punctuation marks)
3. Count number of sentences.
4. Count the frequency of each vowel.
5. Count frequency of the following three words individually: FAST, computer and engineering

There should be a proper menu through which the user can select the desired task. The program should continue until the user asks to terminate it.

Here is what i have done so far:

#include <iostream>
#include <cstring>
#include <fstream>
#include <cstdlib>
using namespace std;
int main(void)
{
int opt=0; //option number
ifstream fin;
string filename;

[Code]...

There are a few problems.

1- When I enter option 'q', an infinite loop starts.
2- The first time, the program gives the correct value. After that, each time it gives answer ZERO.
3- The last part is not working. I experimented something which failed. I don't know how to do the last part.

View 1 Replies View Related

C# :: Pick Up Random Word From Text File And Use It As Nick

Apr 26, 2013

I got this code here id like for it to pick up a random word from a text file and use it as the nick in this line

Code:
SetNick(GetRandomString(10));

how could i do it heres the full code for the client

Code: using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluorineFx;
using FluorineFx.Messaging.Adapter;

[Code] ....

View 8 Replies View Related

C++ :: Counting Number Of Occurrences Of A Word In Text File

Jul 27, 2014

I've been struggling with this for a while, I have to write a function that accepts a word (in my case the word is "the") and file pointer, and counts the number of times the given word appears (case insensitive) within the file associated with the pointer. This means the two words "the" and "THE" should both be counted.

Here is my code:

int WordCount :: countWords(string wrd) {
int counter=0;
string temp = "";
while (getline(*file,temp)) {
for (int i = 0; i < temp.length();i++) {

[Code] ....

This is what I have come up with, but I get an incorrect value. It was suggested to us to consider using the strcpy() or strstr() functions, but I don't know how to use them.

View 6 Replies View Related

C++ :: Counting Number Of Occurrences Of A Word In A Text File

Jul 27, 2014

I have to write a function that accepts a word (in my case the word is "the") and file pointer, and counts the number of times the given word appears (case insensitive) within the file associated with the pointer. This means the two words "the" and "THE" should both be counted.

Here is my code:

int WordCount :: countWords(string wrd)
{
string temp;
int counter;
while (!file->eof())
{
*file >> temp;
if (temp == wrd)

[Code]...

Unfortunately this does not work,

View 1 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved