C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old December 11th, 2008, 08:26 PM
kpanihilus kpanihilus is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2008
Posts: 26 kpanihilus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 57 m 27 sec
Reputation Power: 0
Problem input output program

I have gotten this program to work with cout but when I exchange it for battlereader<< it comes up with 102 errors. here is the code:

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
void inputdata();
void outputdata();
string name, type, attack, defence, healing;
string charcode;


int main()
{

inputdata();
outputdata();
system("pause");
}

void inputdata()
{
ifstream battlereader;
battlereader.open("C:/Documents and Settings/keith1/Desktop/cppgamefinalexaminput.txt");

if (!battlereader.fail())
{
battlereader<<"file could not be opened"<<endl;
system("pause");
}

battlereader<<" player data ">>endl;
battlereader<<" playername playertype attackstrength defensestrength healingpower "<<endl;
battlereader<<"---------------------------------------------------------------------------------------------------------"<<endl;


while (!battlereader.eof())
{
battlereader>>name>>type>>attack>>defence>>healing;
charcode = name.substr(0,1) + type.substr(0,1) + healing;

battlereader<<setw(5)<<charcode<<setw(20)<<name<<setw(15)<<type<<setw(10)<<attack<<setw(10)<<defence<<setw(10)<<healing<<endl;

}
battlereader.close();
};



void outputdata()
{
ofstream battlestream;

battlestream.open("C:/Documents and Settings/keith1/Desktop/cppgamefinalexamoutput.txt");

if (battlestream.fail())
{
battlestream<<"file could not open"<<endl;
}
battlestream.close();
};

Reply With Quote
  #2  
Old December 12th, 2008, 12:23 AM
reaper7861 reaper7861 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2008
Posts: 54 reaper7861 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 56 m 22 sec
Reputation Power: 10
Talking

Quote:
battlereader<<" player data ">>endl;
battlereader<<" playername playertype attackstrength defensestrength healingpower "<<endl;
battlereader<<"---------------------------------------------------------------------------------------------------------"<<endl;


It seems to me you are trying to read in not output so make sure that you are using the correct signs
Code:
battlereader<<" player data ">>endl;

and using endl on an input file stream...?
battlereader is an input file stream as you declared it earlier in your code.
if your trying to output to a file you need to use ofstream not ifstream<-- only for in from a file
ofstream <-- for output from a file
little bit of code:
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string name;
char filename[50];
ifstream fin;//declare an input file stream variable with the name fin
ofstream fout;//declare an output file stream variable with the name fout
fin.open("file.txt");//open a text file named file.txt
fin >> name;//take in the name in the file
cout << "What name do you want to output this file to?" << endl;//prompt user for a name for the output file name
cin.getline(filename,50);//take in that file name
fout.open(filename);//open the file with that name
fout << name;//output the name to the file
}

this code is just to show you how fstream works also you will want to go and look at fstream so you can get a better understanding of it, and this is just a little bit of advice you could use easier words for input and output stream variables since you will have to write it over and over again.
As you see in the little bit of code i wrote i use fin, it stands for filestream in and fout for filestream out. Hope that helps

Last edited by reaper7861 : December 12th, 2008 at 12:49 AM.

Reply With Quote
  #3  
Old December 12th, 2008, 07:30 AM
kpanihilus kpanihilus is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2008
Posts: 26 kpanihilus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 57 m 27 sec
Reputation Power: 0
I turned back all the battlereaders to couts but I cant get the following lines to show up in the output:

cout<<" player data "<<endl;
cout<<" playername playertype attackstrength defensestrength healingpower "<<endl;
cout<<"---------------------------------------------------------------------------------------------------------"<<endl;

Reply With Quote
  #4  
Old December 12th, 2008, 08:57 AM
kpanihilus kpanihilus is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2008
Posts: 26 kpanihilus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 57 m 27 sec
Reputation Power: 0
never mind on the last post. It didn't work like I thought. I changed the program to this:

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
void inputdata();
string name, type, attack, defence, healing;
string charcode;


int main()
{

inputdata();

system("pause");
}

void inputdata()
{
ofstream battlestream;
ifstream battlereader;

battlestream.open("C:/Documents and Settings/keith1/Desktop/cppgamefinalexamoutput.txt");
if (battlestream.fail())
{
cout<< "file could not opened"<<endl;
system("pause");
}


battlereader.open("C:/Documents and Settings/keith1/Desktop/cppgamefinalexaminput.txt");
if (!battlereader.fail())
{
cout<<"file could not be opened"<<endl;
system("pause");
}

cout<<" player data "<<endl;
cout<<" playername playertype attackstrength defensestrength healingpower "<<endl;
cout<<"---------------------------------------------------------------------------------------------------------"<<endl;


while (!battlestream.eof())
{
battlereader>>name>>type>>attack>>defence>>healing;
charcode = name.substr(0,1) + type.substr(0,1) + healing;

cout<<setw(5)<<charcode<<setw(20)<<name<<setw(15)<<type<<setw(10)<<attack<<setw(10)<<defence<<setw(10)<<healing<<endl;

}
battlereader.close();
battlestream.close();
};


but sometimes it creates a new output file everytime i run it or it will not output file(it says "file can not open.")

Reply With Quote
  #5  
Old December 12th, 2008, 02:53 PM
reaper7861 reaper7861 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2008
Posts: 54 reaper7861 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 56 m 22 sec
Reputation Power: 10
is this trying to output the information from the input file to the output file? Please tell me what are you trying to accomplish?

Reply With Quote
  #6  
Old December 12th, 2008, 05:10 PM
kpanihilus kpanihilus is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2008
Posts: 26 kpanihilus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 57 m 27 sec
Reputation Power: 0
Quote:
Originally Posted by reaper7861
is this trying to output the information from the input file to the output file? Please tell me what are you trying to accomplish?


I am trying to read the input file and then write that into the output file.

Reply With Quote
  #7  
Old December 12th, 2008, 06:09 PM
reaper7861 reaper7861 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2008
Posts: 54 reaper7861 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 56 m 22 sec
Reputation Power: 10
You have a problem with your while loop as well,it loops forever so you need to fix that. but here is the code for you to write it to the output file.

Code:
while (!battlestream.eof())
{
battlereader>>name>>type>>attack>>defence>>healing;
charcode = name.substr(0,1) + type.substr(0,1) + healing;

 battlestream<<setw(5)<<charcode<<setw(20)<<name<<setw(15)<<type<<setw(10)<<attack<<setw(10)<<defence<<setw(10)<<healing<<endl;

}

Reply With Quote
  #8  
Old December 12th, 2008, 06:19 PM
kpanihilus kpanihilus is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2008
Posts: 26 kpanihilus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 57 m 27 sec
Reputation Power: 0
it still doesn't write to the output but I have been trying to use getline with the file location and I still don't have it right. Here is what i have so far:



#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <cctype>
#include <cmath>
using namespace std;
void inputdata();
//void fightingstrength();
//void endurance();
string fighting;
string name, type, attack, defence, healing;
string charcode;


int main()
{

inputdata();
/*fightingstrength();
endurance();*/
system("pause");
}

void inputdata()
{
ofstream battlestream;
ifstream battlereader;

battlestream.open("c:/documents and settings/keith1/desktop/cppgamefinalexamoutput.txt");
if (battlestream.fail())
{
/*getline("c:/documents and settings/keith1/desktop/cppgamefinalexaminput.txt",27);*/
battlestream<< "file could not opened"<<endl;
system("pause");
}


battlereader.open("c:/documents and settings/keith1/desktop/cppgamefinalexaminput.txt");
if (!battlereader.fail())
{


battlestream<<"input file could not be opened"<<endl;
system("pause");
};

battlestream<<" player data "<<endl;
battlestream<<" playername playertype attackstrength defensestrength healingpower "<<endl;
battlestream<<"---------------------------------------------------------------------------------------------------------"<<endl;


while (!battlestream.eof())
{
battlereader>>name>>type>>attack>>defence>>healing;
charcode = name.substr(0,1) + type.substr(0,1) + healing;

battlestream<<setw(5)<<charcode<<setw(20)<<name<<setw(15)<<type<<setw(10)<<attack<<setw(10)<<defence<<setw(10)<<healing<<endl;

}
battlereader.close();
battlestream.close();
};

