944,068 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1302
  • C++ RSS
Apr 25th, 2007
0

Homework help...How do I save name into another file??

Expand Post »
How do I make C++ save the names entered by the user into another file on the computer???
#include<iostream>
#include<cstdlib>
#include<iomanip>
#include<string>
#include<fstream>
#include<vector>
using namespace std;
struct players
{
string fname;
string lname;
int atbats;
int runs;
int hits;
int doubles;
int triples;
int hrs;
int rbi;
int sos;
double avg;
};
int main()
{
 
void bubblesort2(players team[]);
string ifile="top100off.txt", ofile="top60def.txt", name;
players team[100];
int i, c;
 
ifstream inFile; 
inFile.open(ifile.c_str());
if (inFile.fail()) 
{
cout << "The file was not opened." << endl;
exit(1);
}
inFile >> team[0].fname >> team[0].lname >> team[0].atbats >> team[0].runs
>> team[0].hits >> team[0].doubles >> team[0].triples >> team[0].hrs 
>> team[0].rbi >> team[0].sos >> team[0].avg;
for(i=1; i<100; i++)
{
inFile >> team[i].fname >> team[i].lname >> team[i].atbats >> team[i].runs
>> team[i].hits >> team[i].doubles >> team[i].triples >> team[i].hrs 
>> team[i].rbi >> team[i].sos >> team[i].avg;
cout << team[i].fname << " " << team[i].lname << " " << team[i].atbats << " " << team[i].runs
<< " " << team[i].hits << " " << team[i].doubles << " " << team[i].triples << " " << team[i].hrs 
<< " " << team[i].rbi << " " << team[i].sos << " " << team[i].avg << "\n"; 
}
ofstream outFile; 
outFile.open(ofile.c_str());
if (outFile.fail())
{
cout << "Failed!!!." << endl;
}
//bubblesort2(team);
 
 
for (c=1; c<=5; c++)
{
cout << "Type a last from the offensive player list. " << endl;
getline(cin, name);
if (islower(name[0]))
{
name[0]=toupper(name[0]);
}
for (i=0; i<100; i++)
{
if (team[i].lname == name)
{
outFile << team[i].fname << " " << team[i].lname << " " << team[i].atbats << " " << team[i].runs
<< " " << team[i].hits << " " << team[i].doubles << " " << team[i].triples << " " << team[i].hrs 
<< " " << team[i].rbi << " " << team[i].sos << " " << team[i].avg << endl;
}
 
}
}
for(i=0; i<100; i++)
{
outFile << team[i].fname << " " << team[i].lname << " " << team[i].atbats << " " << team[i].runs
<< " " << team[i].hits << " " << team[i].doubles << " " << team[i].triples << " " << team[i].hrs 
<< " " << team[i].rbi << " " << team[i].sos << " " << team[i].avg << endl;
}
outFile.close();
 
 
cout << "Closed.";
cin.ignore();
return 42;
}
void bubbleSort2(players team[]) 
{
bool doMore;
do {
doMore = false; 
for (int i=0; i<100; i++) 
{
if (team[i].lname > team[i+1].lname) 
{
 
players temp = team[i];
team[i] = team[i+1]; 
team[i+1] = temp;
doMore = true; 
}
}
} while (doMore);
}
Attached Files
File Type: txt top100off.txt (4.5 KB, 2 views)
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
guystangr is offline Offline
5 posts
since Apr 2007
Apr 25th, 2007
0

Re: Homework help...How do I save name into another file??

Please indent your code so that it's readable. And move your declaration of bubleSort2 outside of main.
Reputation Points: 683
Solved Threads: 53
Posting Virtuoso
Infarction is offline Offline
1,580 posts
since May 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: i need help bad
Next Thread in C++ Forum Timeline: C++ in subversion





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC