C++ noob needs a little help

+3 Earl Hudson · January 17, 2015
Hi guys,
 I was watching Buckys C++ videos (66 and 67 I think) on writing and reading files. They are pretty straight forward. So I started tinkering around with them. Writing the file is a breeze. Reading it, not so much.
I wrote two codes the first one writes a file.

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

int main(){

string name;
string desc;
int price, wt, range, mindam, maxdam, type, armor;
int charges,effect, clas, grpsiz, room, rune;
int poison,level,shop;

name="dagger";
desc="a dagger";
price=15;
wt=10;
range=0;
mindam=1;
maxdam=3;
type=1;
armor=0;
charges=0;
effect=0;
clas=1;
grpsiz=0;
room=0;
rune=0;
poison=0;
level=0;
shop=1;

ofstream thefile("erhitems.txt");
cout << "The file should be created now." << endl;
thefile <<  name  <<endl;
thefile << desc << endl;
thefile << price << endl;
thefile << wt << endl;
thefile << range << endl;
thefile<< mindam << endl;
thefile<< maxdam << endl;
thefile<< type << endl;
thefile<< armor << endl;
thefile<< charges << endl;
thefile<< effect << endl;
thefile<< clas << endl;
thefile<< grpsiz << endl;
thefile<< room << endl;
thefile<< rune << endl;
thefile<< poison <<endl;
thefile<< level <<endl;
thefile<< shop << endl;

    return 0;
}

It does what is should. It creates a text file "erhitems.txt"

The second code is supposed to read it.

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

int main()
{

    string name,desc;
    int price, wt, range, mindam, maxdam, type, armor,charges,effect=0;
    int clas,grpsiz,room,rune,poison,level,shop=0;


    ifstream thefile("erhitems.txt");

    if (thefile.is_open()){cout << "You have the right file." << endl;}
    else{ cout << "You have the wrong file." << endl;}

    thefile >> name >> desc >> price >> wt >> range >> mindam >> maxdam >> type >> armor >> charges;
    thefile >> effect >> clas >> grpsiz >> room >> rune >> poison >> level >> shop;

    cout << "Name:         " << name << endl;
    cout << "Description:  " << desc << endl;
    cout << "Price:        " << price << endl;
    cout << "Weight:       " << wt << endl;
    cout << "Range:        " << range << endl;
    cout << "Min Damage:   " << mindam << endl;
    cout << "Max Damage:   " << maxdam << endl;
    cout << "Type:         " << type << endl;
    cout << "Armor:        " << armor << endl;
    cout << "Charges:      " << charges << endl;
    cout << "Effect:       " << effect << endl;
    cout << "Class:        " << clas << endl;
    cout << "Group Size:   " << grpsiz << endl;
    cout << "Room:         " << room << endl;
    cout << "Rune:         " << rune << endl;
    cout << "Poison:       " << poison << endl;
    cout << "Level:        " << level << endl;
    cout << "Shop:         " << shop << endl;


    return 0;
}


The problem is it doesn't insert the data into the correct variables. 
The text file created is:
dagger,a dagger,15,10,0,1,3,1,0,0,0,1,0,0,0,0,0,1

I need to get "dagger" into name, "a dagger" into description, and each variable with their perspective values.
Can someone please tell me what I need to do in order to achieve my goal please? Before it's over, I will have about a hundred items that will need to be read into a class of items.
Thank you for your help in advance,
E

Post a Reply

Replies

Oldest  Newest  Rating
+2 Jake Scaife · January 23, 2015
The reason why this isn't working is because <iostream> cin wont accept whitespace.

So when you are doing

thefile >> name >> desc >> price >> wt >> range >> mindam >> maxdam >> type >> armor >> charges;

It is reading the whole file upto the first space.. which is why it is storing "dagger,a" in name.. it reads a space.. then moves on to store the rest of the file in the next variable.

Probably the easiest way of getting around this is to probably use spaces to separate your variables in the text file.. However you would need to combat the description having spaces in it.

Although not easier, the best way of doing it (in my opinion) is to use getline() on your file.
getline(thefile, stringVariable) -- this would get the entire line in the file and include whitespace, you can then use some looping creativity to split the string up into an array, or the variables you wish to assign each section to.
Hope this helps, just ask if you need any further help :D
0 Earl Hudson · January 17, 2015
Sorry, I put up the wrong version of the file writer... The only difference is I added the commas.

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

int main(){

string name;
string desc;
int price, wt, range, mindam, maxdam, type, armor;
int charges,effect, clas, grpsiz, room, rune;
int poison,level,shop;

name="dagger";
desc="a dagger";
price=15;
wt=10;
range=0;
mindam=1;
maxdam=3;
type=1;
armor=0;
charges=0;
effect=0;
clas=1;
grpsiz=0;
room=0;
rune=0;
poison=0;
level=0;
shop=1;

ofstream thefile("items.txt");
cout << "The file should be created now." << endl;
thefile <<  name  << ",";
thefile << desc << "," ;
thefile << price << "," ;
thefile << wt << "," ;
thefile << range << "," ;
thefile<< mindam << "," ;
thefile<< maxdam << "," ;
thefile<< type << "," ;
thefile<< armor << "," ;
thefile<< charges << "," ;
thefile<< effect << "," ;
thefile<< clas << "," ;
thefile<< grpsiz << "," ;
thefile<< room << "," ;
thefile<< rune << "," ;
thefile<< poison << "," ;
thefile<< level << "," ;
thefile<< shop << endl;

    return 0;
}
  • 1

C++

107,113 followers
About

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

Links
Moderators
Bucky Roberts Administrator