//void fightingstrength()
//{
//double fighting=attack*healing;
//}
//


anything commented out besides the getline line is what i am working on so far.

Reply With Quote
  #9  
Old December 13th, 2008, 01:59 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Sep 2005
Posts: 1,021 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Weeks 8 h 12 m 36 sec
Reputation Power: 14
Your boolean expressions are mixed up...

first:
cpp Code:
Original - cpp Code
  1. battlestream.open("c:/documents and settings/keith1/desktop/cppgamefinalexamoutput.txt");
  2. if (battlestream.fail())
  3. {
  4. /*getline("c:/documents and settings/keith1/desktop/cppgamefinalexaminput.txt",27);*/
  5. battlestream<< "file could not opened"<<endl;
  6. system("pause");
  7. }

I read this:
-Open battlestream
-if you could not open battlestream then write to battlestream "file could not be opened". So you are writing to a file which had trouble opening..


Second:
cpp Code:
Original - cpp Code
  1. battlereader.open("c:/documents and settings/keith1/desktop/cppgamefinalexaminput.txt");
  2. if (!battlereader.fail())
  3. {
  4. battlestream<<"input file could not be opened"<<endl;
  5. system("pause");
  6. };

I read this:
- Open battlereader
- if it did not fail, i.e., --> if it worked write "input file could not be opened". Again, the boolean expression is flipped.
__________________
There is no such thing as C/C++, you either program C or C++

Reply With Quote
  #10  
Old December 13th, 2008, 07:18 AM
kpanihilus kpanihilus is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2008
Posts: 26 kpanihilus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 57 m 27 sec
Reputation Power: 0
I can get part of the output file(the three lines that says "player data, playername.type,etc and ------------------") but when i try to write the contents of the input file into the output file. But now I was trying to use the getline function but I get I think two error messages. any ideas?

Reply With Quote
  #11  
Old December 13th, 2008, 03:27 PM
kpanihilus kpanihilus is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2008
Posts: 26 kpanihilus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 57 m 27 sec
Reputation Power: 0
this is my code now that i am trying to use getline to get the input file into the output file and i can't get it to work:

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <cctype>
#include <cmath>
using namespace std;
void inputdata();
//void fightingstrength();
//void endurance();
string fighting;
string name, type, attack, defence, healing;
string charcode;


int main()
{

inputdata();
/*fightingstrength();
endurance();*/
system("pause");
}

void inputdata()
{
ofstream battlestream;
ifstream battlereader;

battlestream.open("c:/documents and settings/keith1/desktop/cppgamefinalexamoutput.txt");
if (battlestream.fail())
{

battlestream<< "file could not opened"<<endl;
system("pause");
}


battlereader.open("c:/documents and settings/keith1/desktop/cppgamefinalexaminput.txt");

if (!battlereader.fail())
{
string str;

battlestream<<"input file could not be opened"<<endl;
getline(battlereader, str);

system("pause");
};

battlestream<<" player data "<<endl;
battlestream<<" playername playertype attackstrength defensestrength healingpower "<<endl;
battlestream<<"---------------------------------------------------------------------------------------------------------"<<endl;


while (!battlestream.eof())
{
battlereader>>name>>type>>attack>>defence>>healing;
charcode = name.substr(0,1) + type.substr(0,1) + healing;

battlestream<<setw(5)<<charcode<<setw(20)<<name<<setw(15)<<type<<setw(10)<<attack<<setw(10)<<defence<<setw(10)<<healing<<endl;

}
battlereader.close();
battlestream.close();
};

//void fightingstrength()
//{
//double fighting=attack*healing;
//}
//

Reply With Quote
  #12  
Old December 14th, 2008, 12:04 PM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Sep 2005
Posts: 1,021 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Weeks 8 h 12 m 36 sec
Reputation Power: 14
Did you read my previous post at all? You are confusing the streams..

I am assuming you should at least change this:
while (!battlestream.eof())
into this:
while (!battlereader.eof())

otherwise it will loop forever!

Reply With Quote
  #13  
