I am just learning c++ and there is a problem I can't output my file

+2 Dushyant Singh · January 26, 2016
I just started using c++ with the help of bucky tutorial the problem is I wanna output a file in my comp screen the file name is player.txt but it is not showing anything it blank although my complier does n't show any error I am using code blocks.



can anyone tell me is there any mistake or something and the file I wanna output has this info
 
DushyantSingh / male / 34 / $324234
SelenaGomez / female / 34 / $342343
JustinBieber / male / 44 / $324
ronaldo / male / 34 / $3.24234e+011
messi / male / 32 / $2.34234e+011

here is the code:

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main()
{
    ifstream fobj;
    fobj.open("players.txt");

    string name;
    string gen;
    int id;
    double money;

    while (fobj>>name>>gen>>id>>money)
    {
        cout<<name<<" "<<gen<<" "<<id<<" "<<money<<endl;

    }
return 0;

}


Post a Reply

Replies

Oldest  Newest  Rating
0 See Plus Plus · February 2, 2016
Hello, I am assuming that the player.txt file is exactly as follows:

DushyantSingh / male / 34 / $324234
SelenaGomez / female / 34 / $342343
JustinBieber / male / 44 / $324
ronaldo / male / 34 / $3.24234e+011
messi / male / 32 / $2.34234e+011



When this part of the code:

while (fobj>>name>>gen>>id>>money)


runs, it will first set the name variable to DushyantSingh, but then instead of setting male to the gen variable, it will instead set it to the slash, then setting id to male, ect.  So, you should take the slashes out of the players.txt.

After that, you shouldn't have the dollar sign for the money values in players.txt.  Adding the $ sign makes the value a string, not a double.  The compiler is trying to set a double variable to the value of a string, which won't work.  So, you should either change the money variable to a string or take out the dollar signs in player.txt

Hope this helps!
0 Dushyant Singh · February 2, 2016
@Krootushas Gesu

I have tried everything it's not working it does not showing any output it's just blank I removed the slashes but still the problem is the same its not working the program runs perfectly but does not show any output and sorry for the late response 
  • 1

C++

107,003 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator