Hello. So the most I can tell you is i'm having serious trouble with my code of writing to a txt file. It's not that my code isn't working, it's that c++ is outputting WEIRD crap that makes no logic sense, doing things out of order, and just seriously screwing things up.


Would someone please take a look at the following code and see if it looks right to you?

ofstream userfile;
  userfile.open ("data/users/users.dat");
  userfile << number_of_users;
  
    for (int i = 0; i < number_of_users; i++)
  {
      userfile << "\n" << users[i] << "\n" << UserDifficulty[i];
      } 
  
  userfile.close();

and how about this code...does this next part look right to you?

ifstream file ("data/users/users.dat");
    if (file.is_open())
    {
    while (! file.eof() )
    {
     getline (file,number_of_users1); 
     istringstream buffer(number_of_users1);
    buffer >> number_of_users;
    
    for(int i = 0; i < number_of_users; ++i)
    {
    getline(file,users[i]);
    getline(file,UserDifficulty[i]);
            }
    
    }
    file.close();
    }

Please please please help! I cant figure it out. Thanks :) I'm so frustrated

Recommended Answers

All 18 Replies

Does the output from your first section of code come out correctly? See if you can pinpoint where in the process the garbage is printed. If the output for that first section is not correct check and make sure your arrays are holding that many elements.

it's that c++ is outputting WEIRD crap that makes no logic sense, doing things out of order, and just seriously screwing things up.

C is WYCIWYG (C stands for Code)
Did you input users & other data?

The output is not pure garbage, it just isnt formatting it as I ask it to. Sometimes, it will skip random lines and put the info down below. sometimes it wont even print the user names at all. just the number of users and then spaces. I think its the for loop in the first code that isnt working at all like it should be. I've rewritten it into a make-shift code. check it out. (weird)

ofstream userfile;
  userfile.open ("data/users/users.dat");
  userfile << number_of_users << "\n";
  
for (int i = 0; i < number_of_users; i++)
  {
      userfile << users[i] << "\n" << UserDifficulty[i] << "\n";
      } 
 
   userfile << users[number_of_users] << "\n";
   userfile << UserDifficulty[number_of_users] << "\n";       
 
  userfile.close();

what it will print out is correct-ish sometimes, but for some reason its not using the variables from the for loop. only the variables from the stuff below it. but if I take out the for loop, it only displays one user and one difficutly. but if i take out the stuff below the for loop, it only displays the number of users, and a bunch of spaces.

any ideas?

It maybe due to bugs in other parts of your program. Can you post in the part where you input the values and/or complete code.

userfile << users[number_of_users] << "\n";
userfile << UserDifficulty[number_of_users] << "\n";

You are one past the end on these arrays.

I agree with nbaztec there is probably something going on when you are entering the values especially if the output is not consistently wrong.

"You are one past the end on these arrays."
What does that mean?

And I would post the whole code, but I'm not sure if that would help or just confuse. I'll post some snippet that I think have to do with this feature in my program and maybe someone can help me out by spotting what may be wrong.

//lets start to rewrite the users.dat file because we are now done creating a new user
number_of_users = ++number_of_users;
users[number_of_users] = FinalUsername;
UserDifficulty[number_of_users] = difficulty;


  ofstream userfile;
  userfile.open ("data/users/users.dat");
  userfile << number_of_users << "\n";
  
for (int i = 0; i < number_of_users; i++)
  {
      userfile << users[i] << "\n" << UserDifficulty[i] << "\n";
      } 
 
   userfile << users[number_of_users] << "\n";
   userfile << UserDifficulty[number_of_users] << "\n";       
 
  userfile.close();
//this loads up the users data and puts them into variable settings
    ifstream file ("data/users/users.dat");
    if (file.is_open())
    {
    while (! file.eof() )
    {
     getline (file,number_of_users1);  //first lets retrive the number of users in this file
     istringstream buffer(number_of_users1);
    buffer >> number_of_users;
    
    for(int i = 0; i < number_of_users; ++i)
    {
    getline(file,users[i]);
    getline(file,UserDifficulty[i]);
            }
    
    }
    file.close();
    }
//end load up
//variable setup
int x = 0;
int y = 0;
int screen_x = 1024;
int screen_y = 768;
int percent = 0;
string users[11];
string UserDifficulty[11];
string number_of_users1;
int number_of_users;

Does that give any insight?

>>>>"You are one past the end on these arrays."
>>What does that mean?
Jonsca may have a point here.
Consider this:

int numberOfUsers=5;
char *name = char[numOfUsers]; //char *name = char[5] : Index 0-4
std::cin>>name[0];  //1st name
std::cin>>name[1];  //2nd name
std::cin>>name[4];  //5th name
std::cin>>name[5];  //Error: Index is out of Bounds.
number_of_users = ++number_of_users;

You are incrementing number_of_users before hand. For it to work properly number_of_users should've been set to (-1), which in fact will defeat the very purpose of that variable(As it should've been set to 0). What have you initialized it to(if at all).

Edit:

for(int i = 0; i < number_of_users; ++i)

From this I get that you are using number_of_users as it is. But in the above quoted code you increment it before filling the array. The first array position starts from 0 & going by the above method you will surely exceed the array index whereas first index of array(0) remains un-filled, which just might explain your garbage output.

number_of_users = ++number_of_users;

You are incrementing number_of_users before hand. For it to work properly number_of_users should've been set to (-1), which in fact will defeat the very purpose of that variable(As it should've been set to 0). What have you initialized it to(if at all).

It is initialized to nothing at first (which I guess means a defult zero?) but when the code opens users.dat, it is set to whatever the first line is. Are you saying that I sould move my "number_of_users = ++number_of_users;" to below the code? or should I set that variable -1? If I set it to -1 it would just be reset when the program reads users.dat

Also, doesn't it need to be incremented before all of this? because at the point of where I increment it, the player has just finished creating a new user, thus the count should go up before the file is written?

>>It is initialized to nothing at first (which I guess means a defult zero?)
Don't count on it unless you've declared it as static or extern (which I doubt).

>>Are you saying that I sould move my "number_of_users = ++number_of_users;" to below the code?
Yes.

>>or should I set that variable -1?
No. Set it to an obvious 0.

EDIT:

Also, doesn't it need to be incremented before all of this? because at the point of where I increment it, the player has just finished creating a new user, thus the count should go up before the file is written?

Depends on your logic. As I've stated earlier you'd have to assign the counter a vaule of -1 which would defeat the purpose of the counter(It being a logical counter). If you'd request number_of_users before hand it should return 0 (which is more obvious) rather than -1, don't you think.

(which I guess means a defult zero?)

Unfortunately it can be just about any value that is sitting in that memory location. Visual C++ initializes some values in Debug mode but it's a much better idea to initialize. When using a counter it is usually set to 0.

Increment number_of_users after you have assigned the array values. That way you are starting at 0 and putting your values into position 0.

Depends on your logic. As I've stated earlier you'd have to assign the counter a vaule of -1 which would defeat the purpose of the counter(It being a logical counter). If you'd request number_of_users before hand it should return 0 (which is more obvious) rather than -1, don't you think.

Well I get what your saying. I'm just trying to wrap my brain around it. I changed the increment to after everything. but what happened was after attempting to create a new user, I checked out the .dat file, and all it said was "0", which I guess means that NumberOfUsers started at zero, so the for loop didnt run at all.

I would say just start the NumberOfUsers to one so the for loop would go around, but of course that would mess up all the other things.

>>after attempting to create a new user, I checked out the .dat file, and all it said was "0", which I guess means that NumberOfUsers started at zero, so the for loop didnt run at all.
I meant increment the counter after filling the array.

Oh oh oh oh oh oh, hahaha.

okay, yea that fixed everything. Thank you so much man. I hate these tiny errors that come up in my code that I always overlook. I hope its not just me who this happen too. lol

Glad to be of your help. Mark this as solved.

Looks like nbaztec and jonsca sorted your problems out.
However, I'd like to point out a basic thing that you've been doing wrong, namely "number_of_users = ++number_of_users".

Do not write code like that, see comp.lang.c FAQ list · Question 3.3

commented: Yup +4

Looks like nbaztec and jonsca sorted your problems out.
However, I'd like to point out a basic thing that you've been doing wrong, namely "number_of_users = ++number_of_users".

Do not write code like that, see comp.lang.c FAQ list · Question 3.3

Whoa! Can't believe I missed that one. Thanks for pointing that out.
@OP i++ will increment i & stores the value back. What you are doing is both redundant & wrong.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.