Old December 14th, 2008, 02:12 PM
kpanihilus kpanihilus is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2008
Posts: 26 kpanihilus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 57 m 27 sec
Reputation Power: 0
i tried that part but it still won't write the input file to the output file.

Reply With Quote
  #14  
Old December 14th, 2008, 03:22 PM
reaper7861 reaper7861 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2008
Posts: 54 reaper7861 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 56 m 22 sec
Reputation Power: 10
You should really read he post that icon left he has some really good input to help you with your problem.

Reply With Quote
  #15  
Old December 14th, 2008, 03:34 PM
kpanihilus kpanihilus is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2008
Posts: 26 kpanihilus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 57 m 27 sec
Reputation Power: 0
so just flip the open and fail boolan on both of them

Reply With Quote
  #16  
Old December 14th, 2008, 04:36 PM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Sep 2005
Posts: 1,021 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Weeks 8 h 12 m 36 sec
Reputation Power: 14
Copying a file, without any decent error messages:
cpp Code:
Original - cpp Code
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <iterator>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. int main()
  10. {
  11.    ofstream out("d:\\temp\\out.txt");
  12.    ifstream in("d:\\temp\\in.txt");
  13.  
  14.    if( in && out )
  15.    {
  16.       copy( istreambuf_iterator<char>( in ),
  17.             istreambuf_iterator<char>(),
  18.             ostreambuf_iterator<char>( out ) );
  19.    }
  20. };


Have to get some sleep now.. Tomorrow I will look at your code and see if I can fix it, if you still need it then.

Reply With Quote
  #17  
Old December 15th, 2008, 05:33 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Sep 2005
Posts: 1,021 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Weeks 8 h 12 m 36 sec
Reputation Power: 14
I modified your code to make it work without changing many other things (I would never use globals like that..). I am only posting the inputdata() function:
cpp Code:
Original - cpp Code
  1. void inputdata()
  2. {
  3.    ofstream battlestream("d:\\temp\\out.txt");
  4.    if( !battlestream )
  5.    {
  6.  
  7.       cout << "Output file could not opened" << endl;
  8.       exit(-1);
  9.    }
  10.  
  11.    ifstream battlereader("d:\\temp\\in.txt");
  12.    if( !battlereader )
  13.    {
  14.       cout << "Input file could not be opened" << endl;
  15.       battlestream << "Input file could not be opened" << endl;
  16.       exit(-2);
  17.    };
  18.  
  19.    // so if we are here it means both input and output are successfully opened
  20.  
  21.    battlestream << " player data " << endl;
  22.    battlestream << " playername playertype attackstrength defensestrength healingpower " << endl;
  23.    battlestream << "---------------------------------------------------------------------------------------------------------" << endl;
  24.  
  25.    // LOOP OVER THE INPUT FILE
  26.    while( !battlereader.eof() )
  27.    {
  28.       battlereader >> name >> type >> attack >> defence >> healing;
  29.  
  30.       if( !battlereader )
  31.       {
  32.          cout << "Error reading input file\n";
  33.          exit(-1);
  34.       }
  35.  
  36.       charcode = name.substr(0,1) + type.substr(0,1) + healing;
  37.  
  38.       battlestream << setw(5) << charcode << setw(20) << name << setw(15) << type << setw(10) << attack << setw(10) << defence << setw(10) << healing << endl;
  39.    }
  40.  
  41.    // the streams close when they go out of scope
  42. };

You should of course change the names of the filepaths back to your own. Now it really comes down to what is in your input file. If you are still having problems with THIS code then post your input file so I can check that.

Last edited by Icon : December 15th, 2008 at 05:38 AM.

Reply With Quote
  #18  
Old December 16th, 2008, 01:41 PM
kpanihilus kpanihilus is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2008
Posts: 26 kpanihilus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 57 m 27 sec
Reputation Power: 0
I just tried the two types of code from above and they both don't let me copy the input file into the output file. Here is my code and input file contents:


code:


#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <cctype>
#include <cmath>
#include <algorithm>
using namespace std;
void inputdata();

//void fightingstrength();
//void endurance();
double fighting;

string name, type, attack, defence, healing;
string charcode;


int main()
{

inputdata();
/*fightingstrength();
endurance();*/
system("pause");
}

