Thread: Arrays and File I/O

  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 07:16 PM.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    unsigned MAX = 256;
    char name[MAX + 1];
    read.getline(name, MAX);
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    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 08:23 PM.

  4. #4
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    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, 08:47 AM