Arrays and File I/O

This is a discussion on Arrays and File I/O within the C++ Programming forums, part of the General Programming Boards category; Hello, I am totally new to arrays. I'm posting this in the C++ forum because my game is a mediocre ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    43

    Arrays and File I/O

    Hello, I am totally new to arrays. I'm posting this in the C++ forum because my game is a mediocre console window and I guess that the solution to my array problem is simple. So, I wrote a simple "Guess the number" game and recently implemented a mediocre highscore system.

    Here's how it works:

    When the player gets a highscore, the program creates a text file that contains a string composed of the player's score and of the player's name (Format example : 06John). Before starting a new game, the program reads the previously created file with the use of arrays, and shows the player's name and score.

    Here's a snippet of my code showing the reading part:

    Code:
    char highscore[50];
    
    ifstream read ("Highscore.txt");
    read >> highscore;
    cout << "Highscore: " << highscore[2] << " in ";
    cout << highscore[0] << highscore[1] << " turns." << endl;
    Here's the text file:

    Code:
    06John
    And output:

    Code:
    Highscore: J in 06 turns.
    As you can see, the problem is that currently the player's name is one letter. Is there a way to read (with arrays) until the end of the player's name, starting from slot 2?

    I'm sure the solution is obvious.

    Thanks.
    Last edited by Darklighter; 02-11-2006 at 06:16 PM.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,655
    unsigned MAX = 256;
    char name[MAX + 1];
    read.getline(name, MAX);

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    In C++, you use string types to read in strings. A string type automatically adjusts it's size to handle whatever length string you need to read into it:
    Code:
    #include <iostream> 
    #include <string>
    using namespace std;
    
    int main()
    {
    	string str = "hello";
    	cout<<str<<endl;
    
    	str = "hello world";
    	cout<<str<<endl;
    	
    
    	return 0;
    }
    A string type can be treated like an array, with the first character at index 0 and the next character at index 1, etc. You can use the string function substr() to extract portions of the string you are interested in:
    Code:
    string str = "06John";
    	
    cout<<str.substr(0,2)<<endl;
    cout<<str.substr(2)<<endl;
    Last edited by 7stud; 02-11-2006 at 07:23 PM.

  4. #4
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    Code:
    char highscore[50];
    
    ifstream read ("Highscore.txt");
    read >> highscore;
    cout << "Highscore: " << &highscore[2] << " in ";
    cout << highscore[0] << highscore[1] << " turns." << endl;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File I/O and Arrays
    By John Gaden in forum C++ Programming
    Replies: 6
    Last Post: 08-15-2008, 02:46 PM
  2. File I/O with arrays
    By 20,000leeks in forum C++ Programming
    Replies: 32
    Last Post: 09-18-2006, 02:16 AM
  3. File i/o & int arrays
    By LordBronz in forum C++ Programming
    Replies: 2
    Last Post: 09-10-2006, 11:50 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. File I/O with 2D arrays
    By hypertension in forum C Programming
    Replies: 2
    Last Post: 02-04-2003, 07:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21