void inputdata()
{

ifstream battlereader;
ofstream battlestream;

battlestream.open("C:/Documents and Settings/keith1/Desktop/cppgamefinalexamoutput.txt");
if (!battlestream)
{
cout<<"output file could not be opened"<<endl;
battlestream<<"output file could not be opened"<<endl;

system("pause");
}

battlereader.open("C:/Documents and Settings/keith1/Desktop/cppgamefinalexaminput.txt");
if (!battlereader)
{
cout<<"input file could not be opened"<<endl;
battlestream<<"input file could not be opened"<<endl;

system("pause");
}

battlestream<<" player data "<<endl;
battlestream<<" playername playertype attackstrength defensestrength healingpower fightstrength endurance "<<endl;
battlestream<<"----------------------------------------------------------------------------------------------------------------------------------------"<<endl;

while (!battlereader.eof())
{
battlereader>>name>>type>>attack>>defence>>healing;
charcode = name.substr(0,1) + type.substr(0,1) + healing;
battlestream<<setw(5)<<charcode<<setw(20)<<name<<setw(15)<<type<<setw(10)<<attack<<setw(10)<<defence<<setw(10)<<healing<<endl;

if(!battlereader)
{
battlestream<<"error reading input file/n";
exit(-2);
}
system("pause");
}
battlestream.close();
battlereader.close();
}

//
//void fightingstrength()
//{
//fighting=attack*healing;
//}
//
//void endurance()
//{
// endurance=defence*healing-attack;
//}



input file contents:


Elden, Fighter, 200, 50, 10
Supperdude, Wizard, 100, 20, 50
Hans, Scout, 100, 50, 10
George, Dwarf, 300, 100, 20
Greg, Elf, 200, 50, 5
Sarah, Fairy, 50, 10, 50
John, Swordsman, 200, 30, 10
Sam, Medic, 50, 30, 50
Gram, Wizard, 200, 20, 40



input

Reply With Quote
  #19  
Old December 16th, 2008, 02:16 PM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Sep 2005
Posts: 1,021 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Weeks 8 h 12 m 36 sec
Reputation Power: 14
It works for me. I removed all the empty lines from the input file (including the last newline character).

Reply With Quote
  #20  
Old December 16th, 2008, 02:26 PM
kpanihilus kpanihilus is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2008
Posts: 26 kpanihilus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 57 m 27 sec
Reputation Power: 0
using the code with d:\\temp\\in.txt" and you got it to output to a file?

Reply With Quote
  #21  
Old December 16th, 2008, 02:31 PM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Sep 2005
Posts: 1,021 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Weeks 8 h 12 m 36 sec
Reputation Power: 14
Of course I change the paths because I dont have 'C:/Documents and Settings/keith1' ...

I advise you to test it with a short path like c:/temp without spaces and stuff in them. But of course it should work with your docs&settings path as well for you.

Reply With Quote
  #22  
Old December 16th, 2008, 02:46 PM
kpanihilus kpanihilus is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2008
Posts: 26 kpanihilus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 57 m 27 sec
Reputation Power: 0
I still have a problem with these to segments:


ifstream battlereader("c://temp/cppgamefinalexaminputput.txt");
if (!battlereader)
{
cout<<"input file could not be opened"<<endl;
battlestream<<"input file could not be opened"<<endl;
exit(-2);
system("pause");
}



and



while (!battlereader.eof())
{
battlereader>>name>>type>>attack>>defence>>healing;
charcode = name.substr(0,1) + type.substr(0,1) + healing;
battlestream<<setw(5)<<charcode<<setw(20)<<name<<setw(15)<<type<<setw(10)<<attack<<setw(10)<<defence<<setw(10)<<healing<<endl;

if(!battlereader)
{
battlestream<<"error reading input file/n";
exit(-2);
}
system("pause");
}

}


the first one says the error message if it can't open something and the second one says its errror message but I can't pinpoint the fault of it.

Reply With Quote
  #23  
Old December 19th, 2008, 04:38 PM
reaper7861 reaper7861 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2008
Posts: 54 reaper7861 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 56 m 22 sec
Reputation Power: 10
this is what i want you to do goto this folder
My Documents\Visual Studio 2005\Projects\Battle Simulator\Battle Simulator\ where Battle Simulator is the name of your project and inside the folders there place a file as such:
your input file name will be..
cppgamefinalexaminput.txt
output will be created when needed

then your code will be
Code:
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <cctype>
#include <cmath>
#include <algorithm>
using namespace std;
void inputdata();

//void fightingstrength();
//void endurance();
double fighting;

string name, type, attack, defence, healing;
string charcode;


int main()
{

inputdata();
/*fightingstrength();
endurance();*/
system("pause");
}

void inputdata()
{

ifstream battlereader;
ofstream battlestream;

battlestream.open("cppgamefinalexamoutput.txt");
if (!battlestream)
{
cout<<"output file could not be opened"<<endl;
battlestream<<"output file could not be opened"<<endl;

system("pause");
}

battlereader.open("cppgamefinalexaminput.txt");
if (!battlereader)
{
cout<<"input file could not be opened"<<endl;
battlestream<<"input file could not be opened"<<endl;

system("pause");
}

battlestream<<" player data "<<endl;
battlestream<<" playername playertype attackstrength defensestrength healingpower fightstrength endurance "<<endl;
battlestream<<"----------------------------------------------------------------------------------------------------------------------------------------"<<endl;

while (!battlereader.eof())
{
battlereader>>name>>type>>attack>>defence>>healing;
charcode = name.substr(0,1) + type.substr(0,1) + healing;
battlestream<<setw(5)<<charcode<<setw(20)<<name<<setw(15)<<type<<setw(10)<<attack<<setw(10)<<defence<<setw(10)<<healing<<endl;

if(!battlereader)
{
battlestream<<"error reading input file/n";
exit(-2);
}
system("pause");
}
battlestream.close();
battlereader.close();
}

//
//void fightingstrength()
//{
//fighting=attack*healing;
//}
//
//void endurance()
//{
// endurance=defence*healing-attack;
//}

then run it and it will run perfect if you are having problems after that it is your pathing...in other words your path to your files is wrong...please use what i am showing you here and your code will work perfectly
this is the outputfile i am getting:
Code:
 player data 
 playername playertype attackstrength defensestrength healingpower fightstrength endurance 
----------------------------------------------------------------------------------------------------------------------------------------
 EF10              Elden,       Fighter,      200,       50,        10
 SW50         Supperdude,        Wizard,      100,       20,        50
 HS10               Hans,         Scout,      100,       50,        10
 GD20             George,         Dwarf,      300,      100,        20
  GE5               Greg,           Elf,      200,       50,         5
 SF50              Sarah,         Fairy,       50,       10,        50
 JS10               John,     Swordsman,      200,       30,        10
 SM50                Sam,         Medic,       50,       30,        50
 GW40               Gram,        Wizard,      200,       20,        40
 GW40               Gram,        Wizard,      200,       20,        40

Last edited by reaper7861 : December 19th, 2008 at 04:41 PM.

Reply With Quote
  #24  
Old December 21st, 2008, 08:19 AM
kpanihilus kpanihilus is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2008
Posts: 26 kpanihilus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 57 m 27 sec
Reputation Power: 0
I actually did it a different way like this:

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <cctype>
#include <cmath>
#include <algorithm>
using namespace std;
void inputdata();

//void fightingstrength();
//void endurance();
double fighting;

string name, type, attack, defence, healing;
string charcode;


int main()
{

inputdata();
/*fightingstrength();
endurance();*/
system("pause");
}

void inputdata()
{

ifstream battlereader;
ofstream battlestream;

battlestream<<" player data "<<endl;
battlestream<<" playername playertype attackstrength defensestrength healingpower fightstrength endurance "<<endl;
battlestream<<"----------------------------------------------------------------------------------------------------------------------------------------"<<endl;


battlestream.open("c:/documents and settings/keith1/desktop/cppgamefinalexamoutput.txt");


battlereader.open("cppgamefinalexaminput.txt");
if (!battlereader)


battlereader>>name>>type>>attack>>defence>>healing;
system("pause");
}

or something like that because i dont have the code in front of me but it did work and thank you for your help.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Problem input output program


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap