Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

data file problem?

Status
Not open for further replies.

ggreg

Programmer
Mar 9, 2001
201
US
I have a data file that looks like below
tom#4
sam#6
bill#2
chuck#4
Matt#3
Jeff#5
the above file is name car.txt........
I need code to allow a user to enter more than one name and then when
they have enter all the names they need ,,,then they enter the word exit
.....then it will look for the names above and add the numbers next to the
names and give me a total....Please give me full code because at this
point in my c++ exprience I might not be able to work it out ...let me show you
why ....below is the code I have been writing to work the above problem out
and I know its not even close to doing what I want .
thanks for any help.

#include <iostream.h>
#include <fstream.h>
#include <string.h>
#include <ctype.h>
struct counter
{
char name[25];
short seats;
};



void main()
{
//open the file for input



ifstream inFile;
inFile.open(&quot;car.txt&quot;, ios::in);

//check file open
if(!inFile.fail()) //open was successful

while(!inFile.eof())
{

inFile.get(counter.name, 25, '#');
inFile.ignore(1);
inFile >> counter.seats;
inFile.ignore(100, '\n');
// counter++;
}
{

//all data is entered

cout << endl << &quot;Would you like to search for a name?&quot;;
cin >> counter.name;
cin.ignore(100, '\n');
}
//search for the name
while(toupper(counter.name) == 'Y')
{
cout << &quot;Please enter a search name: &quot;;
cin.getline(newname, 25);

for(short x = 0; x < 25; x++)
{
if(stricmp(counter.name) == counter[x].name)
{
numseats = counter[x].seats;
total = total + numseats;
found = 'Y';
break;
}
else
found = 'N';
}
if(found == 'Y')
{

cout << &quot;The name &quot; << newname << &quot; exists.&quot; << endl;
cout << &quot;Would you like to enter another name?&quot;;
cin >> answer;
cin.ignore(100, '\n');
}
else
{
cout << &quot;That name does not exist in the database.&quot; << endl;
cout << &quot;Would you like to enter another name?&quot;;
cin >> counter.name;
cin.ignore(100, '\n');
}
}

cout << &quot;The total number of seats is: &quot; << total << endl;

//close file
inFile.close();
}
else
cout << &quot;Error opening data file!&quot; << endl;


}

 
There is a code what compille, but I have not debugged or tested it.

#include <iostream.h>
#include <fstream.h>
#include <string.h>
#include <ctype.h>
struct counter
{
char name[25];
short seats;
};
void main()
{
ifstream inFile;
inFile.open(&quot;car.txt&quot;, ios::in);
counter xxx;
if(inFile.fail())return;
while(!inFile.eof())
{
inFile.get(xxx.name, 25, '#');
inFile.ignore(1);
inFile >> xxx.seats;
inFile.ignore(100, '\n');
}
{
cout << endl << &quot;Would you like to search for a name?&quot;;
cin >> xxx.name;
cin.ignore(100, '\n');
}
counter ddd[25];
char found=0;
short answer;
int total=0;
while(toupper(xxx.name[0]) == 'Y')
{
char newname[50];
cout << &quot;Please enter a search name: &quot;;
cin.getline(newname, 25);
for(short x = 0; x < 25; x++)
{
int numseats=0;
int total =0;
if(!strcmp(xxx.name,ddd[x].name))
{
numseats = ddd[x].seats;
total += numseats;
found = 'Y';
break;
}else
{
found = 'N';
}if(found == 'Y')
{
cout << &quot;The name &quot; << newname << &quot; exists.&quot; << endl;
cout << &quot;Would you like to enter another name?&quot;;
cin >> answer;
cin.ignore(100, '\n');
}
else
{
cout << &quot;That name does not exist in the database.&quot; << endl;
cout << &quot;Would you like to enter another name?&quot;;
cin >> xxx.name;
cin.ignore(100, '\n');
}
}
cout << &quot;The total number of seats is: &quot; << total << endl;
inFile.close();
}
if(0);
else
cout << &quot;Error opening data file!&quot; << endl;
}
John Fill
1c.bmp


ivfmd@mail.md
 
There are no errors in your code
but.....it only lets me type one name and then
gives your statement about error opening file....
then closes program.....if your code did work what would
be needed to type more than one name then break it from typing
names so it could then add up the numbers got any guess??
 
No, because I don't know the spacific of your program. For example to open a file in read mode, this file must exist at least. If it exists, see if you put right path. If the file is in the same directory as the program, put it in the projects directory or add Debug\\ to your path. When you could open file, check if it has correct structure and go on. John Fill
1c.bmp


ivfmd@mail.md
 
By the way, debug your program and see when do errors appear. Is very easy and very efficient to use debuggers. John Fill
1c.bmp


ivfmd@mail.md
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top