By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,200 Members | 1,755 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,200 IT Pros & Developers. It's quick & easy.

C++ parsing username from string

P: 3
Hello Guys,
Yes, another beginner in C++ and I have a homework assignment that is giving me a headache.
Can someone help me with the following problem?
I have a text file with names, phone numbers and addresses.
I have to ask the user to input first name, last name then with a function call I have to find this name in my text file. The trick is that the name is in different format. Instead of first name, last name, it is backwards. Then when the program finds this name, it has to print out the person's phone and address. If the user input a false name, I have to say, no name found else, if user input nothing, it terminates the program. I know it sound maybe silly question, but it is very important for me.
Thank you in advance.
beginner#121315334564256645325235
Mar 31 '07 #1
Share this Question
Share on Google+
5 Replies


sicarie
Expert Mod 2.5K+
P: 4,677
What have you tried so far? Where are you having trouble?
Mar 31 '07 #2

P: 3
Here is what I've done so far. It does not find the matching name:( so I am stuck and can not go further...


#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

void findname (string firstlast);

int main (){



ifstream in;
in.open ("phoneDir");


int lines=0;
string data;
string name;



while (in){

cout<<"First and Last Name Please (all lower case): ";
cin>>name;

findname (data);



string phone;
getline (in,phone);
string address;
getline (in,address);


cout<<"The phone is: "<<phone<<endl;
cout<<"The address is: "<<address<<endl;

in.close ();

lines=lines+1;
}
}


void findname (string firstlast){

string firstname;
string lastname;

int spacepos=firstlast.find (' ');


firstname=firstlast.substr(0,spacepos);
lastname=firstlast.substr(spacepos+1,firstlast.len gth()-firstname.length()-1);
firstlast.find(firstname);
firstlast.find(lastname);


}
Mar 31 '07 #3

sicarie
Expert Mod 2.5K+
P: 4,677
Expand|Select|Wrap|Line Numbers
  1.   int lines=0;
  2.   string data;
  3.   string name;
  4.   while (in){
  5.       cout<<"First and Last Name Please (all lower case): ";
  6.       cin>>name;
  7.      findname (data);
  8.  
You assign the name to 'name', but pass 'data' which is so far uninitialized. In C++, a variable that has been declared but not initialized carries the value that was already in the memory space it was given - or junk data that you don't want. Try passing name (I didn't check the logic on your findname() function - see if passing it name fixes it).
Mar 31 '07 #4

P: 3
Hi,
I've tried, but I think the problem is with my function as well. The user inputs :
john doe but the file has doe, john and it might be in the 15th row or the 4th one. So the function has to find this name first. The other part would be easy for me, but can not get over this first part...
Thanks.


Expand|Select|Wrap|Line Numbers
  1.   int lines=0;
  2.   string data;
  3.   string name;
  4.   while (in){
  5.       cout<<"First and Last Name Please (all lower case): ";
  6.       cin>>name;
  7.      findname (data);
  8.  
You assign the name to 'name', but pass 'data' which is so far uninitialized. In C++, a variable that has been declared but not initialized carries the value that was already in the memory space it was given - or junk data that you don't want. Try passing name (I didn't check the logic on your findname() function - see if passing it name fixes it).
Mar 31 '07 #5

sicarie
Expert Mod 2.5K+
P: 4,677
Hi,
I've tried, but I think the problem is with my function as well. The user inputs :
john doe but the file has doe, john and it might be in the 15th row or the 4th one. So the function has to find this name first. The other part would be easy for me, but can not get over this first part...
Thanks.
Oh, if it's in the form 'doe, john' check out strok() - that will allow you to pull that into two different parts, and then you can use strcat() or strcpy() to combine them again (only with the first part as the last - keeping in mind you'll have to get the comma and space out of the second part of the string).

I've also changed the title the thread to better describe your issue, hopefully draw more people to the thread.
Mar 31 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.