CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Join Date
    Mar 2010
    Posts
    46

    confused with I/O

    This is an add on of my post earlier. My game has 5.txt files. Each one has 10 different facts about that specific category. When the user selects a category it goes into a switch and I want to assign each switch to a specific text file, open the text file and print one fact to the screen. I know how to get data from one .txt manipulate it and output it to another but I just want the program to open the file and print one fact to the screen then ask the user if they want another fact. I am confused about how to do this because when i use

    cout it just outputs what i put in the parenthesis. what am i missing here.

  2. #2
    Join Date
    Aug 2008
    Posts
    902

    Re: confused with I/O

    Quote Originally Posted by AKGROWN View Post
    This is an add on of my post earlier. My game has 5.txt files. Each one has 10 different facts about that specific category. When the user selects a category it goes into a switch and I want to assign each switch to a specific text file, open the text file and print one fact to the screen. I know how to get data from one .txt manipulate it and output it to another but I just want the program to open the file and print one fact to the screen then ask the user if they want another fact. I am confused about how to do this because when i use

    cout it just outputs what i put in the parenthesis. what am i missing here.
    Did you consider using XML instead? You could place all of your categories and and questions in one file, and let the program take care of generating the category options.

  3. #3
    Join Date
    Mar 2010
    Posts
    46

    Re: confused with I/O

    no i havn't only because we have not learned anything about xml files in school. I am not even sure what they are or what they do, but I think I came up with a better idea. It may involve more typing but it alleviates alot of the problems I am facing. All i have to figure out now is how to write the loops. thanks for the input.

  4. #4
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: confused with I/O

    AKGROWN, you have 5 text files or 10 text files? Or you have 10 facts in each text file? Can you clear that thing out?

    And how to you actually "cout" this thing? What about parenthesis - did you mean quotes instead? Post some code.
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  5. #5
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: confused with I/O

    Quote Originally Posted by Chris_F View Post
    Did you consider using XML instead? You could place all of your categories and and questions in one file, and let the program take care of generating the category options.
    The original poster is a naive programmer, and is using C++. Asking him to use XML would be over burden. How can you parse the XML file?
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  6. #6
    Join Date
    Mar 2010
    Posts
    46

    Re: confused with I/O

    I can post what I have but it wouldnt really make any sense as of yet. There is there are 5 text files

    theUniverse.txt
    facts 1-10
    Sports.txt
    facts 1-10
    usHistory.txt
    facts 1-10
    Movies.txt
    facts 1-10
    videoGames.txt
    facts 1-10

    if I choose

    A: The universe

    how do i get it to open that file and print one fact to the screen. After the fact prints I am going to do a while loop so that it asks the user if he/she wants another fact; if yes
    print the next fact in the list to the screen if no escape back to main menu.

    here is what I have so far please dont flame or laugh it is a work in progress

    Code:
    #include <iostream>
    #include <fstream>
    #include "triviaFunc.h";
    
    using namespace std;
    
    int main()
    
    
    {
    	ifstream inFile;
    	ofstream outFile;
    	char category;
    	char getFact;
    	
    	cout << "Welcome to the TRIVIA TRAINER! This application will help you prepare" << endl;
    	cout << "for trivia. Choose one of the categories and a fact about your choosen" <<endl;
    	cout << "category will appear on the screen." << endl;
       
    	cout <<"Please choose a Trivia Trainer category." << endl;
    	cout <<"A: The Universe" << endl;
    	cout <<"B: U.S. History" << endl;
    	cout <<"C: Video Games" << endl;
    	cout <<"D: Movies" << endl;
    	cout <<"E: Sports" << endl;
    	cin >> category;
    	cout <<"You choose to learn about category: " << category << endl;
    
    	switch (category)
    	{
    case 'A':
    	cout << "would you like to hear a fact about the universe? enter yes or no: " << endl;
    	cin >> getFact;
    	if 
    		(getFact==yes)
    		inFile.open("theUniverse.txt");
    	cout
    	
    
    	else if
    		(getFact==no)
    		break:
    		
    
    case 'B' 'b':
    case 'C' 'c':
    case 'D' 'd':
    case 'E' 'e':
    
    	break;
    default:
    	cout << "ERROR INVALID SELECTION, PLEASE CHOOSE A CATEGORY LISTED." << endl;
    	}
    		
    
    	return 0;
    
    }
    ignore the triviaFunc.h. I am not sure if I will need to use a function or not and if I do I am not sure how I am going to write it yet.

  7. #7
    Join Date
    Mar 2010
    Posts
    46

    Re: confused with I/O

    Could someone help me out please!

  8. #8
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: confused with I/O

    Actually no one would be interested in your homework

    First learn the syntax of switch and cout. What is this?
    Code:
    case 'B' 'b':
    case 'C' 'c':
    case 'D' 'd':
    case 'E' 'e':
    and this?
    Code:
    if 
    		(getFact==yes)
    		inFile.open("theUniverse.txt");
    	cout
    	
    
    	else if
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  9. #9
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: confused with I/O

    Also ...

    the variable yes is not declared
    the variable no is not declared


    You ask the user to enter yes or no into the variable getFact, but
    getFact only has room for a single character.

  10. #10
    Join Date
    Mar 2010
    Posts
    46

    Re: confused with I/O

    Quote Originally Posted by Ajay Vijay View Post
    Actually no one would be interested in your homework

    First learn the syntax of switch and cout. What is this?
    Code:
    case 'B' 'b':
    case 'C' 'c':
    case 'D' 'd':
    case 'E' 'e':
    and this?
    Code:
    if 
    		(getFact==yes)
    		inFile.open("theUniverse.txt");
    	cout
    	
    
    	else if
    This is what I am asking about because I do not understand what it is I need to do here. If i understood and knew how to do it correctly i wouldnt be asking for help. Im not asking you to wirte my code only point me in the right direction.

    I changed it up a bit and declared variable yes and no but I am still stuck here is the changes I made.
    Code:
    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    
    using namespace std;
    
    int main()
    
    	ifstream infile;
    	int category=0;
        bool yes = true;
    	bool no = false;
    
    	cout << "Welcome to the TRIVIA TRAINER! This application will help you prepare" << endl;
    	cout << "for trivia. Choose one of the categories and a fact about your choosen" <<endl;
    	cout << "category will appear on the screen." << endl;
       
    	cout <<"Please choose a Trivia Trainer category." << endl;
    	cout <<"1: The Universe" << endl;
    	cout <<"2: U.S. History" << endl;
    	cout <<"3: Video Games" << endl;
    	cout <<"4: Movies" << endl;
    	cout <<"5: Sports" << endl;
    	cin >> category;
    	cout <<"You choose to learn about category: " << category << endl;
    
    	switch (category)
    	{
    case '1':
    	cout << "would you like to hear a fact about the universe? enter yes or no: " << endl;
    	cin >> getFact;
    	if (getFact == yes)
    		infile.open("theUniverse.txt");
    	inFile >> ?????? This is where I am stuck, I do not understand how to get it to read one line from the text file once the file is open.

  11. #11
    Join Date
    Mar 2010
    Posts
    46

    Re: confused with I/O

    Do I need to declare a variable for every fact that will be read from the file

    like

    string fact1, fact2.......fact10?

  12. #12
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: confused with I/O

    Start with basic program. Know how and why to declare variable.

    Learn what if-else, switch constructs are.

    Learn, separately, how to read and write file contents.

    And then, do what you are trying to do?


    Feel offended? We also feel irritated when people like you, without trying anything on their own, put the question here. And think they just waved the magic-wand, and expect perfect reply. This is real world!
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  13. #13
    Join Date
    Mar 2010
    Posts
    46

    Re: confused with I/O

    offended no, but remember you wernt always a programming genuis either. All I asked was for help not a read the f$$$$$$ manual post. I have been trying to figure this out for 4 days before I even came here for help. If you dont want to help then dont post, simple as that. I never asked you to write my code or do my homework, all I asked for was help understanding what I am doing wrong.

  14. #14
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: confused with I/O

    Quote Originally Posted by Ajay Vijay View Post
    The original poster is a naive programmer, and is using C++. Asking him to use XML would be over burden. How can you parse the XML file?
    Well, libxml2 isn't very hard to use.

  15. #15
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: confused with I/O

    Quote Originally Posted by AKGROWN View Post
    offended no, but remember you wernt always a programming genuis either. All I asked was for help not a read the f$$$$$$ manual post. I have been trying to figure this out for 4 days before I even came here for help. If you dont want to help then dont post, simple as that. I never asked you to write my code or do my homework, all I asked for was help understanding what I am doing wrong.
    No. I wasn't a genuis, I was idiot - but I gave effort, hardwork in that, and not just posted these petty stuff on forurms to get the reply.

    Your level of programming knowledge forced me to write all that. What if I or anyone would give you perfect solution? You'd be happy to get the solution. But how much you would have learned? Only by hit-and-trial you'd learn more than you'd learn by reading the some stuff. Remember, programming is an art, and is only learnt by practice.

    And please, don't attempt to write those words.
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured