| | |
Need help with a problem dealing with files (retriving and checking information)
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2009
Posts: 22
Reputation:
Solved Threads: 0
I'm helping a friend out with some code and I'm having the hardest time figuring out how to do this. I know it is simple, but for the life of me I can't remember how to do it. Any help or advice would be welcomed. Thanks for the help in advance.
Problem: I need to open a file first, then have a user input a 6 digit number then check the file and if it finds it then it will get the name by it and if it doesn't exist in the file then it will say so. I'm having trouble having it run through and check the file. I can have it run through the whole document and output the data and I can check the very first line of data, but after that I can't get it to work at all. It will get the first three data values from the file if it isn't the first number of the file, but it won't get the name or check. I haven't got to the part about output if the number doesn't exit in the file, but I'm not concerned about that part yet. Here is my code. Thanks again for the help.
Sincerely yours;
jdm
Problem: I need to open a file first, then have a user input a 6 digit number then check the file and if it finds it then it will get the name by it and if it doesn't exist in the file then it will say so. I'm having trouble having it run through and check the file. I can have it run through the whole document and output the data and I can check the very first line of data, but after that I can't get it to work at all. It will get the first three data values from the file if it isn't the first number of the file, but it won't get the name or check. I haven't got to the part about output if the number doesn't exit in the file, but I'm not concerned about that part yet. Here is my code. Thanks again for the help.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <iomanip> #include <fstream> #include <string> using namespace std; //helps run the program int main() { double sticker; string Fname; double first; string second; second = "not"; ifstream dataIn; //for the data input dataIn.open("HWK5.txt"); cout << "sticker: "; cin >> sticker; while(dataIn) { dataIn >> first; cout << first << endl;; if (first == sticker) { dataIn >> second; cout << second << endl; Fname = second; } else if (first != sticker){ dataIn >> first; cout << first << endl; } } cout << "here: " << Fname << endl; dataIn.close(); system("Pause"); return 0; } /* while(dataIn) { dataIn >> first; cout << first << endl; if (first != sticker){ dataIn >> first; cout << first << endl; } else if (first == sticker) { dataIn >> second; cout << second << endl; Fname = second; } } */
Sincerely yours;
jdm
0
#2 28 Days Ago
For checking every line of the data, assuming each number has its own line, try this:
Maybe I misread your question, but I gathered that you couldn't get your inputted number to check against all the values in your file.
For checking whether the number is found, you might include something like:
Hope this helps.
C++ Syntax (Toggle Plain Text)
string sticker; string line; dataIn.open("HWK5.txt"); if (dataIn.is_open()) { while (! dataIn.eof() ) { getline (dataIn,line); stickCheck = (atoi(line.c_str())); if(stickCheck == sticker) { //once numbers match perform your function } } dataIn.close(); }
Maybe I misread your question, but I gathered that you couldn't get your inputted number to check against all the values in your file.
For checking whether the number is found, you might include something like:
C++ Syntax (Toggle Plain Text)
string sticker; string line; bool numFound = false; dataIn.open("HWK5.txt"); if (dataIn.is_open()) { while (! dataIn.eof() ) { getline (dataIn,line); stickCheck = (atoi(line.c_str())); if(stickCheck == sticker) { //once numbers match perform your function numFound = true; } } if(numFound == false) { cout << "Number not found!" << endl; } dataIn.close(); } else { cout << "Unable to open file!"; }
Hope this helps.
"Don't be afraid to give your best to what seemingly are small jobs. Every time you conquer one it makes you that much stronger. If you do the little jobs well, the big ones will tend to take care of themselves." - Dale Carnegie
\m/
\m/
•
•
Join Date: Jan 2009
Posts: 22
Reputation:
Solved Threads: 0
0
#3 28 Days Ago
Thank you for your help. I will give that a try and let you know how it work.
Sincerely yours;
jdm
11
Sincerely yours;
jdm
11
•
•
•
•
For checking every line of the data, assuming each number has its own line, try this:
C++ Syntax (Toggle Plain Text)
string sticker; string line; dataIn.open("HWK5.txt"); if (dataIn.is_open()) { while (! dataIn.eof() ) { getline (dataIn,line); stickCheck = (atoi(line.c_str())); if(stickCheck == sticker) { //once numbers match perform your function } } dataIn.close(); }
Maybe I misread your question, but I gathered that you couldn't get your inputted number to check against all the values in your file.
For checking whether the number is found, you might include something like:
C++ Syntax (Toggle Plain Text)
string sticker; string line; bool numFound = false; dataIn.open("HWK5.txt"); if (dataIn.is_open()) { while (! dataIn.eof() ) { getline (dataIn,line); stickCheck = (atoi(line.c_str())); if(stickCheck == sticker) { //once numbers match perform your function numFound = true; } } if(numFound == false) { cout << "Number not found!" << endl; } dataIn.close(); } else { cout << "Unable to open file!"; }
Hope this helps.
•
•
Join Date: Sep 2009
Posts: 458
Reputation:
Solved Threads: 52
0
#5 15 Days Ago
The atoi() function should strip out the text as long as the numbers are first. Either that or you could change his getline to one with a delimiter of ' ' (space) http://www.cplusplus.com/reference/i...tream/getline/
![]() |
Similar Threads
- How to use wildcard for attachement file name? (VB.NET)
- problem linking my files (C++)
- can't fix this problem dealing w/ in system32\ntoskrnl.exe (Viruses, Spyware and other Nasties)
- VB6 & Access (user name, network printer, reminders) (Visual Basic 4 / 5 / 6)
- Explorer 'crashes' when trying to delete a file (Windows NT / 2000 / XP)
- XP application hangs when importing Video files (Windows NT / 2000 / XP)
- Annoying problem with moving files (Windows NT / 2000 / XP)
- Problems IE 6 Win98 redirected from google to other site (Web Browsers)
Other Threads in the C++ Forum
- Previous Thread: Tip: How to avoid string parsing, File I/O
- Next Thread: getline problem
| Thread Tools | Search this Thread |
api application array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib library linkedlist linker list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg simple sorting string strings studio temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





