Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › OCN community, I need some help with a C++ problem
New Posts  All Forums:Forum Nav:

OCN community, I need some help with a C++ problem

post #1 of 7
Thread Starter 
I've got a program that requires the user to enter their "number." It could be 47, or 500, or whatever. And what the program will do is search a .txt file that looks like this:

47 John Smith
500 Jane Doe
33 Jimminey Cricket

I need to have the user enter their number then have only their data brought in and stored into the variables, to be used later in the program. And all of this will be in a loop for multiple users, so there will be only one set of variables, replaced each time the user enters his or her number. I know how to do loops and how to open an input file and all that, I just don't know the exact line of code that tells the computer to search this file for this number I put in.

My question is, what specific line of code searches the already opened .txt file and extracts only the information I need based on the first number in the sentence?

Any help or advice will be much appreciated thumb.gif
The Urban
(17 items)
 
Lenovo Y500
(13 items)
 
 
CPUGraphicsRAMHard Drive
Intel Core i7-3630QM NVIDIA GeForce GT 650M 8gb DDR3 1600Mhz Plextor 128GB MSATA SSD 
Hard DriveOptical DriveOSMonitor
1TB 5400rpm DVD+RW Multi Combo Disc Drive Windows 8 64bit 15.6" 1920x1080p 
KeyboardPowerCaseMouse
Red LED Backlit 120w AC Adapter Brushed Aluminum Razer Orochi 2013 Wireless Bluetooth Mobile Gam... 
Audio
JBL Stereo Speakers 
  hide details  
Reply
The Urban
(17 items)
 
Lenovo Y500
(13 items)
 
 
CPUGraphicsRAMHard Drive
Intel Core i7-3630QM NVIDIA GeForce GT 650M 8gb DDR3 1600Mhz Plextor 128GB MSATA SSD 
Hard DriveOptical DriveOSMonitor
1TB 5400rpm DVD+RW Multi Combo Disc Drive Windows 8 64bit 15.6" 1920x1080p 
KeyboardPowerCaseMouse
Red LED Backlit 120w AC Adapter Brushed Aluminum Razer Orochi 2013 Wireless Bluetooth Mobile Gam... 
Audio
JBL Stereo Speakers 
  hide details  
Reply
post #2 of 7
you can use an ifstream to open the file and read it. just try to match the input with a subsection of each line (do something like myString.contains(inputString) ) that way you'll know if you found the beginning of the string that you want. this only works if you can guarantee that each number will be unique
my computer
(13 items)
 
  
CPUMotherboardGraphicsRAM
phenom II X4 955 Gigabyte GTX 480 ZOTAC AMP! 4gigs G.Skill ddr3 1333mhz 
Hard DriveOSMonitorPower
500 gb WD caviar Black Windows 7 premium 64 bit 20 inch Acer 750w corsair 
Case
coolermaster cm690 
  hide details  
Reply
my computer
(13 items)
 
  
CPUMotherboardGraphicsRAM
phenom II X4 955 Gigabyte GTX 480 ZOTAC AMP! 4gigs G.Skill ddr3 1333mhz 
Hard DriveOSMonitorPower
500 gb WD caviar Black Windows 7 premium 64 bit 20 inch Acer 750w corsair 
Case
coolermaster cm690 
  hide details  
Reply
post #3 of 7
Thread Starter 
Okay so lets assume I've already opened the file and created the variables int num, string first,last. You're saying it would go something like this?

cout << "Enter your assigned number: ";
cin >> num;

if (myString.contains(num))
{
inputFile >> first >> last;
cout << "Hello " << first << last;
}

else
cout << "I'm sorry, but you don't seem to be in our records";
Edited by SawyerAngelo - 9/11/13 at 9:41am
The Urban
(17 items)
 
Lenovo Y500
(13 items)
 
 
CPUGraphicsRAMHard Drive
Intel Core i7-3630QM NVIDIA GeForce GT 650M 8gb DDR3 1600Mhz Plextor 128GB MSATA SSD 
Hard DriveOptical DriveOSMonitor
1TB 5400rpm DVD+RW Multi Combo Disc Drive Windows 8 64bit 15.6" 1920x1080p 
KeyboardPowerCaseMouse
Red LED Backlit 120w AC Adapter Brushed Aluminum Razer Orochi 2013 Wireless Bluetooth Mobile Gam... 
Audio
JBL Stereo Speakers 
  hide details  
Reply
The Urban
(17 items)
 
Lenovo Y500
(13 items)
 
 
CPUGraphicsRAMHard Drive
Intel Core i7-3630QM NVIDIA GeForce GT 650M 8gb DDR3 1600Mhz Plextor 128GB MSATA SSD 
Hard DriveOptical DriveOSMonitor
1TB 5400rpm DVD+RW Multi Combo Disc Drive Windows 8 64bit 15.6" 1920x1080p 
KeyboardPowerCaseMouse
Red LED Backlit 120w AC Adapter Brushed Aluminum Razer Orochi 2013 Wireless Bluetooth Mobile Gam... 
Audio
JBL Stereo Speakers 
  hide details  
Reply
post #4 of 7
There is no string::contains method in c++. There is string::find, which you could use, but better yet you can just compare integers, e.g.:
Code:
struct Record {
        int id;
        string first, last;     
}

int findRecordInFile(string const& filename, int const number, Record& record) {
        
        ifstream ifs;
        ifs.open(filename);
        
        if (ifs.fail()) {
                return 1; // error reading the file
        }
        
        while (ifs >> record.id >> record.first >> record.last) {
                if (record.id == number) {
                        ifs.close();
                        return 0; // record found
                }
        }
        
        ifs.close();
        return 2; // record not found
}
buka
(17 items)
 
  
Reply
buka
(17 items)
 
  
Reply
post #5 of 7
First, I would write your data in binary format. Then, when reading, you can seek to the beginning of each record and check the idea. Or even better, write a little merge sort and use binary search smile.gif
post #6 of 7
Does this help?
Code:
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;

int main()
{
string str;
string str1;
string searchFor = "47";
int index;
ifstream ifs( "text.txt" );

while( getline(ifs, str) )// build one big string
str1 += str + '\n';


if( str1.find(searchFor, 0) != string::npos )
{
index = str1.find(searchFor, 0); // looking for the first occurance

string subStr = str1.substr( index ); // grab the single line 

string fName;
string lName;
int userNum;
stringstream ss( subStr );

ss >> userNum >> fName >> lName;
cout << userNum << " " << fName << " " << lName << endl;
}
else
cout << "couldn't find user" << endl;
}
2009
(15 items)
 
   
CPUMotherboardGraphicsRAM
i7 920 4.0Ghz 1.20v golden? Asus p6x58D Premium 770gtx Windforce OC ocz gold 1600 8-8-8-24-74-1T 
Hard DriveCoolingOSMonitor
x25-m80G2 Black640 H100 GT AP-15s Windows 7 Pro. 64 bit Dell U2410 1920x1200 IPS 
PowerCaseAudio
corsair tx 850 antec 1200 SoundBlaster ZX 
CPUMotherboardGraphicsRAM
Intel i7 3720qm(ivy) 2.6 Sager GTX 670m 8 gig 
Hard DriveKeyboardPower
80gig mssd steel series Huge power brick 
  hide details  
Reply
2009
(15 items)
 
   
CPUMotherboardGraphicsRAM
i7 920 4.0Ghz 1.20v golden? Asus p6x58D Premium 770gtx Windforce OC ocz gold 1600 8-8-8-24-74-1T 
Hard DriveCoolingOSMonitor
x25-m80G2 Black640 H100 GT AP-15s Windows 7 Pro. 64 bit Dell U2410 1920x1200 IPS 
PowerCaseAudio
corsair tx 850 antec 1200 SoundBlaster ZX 
CPUMotherboardGraphicsRAM
Intel i7 3720qm(ivy) 2.6 Sager GTX 670m 8 gig 
Hard DriveKeyboardPower
80gig mssd steel series Huge power brick 
  hide details  
Reply
post #7 of 7
Thread Starter 
Oh this was a while ago for a homework assignment, I'm not working on it anymore. Lol I ended up just skipping it since the instructor allows two missed/dropped homeworks for the semester. I apologize, I should have stated it earlier, instead of leaving you guys hanging.. But you know how life just gets in the way some times redface.gif
The Urban
(17 items)
 
Lenovo Y500
(13 items)
 
 
CPUGraphicsRAMHard Drive
Intel Core i7-3630QM NVIDIA GeForce GT 650M 8gb DDR3 1600Mhz Plextor 128GB MSATA SSD 
Hard DriveOptical DriveOSMonitor
1TB 5400rpm DVD+RW Multi Combo Disc Drive Windows 8 64bit 15.6" 1920x1080p 
KeyboardPowerCaseMouse
Red LED Backlit 120w AC Adapter Brushed Aluminum Razer Orochi 2013 Wireless Bluetooth Mobile Gam... 
Audio
JBL Stereo Speakers 
  hide details  
Reply
The Urban
(17 items)
 
Lenovo Y500
(13 items)
 
 
CPUGraphicsRAMHard Drive
Intel Core i7-3630QM NVIDIA GeForce GT 650M 8gb DDR3 1600Mhz Plextor 128GB MSATA SSD 
Hard DriveOptical DriveOSMonitor
1TB 5400rpm DVD+RW Multi Combo Disc Drive Windows 8 64bit 15.6" 1920x1080p 
KeyboardPowerCaseMouse
Red LED Backlit 120w AC Adapter Brushed Aluminum Razer Orochi 2013 Wireless Bluetooth Mobile Gam... 
Audio
JBL Stereo Speakers 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › OCN community, I need some help with a C++ problem