0

I was trying to create a simple struct array database that would be able to look up a user id and print out there first name, last name and age. So I've created a text file with 10 simple names ...here is my code ...can any one help me with this ...

//my code
//

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std; 

struct namesOfTen
	{
		string fName;
		string lName;
		int age;
		int id;
	} tenNamesId[10];
void seqOrderedSearch (const namesOfTen& list, int searchItem);
int _tmain(int argc, _TCHAR* argv[])
{
	

	ifstream inFile;
	ofstream outFile;
    
	inFile.open("C:\\Comp220\\names_10_ch11.txt");\\this is where my text file is located

	if(!inFile)
    {
        cout << "input file not found\n\n";
        return 1;
    }
	
	for (int i=0; i < 10; i++)
{
	inFile >> tenNamesId[i].fName;
	inFile >> tenNamesId[i].lName;
	inFile >> tenNamesId[i].age;
	inFile >> tenNamesId[i].id;
}
	inFile.close();

cout<<"Please enter user ID"<<endl;
cin>>tenNamesId[2].id;



	return 0;
}
int seqOrderedSearch (const namesOfTen& list, int searchItem)
	{
		int loc;
		bool found = false;
		
		for (loc = 0; loc < tenNamesId.id ; loc++)
			if (list.id[loc] == searchItem)
			{
				found = true;
				break;
			}
			if (found)
				return 
				cout<<"First Name:"<<tenNamesId.fName<<"Last Name:"<<tenNamesId.lName
				<<"Age:"<<tenNamesId.age<<endl;
			else 
				return -1;
	}
2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by ArkM
0

1. Use code tags:
[code=c++] your code

[/code]
2. You don't understand a role of "stdafx.h" header in VC++. Place all system header includes in stdafx.h, not in your cpp modules:

stdafx.h:
...
#include <iostream>
#include <fstream>
#include <string>
// TODO: reference additional headers your program requires here

yourcppfile.cpp:
#include "stdafx.h"
// may be another your header files:
#include "myheaderfile.h"
...

3. To pass an array to the function:

int seqOrderedSearch (const namesOfTen list[], int n, int searchItem)
{
    int loc;
    for (loc = 0; loc < n && list[loc].id != searchItem, ++loc)
        ;
    return loc < n? loc: -1;
}

Never believe to unspecified array sizes or other arbitrary assumptions...
4. Never print anything in search et al utility functions (except for debugging). Let high level caller prints results...

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.