Post: C++ Code
09-13-2011, 06:41 PM #1
(adsbygoogle = window.adsbygoogle || []).push({}); Okay, I would like to know if anyone can post a code to get a question from a text file.
for example:
text file with questions
question 1
question 2
question 3
program grabs question 2
output question 2.


if anyone can help that'd be great.
Last edited by Docko412 ; 09-14-2011 at 03:25 AM. Reason: making more clear
09-13-2011, 09:35 PM #2
Epic?
Awe-Inspiring
Originally posted by D3ss3rtPr0ducts View Post
Okay, I would like to know if anyone can post a code to get a question from a text file.
for example:
txt file question?
program grab question
output question.

if anyone can help that'd be great.


    

//#include "stdafx.h" // uncomment this line if using visual studio
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
fstream reader;
reader.open("text.txt");
string output;

if (reader)
{
while (!reader.eof())
{
reader >> output;
cout << output;
}
}
else
{
cout << "File Does Not Exist\n";
return 1;
}

reader.close();
system("PAUSE");

return 0;
}


Not sure if that's what you're looking for, but it uses fstream to rad the content of a text file (called "text.txt") and prints the contents to the screen via a while loop. It also implements some basic error checking.

The program returns 1 on error, and 0 on success, it uses system just to pause the program so you can read the content.

Again, it simply prints the content of the text file. If this isn't what you're looking for, you're gonna have to be more specific.
Last edited by Epic? ; 09-13-2011 at 09:38 PM.
09-14-2011, 03:13 AM #3
Originally posted by AsianInvasion View Post
    

//#include "stdafx.h" // uncomment this line if using visual studio
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
fstream reader;
reader.open("text.txt");
string output;

if (reader)
{
while (!reader.eof())
{
reader >> output;
cout << output;
}
}
else
{
cout << "File Does Not Exist\n";
return 1;
}

reader.close();
system("PAUSE");

return 0;
}


Not sure if that's what you're looking for, but it uses fstream to rad the content of a text file (called "text.txt") and prints the contents to the screen via a while loop. It also implements some basic error checking.

The program returns 1 on error, and 0 on success, it uses system just to pause the program so you can read the content.

Again, it simply prints the content of the text file. If this isn't what you're looking for, you're gonna have to be more specific.


this is close, but i'm looking for like okay lets say i have a text file with questions in it
like
question 1
question 2
question 3
and say i wanted to get question 2 out of the text file and print just that out.
you know?
09-14-2011, 03:24 AM #4
Ritztro
I am a Game Developer
Originally posted by D3ss3rtPr0ducts View Post
this is close, but i'm looking for like okay lets say i have a text file with questions in it
like
question 1
question 2
question 3
and say i wanted to get question 2 out of the text file and print just that out.
you know?


Well what you could do is...
    #include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

int main()
{
fstream file;
file.open("questions.txt");
vector<string> questions;

if (file)
{
while (!file.eof())
{
string temp;
getline(file, temp);
questions.push_back(temp);
}
}
else
{
cout << "File Does Not Exist\n";
return 1;
}

for(int i = 0; i < questions.size(); i++) //Or you could just do cout << questions[numberOfQuestion] << endl;
cout << questions[i] << endl;

file.close();

return 0;
}

The following user thanked Ritztro for this useful post:

Docko412
09-14-2011, 03:32 AM #5
Originally posted by Ritztro View Post
Well what you could do is...
    #include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

int main()
{
fstream file;
file.open("questions.txt");
vector<string> questions;

if (file)
{
while (!file.eof())
{
string temp;
getline(file, temp);
questions.push_back(temp);
}
}
else
{
cout << "File Does Not Exist\n";
return 1;
}

for(int i = 0; i < questions.size(); i++) //Or you could just do cout << questions[numberOfQuestion] << endl;
cout << questions[i] << endl;

file.close();

return 0;
}

thank you sir, this is great.
now i'm going to just change it a bit, so it randomizes the output. then get to making my game.
thanks.
09-14-2011, 03:52 AM #6
Ritztro
I am a Game Developer
Originally posted by D3ss3rtPr0ducts View Post
thank you sir, this is great.
now i'm going to just change it a bit, so it randomizes the output. then get to making my game.
thanks.


Ya no problem Smile. If you have another question then feel free to message me or very soon you could "Contact Me" on my web site Smile
09-14-2011, 08:32 AM #7
Originally posted by Ritztro View Post
Ya no problem Smile. If you have another question then feel free to message me or very soon you could "Contact Me" on my web site Smile


is there any way you could make this a class for me or something along those lines?
cuz i need to call a random question from a text file. anyway?
09-15-2011, 01:06 AM #8
Ritztro
I am a Game Developer
Originally posted by D3ss3rtPr0ducts View Post
is there any way you could make this a class for me or something along those lines?
cuz i need to call a random question from a text file. anyway?


What do you mean, "make a class for this"? Do you mean like combine all the code into one question class or something? I think it would be easier to just do it how it is done right now.
09-15-2011, 06:19 AM #9
Originally posted by Ritztro View Post
What do you mean, "make a class for this"? Do you mean like combine all the code into one question class or something? I think it would be easier to just do it how it is done right now.


oh my bad, i figured it out, sorry. lol
but thanks anyway.

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo