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("car.txt", 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 << "Would you like to search for a name?";
cin >> counter.name;
cin.ignore(100, '\n');
}
//search for the name
while(toupper(counter.name) == 'Y')
{
cout << "Please enter a search name: ";
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 << "The name " << newname << " exists." << endl;
cout << "Would you like to enter another name?";
cin >> answer;
cin.ignore(100, '\n');
}
else
{
cout << "That name does not exist in the database." << endl;
cout << "Would you like to enter another name?";
cin >> counter.name;
cin.ignore(100, '\n');
}
}
cout << "The total number of seats is: " << total << endl;
//close file
inFile.close();
}
else
cout << "Error opening data file!" << endl;
}
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("car.txt", 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 << "Would you like to search for a name?";
cin >> counter.name;
cin.ignore(100, '\n');
}
//search for the name
while(toupper(counter.name) == 'Y')
{
cout << "Please enter a search name: ";
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 << "The name " << newname << " exists." << endl;
cout << "Would you like to enter another name?";
cin >> answer;
cin.ignore(100, '\n');
}
else
{
cout << "That name does not exist in the database." << endl;
cout << "Would you like to enter another name?";
cin >> counter.name;
cin.ignore(100, '\n');
}
}
cout << "The total number of seats is: " << total << endl;
//close file
inFile.close();
}
else
cout << "Error opening data file!" << endl;
}