#include <fstream.h>

This is a discussion on #include <fstream.h> within the C++ Programming forums, part of the General Programming Boards category; When i saved some information, it all ended up into one variable. Here is the code: #include <iostream.h> #include <conio.h> ...

  1. #1
    Flash Animator, OF DOOOOM bluehead's Avatar
    Join Date
    Nov 2001
    Posts
    269

    #include <fstream.h>

    When i saved some information, it all ended up into one variable.
    Here is the code:

    #include <iostream.h>
    #include <conio.h>
    #include <stdlib.h>
    #include <fstream.h>

    char username[10];
    int health;
    int strength;
    int xp = 0;

    int main()
    {
    cout<<"Username>";
    cin>>username;

    ifstream input(username);

    if (input)
    {
    cout<<"Sorry, username used.";
    }
    if (!input)
    {
    input.close();
    int points = 10;
    cout<<"You have 10 points- How much you want in strength?"<<endl;
    cout<<"Strength= ";
    cin>>strength;
    if (strength > points)
    {
    cout<<"sorry. Impossible move!";
    getch();
    exit(0);
    }
    points = points - strength;
    cout<<"You have "<<points<<" health.";
    health = points;

    ofstream output(username);
    output<<health;
    output<<strength;
    output<<xp;
    output.close();
    }
    getch();
    return 0;
    }

    That makes the variables. Now to show them:

    #include <iostream.h>
    #include <conio.h>
    #include <fstream.h>

    char usrnm[10];
    int xpmin;
    int xpmax;
    int lvl;
    int strth;
    int def;
    int hlth;

    int main()
    {
    cout<<"Username>";
    cin>>usrnm;

    ifstream input(usrnm);

    if (!input)
    {
    cout<<"No such username";
    }
    else if (input)
    {
    input>>xpmin;
    input>>xpmax;
    input>>lvl;
    input>>strth;
    input>>hlth;
    input>>def;
    cout<<"level "<<lvl<<endl;
    cout<<xpmin<<"/"<<xpmax<<" xp"<<endl;
    cout<<def<<" defense \n";
    cout<<strth<<" strength \n";
    cout<<hlth<<" total health \n";
    }
    getch();
    return 0;
    }

    I may have used stuff you guys dont usually use, but just tell me why instead of showing the diferant variables, it ends up the numbers are in hlth only. in other words:

    when you enter like "def = 10" "strth=5" "hlth=5", then how come when you compile it it says:

    level 0
    0/0
    0 defense
    0 strength
    155 health

    that is what it is, maybe you can help maybe not, but please try
    Code:
    #if _emo
      #define stereo_type_i_dislike
    #endif

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    3
    Here is my shoot at this one.

    You have:

    points = points - strength;
    cout<<"You have "<<points<<" health.";
    health = points;


    change this to;


    Points = strength - points;
    cout<<"you have "<<points<<"health.";
    health=points

  3. #3
    Flash Animator, OF DOOOOM bluehead's Avatar
    Join Date
    Nov 2001
    Posts
    269
    Thats not what im asking for but i'll try it
    Code:
    #if _emo
      #define stereo_type_i_dislike
    #endif

  4. #4
    Unregistered
    Guest
    files store data, but they aren't mindreaders. Inparticular, look at the following lines:

    ofstream output(username);
    output<<health;
    output<<strength;
    output<<xp;

    Lets say health = 1; strength = 4 ; xp = 0.

    The data will appear in the file as:

    140

    Why?

    Because you don't delimit one piece of data from another. A space is often used to delimit, but you could use a comma, or any other value you wish, say a newline char, whatever. So do something like this:

    ofstream output(username);
    output << health << endl;
    output << strength << endl;
    output << xp << endl;

    If you don't delimit the data and you then read the file back the file will think the first data item is 140 rather than the desired effect that 1 is the first data itme, 4 is the second data item, and 0 is the last data item.

    BTW don't use input to read in xpmin if you don't write xpmin to the file to begin with. If you had separated the three data items using a delimiter and you read the file back to the second program the first data item will go into xpmin instead of health or whereever it started from, the second will go into xpmax instead of into strenth, and the third will go into lvl instead of xp.

  5. #5
    Flash Animator, OF DOOOOM bluehead's Avatar
    Join Date
    Nov 2001
    Posts
    269
    Lol, that was a code, and i forgot to add it, so i made another .exe file that adds your xp <<lol>>
    Code:
    #if _emo
      #define stereo_type_i_dislike
    #endif

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2d game
    By JordanCason in forum Game Programming
    Replies: 5
    Last Post: 12-08-2007, 09:08 PM
  2. MFC include BS
    By VirtualAce in forum Windows Programming
    Replies: 4
    Last Post: 10-31-2005, 11:44 AM
  3. dont konw what to do next.
    By negevy in forum C Programming
    Replies: 29
    Last Post: 09-09-2005, 03:06 PM
  4. #includes don't seem to be working
    By Inquirer in forum C++ Programming
    Replies: 12
    Last Post: 08-05-2002, 05:38 PM
  5. MFC Assertion Failure
    By maxthecat in forum Windows Programming
    Replies: 5
    Last Post: 08-01-2002, 09:58 AM

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