Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

C++ Function help


  • Please log in to reply

#1
maliquenavidad

maliquenavidad

    New Member

  • Member
  • Pip
  • 2 posts
I'm tasked to create a fairly large program, and this is only one function within the program. The purpose is to pick a random word from a file, store the word, and display the correct number of "*s" as output. It's supposed to be for a hang man game.

Here is my code thus far. Sorry for not putting it in a code box, but I didn't see anything that told me how.

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

using namespace std;




int main()
{
int i, w,ran; //number of lines in file

string n, mar, log;//mar=*s log= word

srand((unsigned)time( NULL ));

ran = rand();
w= 4;

for(i = 0; i < 1; i++)
{
i = w * rand() / 32767 ;

cout << i +1 << endl;
}


ifstream infile;


infile.open ("word.txt");

if (!infile)
{ cout << "There is no file. Failure. " << endl;
return 1; //comes back as failure
}


getline(infile,


mar[i];

while (mar.find(" ") != string::npos )
{
mar[1];
cout<< "*";
mar[2];
cout<<"*";
mar[3];
cout<< "*";
mar[4];
cout<< "*";
mar[5];
cout<< "*";
mar[6];
cout <<"*";
mar[7];
cout<<"*";
mar[8];
cout<<"*";
mar[9];
cout<<"*";
}

cout<< mar;

infile.close();

return 0;

}
  • 0

Advertisements


#2
ricci

ricci

    Member

  • Member
  • PipPip
  • 64 posts
Hi maliquenavidad,

You can use code boxes by clicking the code button on the toolbar just above the typing area while you are entering your post. It has a # in it and is to the right of the bold, italics, underline, strike-through, etc buttons. Or you can just type [ c o d e ] (without spaces) to start a code box and [ / c o d e ] (without spaces) to end one.

As far as your code goes, I'm not clear on what your question is. If you would post some specific questions about what you're having trouble with, I (or someone) would be happy to help you out.

-Ricci
  • 0

#3
maliquenavidad

maliquenavidad

    New Member

  • Topic Starter
  • Member
  • Pip
  • 2 posts
My question falls in this area. I want to use the random number (1-4) to pick a word out of a file(There are 4 words in the file). I'm pretty sure I need to use getline for this, but I don't know how to go to the random line.

After I go to that line, I want to put the word on the line in a string. Then, using a loop, I want to count how many characters are in the word and replace those characters using a new string with "*"s in place of the characters.

for(i = 0; i < 1; i++)
{
i = w * rand() / 32767;

cout << i +1 << endl;
}


ifstream infile;


infile.open ("word.txt");

if (!infile)
{ cout << "There is no file. Failure. " << endl;
return 1; //comes back as failure
}


getline(infile,


  • 0

#4
ricci

ricci

    Member

  • Member
  • PipPip
  • 64 posts

My question falls in this area. I want to use the random number (1-4) to pick a word out of a file(There are 4 words in the file). I'm pretty sure I need to use getline for this, but I don't know how to go to the random line.

In order to find a specific line in a file, you're going to have to read through the file and count the lines. Files are not organized by line on a disk, so you can't just read a certain line from a file. The getline function gets the rest of the current line and puts it into a character array. So, if you want to read the third line, for example, then you use getline to read the first line, then use getline to read the second line, then use getline to read the third line. This would be a great use for a "for" loop.

-Ricci

Edited by ricci, 29 November 2005 - 03:36 PM.

  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP