Thread: File I/O help

  1. #1
    Registered User
    Join Date
    May 2006
    Location
    New York
    Posts
    5

    File I/O help

    I have just a quick question that someone could hopefully solve for me in a matter of seconds. Below is a text based trivial pursuit program (as of right not there isn't much too it, but i need to fix this problem first).

    The problem lays within the case 'y' of the switch structure. When the user types in his or her name, instead of showing in the file a blank space shows. I have tried everything, changing the dummy string, taking it out, moving things around, nothing seems to work. Can someone take a look for me?


    Code:
    // Brandon Blincoe
    // Trivial Pursuit
    // The game of trivial pursuit using files storing data
    // CREDITS
    
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    #include <string>
    #include <assert.h>
    
    using namespace std;
    using std::string;
    
    int main()
    {
    	string name;
    	string dummy = "";
    	char user_entry;
    	char answer;
    	int user_entry_2 = 0;
    	int counter = 1;
    	int player_count = counter;
    	ofstream responses;
    
    	cout << "Trivial Pursuit\n\n";
    	cout << "Would you like to begin? <Y/N>";
    	cin >> user_entry;
    	responses.open("RESPONSE.DAT", ios::out);
    	assert(!responses.fail());
    	do
    	{
    		switch(user_entry)
    		{
    		case 'Y':
    		case 'y':
    			cout << "Player " << counter << ": ";
    			getline(cin, name);
    			responses << name << "\n";
    			getline(cin, dummy);
    			cout << "Add another player? <Y/N>";
    			cin >> user_entry;
    			counter++;
    			break;
    		case 'N':
    		case 'n':
    			system("CLS");
    			while(player_count < counter)
    			{
    				if(player_count == 1) // begins questions for player 1
    				{
    					cout << "Player " << player_count << " begin.\n";
    					cout << "Question 1:\n";
    					cout << "When x squared minus four is factored, what is one value of x?\n";
    					cout << "a) (1)";
    					cout << setw(20) << "b) (2)\n";
    					cout << "c) (-4)";
    					cout << setw(19) << "d) (4)\n";
    					cout << "Your Answer is: ";
    					cin >> answer;
    					cout << "\n\n";
    					responses << answer << "\n";
    					cout << "Question 2:\n";
    					cout << "Why doesn't oil and water(H20) mix?\n";
    					cout << "a) Molecular Formulas differ";
    					cout << setw(29) << "b) They don't feel like it\n";
    					cout << "c) Like dissolves like";
    					cout << setw(22) << "d) Who cares?\n";
    					cout << "Your Answer is: ";
    					cin >> answer;
    					responses << answer << "\n";
    					player_count++;
    				}
    				else
    				{
    					system("CLS");
    					cout << "Player " << player_count << " begin.\n";
    					cout << "Question 1:\n";
    					cout << "When x squared minus four is factored, what is one value of x?\n";
    					cout << "a) (1)";
    					cout << setw(20) << "b) (2)\n";
    					cout << "c) (-4)";
    					cout << setw(19) << "d) (4)\n";
    					cout << "Your Answer is: ";
    					cin >> answer;
    					cout << "\n\n";
    					responses << answer << "\n";
    					cout << "Question 2:\n";
    					cout << "Why doesn't oil and water(H20) mix?\n";
    					cout << "a) Molecular Formulas differ";
    					cout << setw(29) << "b) They don't feel like it\n";
    					cout << "c) Like dissolves like";
    					cout << setw(22) << "d) Who cares?\n";
    					cout << "Your Answer is: ";
    					cin >> answer;
    					responses << answer << "\n";
    					player_count++;
    				}
    			}
    			cout << "Congratulations! Your done! <PRESS 1 to QUIT>";
    			cin >> user_entry_2;
    			break;
    		}
    	}
    	while(user_entry_2 < 1);
    	system("CLS");
    	responses.close();
    	assert(!responses.fail());
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Why do you use the switch statement lik ethat when you could just use if/else?

    And what is the getline dummy for?

  3. #3
    Registered User
    Join Date
    May 2006
    Location
    New York
    Posts
    5
    Code:
    getline(cin, dummy);
    swallows the whitespace that is created when using getline . I used getline to "get" the players name, then from there the name should print in the file created (RESPONSES.DAT) but it just shows as blank space. If I take getline dummy out it skips a cin. Run it and mess with it to see what I mean.

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Have you tried it with a text file instead?

    I've never actually used a DAT file for a string

    You don't need the std::string either as it is covered by using namespace std;

  5. #5
    Registered User
    Join Date
    May 2006
    Location
    New York
    Posts
    5
    I just tried using a text file, still the same result. Thank you for the insight on the std::string, I did not realize i didn't need it. Ill keep working at this, let me know if you come up with something. Thanks for the input.

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    What exactly does assert do as that is not what I use when doing file i/o

  7. #7
    Registered User
    Join Date
    May 2006
    Location
    New York
    Posts
    5
    Code:
    assert();
    would be the same thing as saying if(filename) such that the file opened correctly else the file failed to open. Assert just takes less hassle typing in those if/else statments.

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    ofstream responses("RESPONSES.DAT");

    Edit: With ofstream you don't need to make sure that teh file is open as it creates it if it isn't.

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Well I have got it to work now...

    but get rid of the dummy and put:

    Code:
    cin >> user_entry;
    cin.ignore();

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by Blincoe
    Code:
    assert();
    would be the same thing as saying if(filename) such that the file opened correctly else the file failed to open. Assert just takes less hassle typing in those if/else statments.
    Then why not just do that? If I remember correctly, assert does nothing after you define a macro to turn off debugging. That's not good enough, one day you might not be able to open the file and your program explodes.

  11. #11
    Registered User
    Join Date
    May 2006
    Location
    New York
    Posts
    5
    Bumfluff thanks for the help. I got it all working, I appreciate it. Thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. 2 questions surrounding an I/O file
    By Guti14 in forum C Programming
    Replies: 2
    Last Post: 08-30-2004, 11:21 PM
  4. File I/O problems!!! Help!!!
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 05-17-2002, 08:09 PM
  5. advice on file i/o
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-29-2001, 05:56 AM