» »

problemi z export file c++

problemi z export file c++

kretze ::

dober večer situacija je naslednja delam program hangman(vislice) in sicer ustvaril sem si txt file in ko ga hočem exportat v kodo visual studia da bi mi izpisal random besedo mi je enostavno ne izpiše...mogoče kdo ve zakaj??? spodaj prilagam sliko od tega txt file-a in pa seveda kodo...najbolj me zanima če imam funkcijo LoadRandomWord dobro napisano....pa hvala vsem za odgovore


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

void printMessage(string msg, bool printTop=true, bool printBotom=true)
{



	if (printTop)
	{
		cout << "+----------------------------------------------+" << endl;
		cout << "|";

	}
	else
	{
		cout << "|";
	}

	bool front = true;

	for (int i = msg.length(); i < 46; i++)
	{
		if (front)
		{
			msg = " " + msg;
		}
		else
		{
			msg = msg + " ";
		}
		front = !front;
	}
	cout << msg.c_str();

	if (printBotom)
	{

		cout << "|" << endl;
		cout << "+----------------------------------------------+" << endl;

	}
	else
		cout << "|" << endl;
}

void DrawHangMan(int guescount = 0)
{
	if (guescount >= 1)
	
		printMessage("|", false, false);
	
	else
		printMessage("", false, false);


	if (guescount >= 2)

		printMessage("|", false, false);

	else
		printMessage("", false, false);


	if (guescount >= 3)

		printMessage("O", false, false);

	else
		printMessage("", false, false);



	if (guescount == 4 )

		printMessage("/  ", false, false);

	

	if (guescount == 5)

		printMessage("/| ", false, false);

	


	if (guescount >=6)

		printMessage("/|\\", false, false);

	else
		printMessage("", false, false);


	if (guescount >= 7)

		printMessage("|", false, false);

	else
		printMessage("", false, false);


	if (guescount == 8)

		printMessage("/", false, false);

	

	if (guescount>= 9)

		printMessage("/ \\", false, false);

	else
		printMessage("", false, false);
}

void PrintLetters(string inputt, char from, char to)
{
	string s;
	for (char i = from; i <= to; i++)
	{
		if (inputt.find(i) == string::npos)
		{
			s += i;
			s += " ";
		}
			
		else
			s += "  ";
		
	}
	printMessage(s, false, false);
}

void PrintAvailibleLetters(string taken)
{
	printMessage("availible letters");
	PrintLetters(taken, 'A', 'M');
	PrintLetters(taken, 'N', 'Z');


}

bool printWordAndChechWin(string word, string guessed)
{
	bool won = true;
	string s;
	for (int i = 0; i < word.length(); i++)
	{
		if (guessed.find(word[i]) == string::npos)
		{
			won = false;
			s += "_ ";
		}
		else
		{
			s += word[i];
			s += " ";
		}

		
	}
	printMessage(s, false);
	return won;
}

string LoadRandomWord(string path)
{
	int linecount = 0;
	string word;
	vector<string> v;
	ifstream reader(path);
	if (reader.is_open())
	{
		while (std::getline(reader, word))
			v.push_back(word);

		int randomLine = rand() % v.size();
		word = v.at(randomLine);
		reader.close();
	}
	return word;
}




int main() {
	srand(time(0));
	string gueses = "ABHKIJKLL";
	string wordToGuess;
	wordToGuess=LoadRandomWord("words.txt");
	cout << wordToGuess << endl << endl;
	printMessage("HANGMAN IS COOL",true,true);
	DrawHangMan(9);
	PrintAvailibleLetters(gueses);
	printMessage("Guess the word");
	printWordAndChechWin("ALEXES", gueses);



	system("pause");// 
	return 0;
}

kretze ::

pa še slika txt file
 txt file

txt file

kow ::

Ne narobe razumeti, ampak -> kaj ce bi ti prvo opisal, kako razumes kodo funkcije. Vrstico za vrstico.
To je namrec tipicna metoda za resevanje problemov. Pa pustimo debugger na miru trenutno.

kretze ::

saj te ne razumem narobe...delal sem na tem programu pa sem pol samo skopiral brez da bi skomentiral program...v bisrvu me najbolj zanima funkcija LoadRandomWord ker mislim da bi morala bit dobro napisna ampak ne deluje najbolje...pol še pa me zanima ta stavek : wordToGuess=LoadRandomWord("words.txt");...saj mi ne exporta txt fila kot bi mi ga moralo

kow ::

Funkcija LoadRandomWord ima 1 parameter tipa string. Ki je relativna pot do datoteke words.txt. Vrne pa string, ki ga assignas spremenljivki wordToGuess.

Ne razumem pa, kaj naj bi zate pomenilo"exportati txt file".

kretze ::

pač txt file mi ne bere dobro ko zaženem program....ker bi se mi zgoraj morala pojavit ena od besed ki jim mam v tem file-ju....meni pa nič ne izpiše

sem pa najverjetneje naredil napako ko sem rekel exportat file....moral bi napisat inportat file

Zgodovina sprememb…

  • spremenilo: kretze ()

kow ::

"odpreti" file.

To se zgodi v stavku:
ifstream reader(path);

Preveri, ce funkcija http://www.cplusplus.com/reference/fstr... vrne true.
Torej napisi nekaj na konzolo znotraj naslednjega if stavka.


Vredno ogleda ...

TemaSporočilaOglediZadnje sporočilo
TemaSporočilaOglediZadnje sporočilo
»

C++ palindrom program

Oddelek: Programiranje
91418 (1138) mallard
»

[C] - spreminjanje atributov datotek

Oddelek: Programiranje
12962 (847) pijavka
»

Pomoč pri programiranju v C++

Oddelek: Programiranje
141723 (1290) amacar
»

Pomoč pri nalogi v C++

Oddelek: Programiranje
51292 (1292) bozjak
»

win api (c++)

Oddelek: Programiranje
462409 (1689) Gundolf

Več podobnih tem