Hi all!!!
simple question , i have included a txt file in my program which holds names and numbers like this

Alan 23
Bob 50
Joan 35

i want to know if it is possible to do a search in that file by the name and return the corresponding number to the screen.
example;
cin>> name;
if(name correct )
{cout<< number;
}

thank you all!!

Recommended Answers

All 6 Replies

load the names in an array or vector and then do a 'find'?

load the names in an array or vector and then do a 'find'?

No I mean doing a fstream type thing in a external txt file.

thats what i'm saying, fstream provides an interface to read and write data from and to files just like input/output streams. You can open the file and read all the contents in vector of required type(may be a vector of structures) and then compare your value against these values. I'm not sure how to do it otherwise may be someone else can help if you elaborate more on what exactly you want.

thats what i'm saying, fstream provides an interface to read and write data from and to files just like input/output streams. You can open the file and read all the contents in vector of required type(may be a vector of structures) and then compare your value against these values. I'm not sure how to do it otherwise may be someone else can help if you elaborate more on what exactly you want.

ok here is my code what i need is to have the red information inside a txt file and read from there, cuz the amount of info im going to insert will be big and i dont want to get lost , and it would b a lot easier to edit the txt file later if needed.... thanks again...

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <ctime>
#include <string>
#include <iomanip>
#include <ctype.h>

using namespace std;

struct CodeInfo
{
	string productCode;
	double price; // or use double if you wish
	double subtotal;
	double jobtotal;
	
};

int main(int argc, char **argv)
{
	CodeInfo items[3] = { {"rb38ssw500x500", 200},{"rb38ssw600x600", 300},{"rb38ssw700x700", 400}};
	double subtotal;
	double jobtotal;
    double grandsub;

   do {
		string code;
		size_t cnt = -1;
		
		cout << "Enter product code (or ctrl-D to exit): ";
		cin >> code;
		cout<< "Enter Value on the JobSheet:";
		cin>> jobtotal;
		for (size_t i = 0; i < sizeof(items) / sizeof(CodeInfo); i++) {
			if (code == items[i].productCode) {
				cnt = i;
				cout << "setting\n";
			
			}
		}
		
		if (cnt != -1) {
			cout<< "Price is:" << items[cnt].price<< endl;
			cout << "Discount Given is: " << (items[cnt].price - jobtotal) * 100 / items[cnt].price <<"%"<< endl; // price per item calculations can be made
			subtotal += items[cnt].price ;
	        grandsub += jobtotal;
		} else { 
			cout<< "\nSubtotal= " << subtotal << endl;
			cout << "\nDiscount = " <<  (subtotal - grandsub) * 100 / subtotal   << "%"<< endl;   //  setprecision(2) << subtotal << endl;
			break;                                           // final subtotal , calculations can be made  
		}
		
	} while (!cin.eof());
	
	return 0;
}

What agni is suggesting is you do something like this, and then do a search on the name for the inputted name and output the corrisponding number.

#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <sstream>

using namespace std;

struct data{
       string name;
       int number;
};

int main(void){
    vector<data> FileData;
    data tempData;
    ifstream ins("example.txt");
    string dataline;
    istringstream iss;
    
    while(getline(ins, dataline)){
                       iss.clear();
                       iss.str(dataline);
                       iss >> tempData.name >> tempData.number;
                       FileData.push_back(tempData);
    }
    
    for(int i = 0; i < FileData.size(); i++)
            cout << FileData[i].name << '\t' << FileData[i].number << endl;

    return 0;
}

Chris

sorry guys i was way for the week couldnt check my post , ok back to it now ....
so i try using the last code was given it to me and it didnt work , dont know y , anyway ...anyone that can help me at this time would b great !!!
im still learning and this forum is helping me a lot
i want to put all that structure into a file so my code wont look more messy or confusing later ...thank you all

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.