| | |
phonebook problem
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
Thanks for reply but I'm really sorry to say that it doesnot work, i tried it with just cin>>firstname. it still accepts only one character may be because of the for loop. because it stores one characterof fristname at position i=0, one character of lastname at positon 1 and one caharacter of phonenumber at position 2...i guess array has to be used there..but i'm not being able to use it..
as suggested before you don't need that for loop. Just ask the three questions
C++ Syntax (Toggle Plain Text)
cout << "Type the contacts first name:\n"; cin >> firstName; cout <<"Type the contacts last name:\n"; cin >> lastName; cout <<"Type the phonenumber\n"; cin >> phonenumber; // // You also need to separate the names with a space outFile << firstName << " " << lastName << " " << phonenumber << "\n"; selectionMsg(choice);
Last edited by Ancient Dragon; Mar 21st, 2008 at 11:55 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
i'm not getting where the problem is . the code is as folows as u told to do. but it's still accepting only one character..
I'm also looking at it to find where the problem still exists..
I'm also looking at it to find where the problem still exists..
C++ Syntax (Toggle Plain Text)
#include<iostream> #include<cstring> #include<fstream> using namespace std; const char OUTPUT_FILE[] = "out1.txt"; void introMsg(); void selectionMsg(int& choice); void addBlackBook(char firstName, char lastName, char phonenumber, ofstream& outFile, int& choice); void checkFileOpen(ofstream& aoutFile); //char outputBlackBook(); //void deleteFromBlackBook(); //char searchBlackBook(); int main(){ ifstream inFile; ofstream outFile; outFile.open(OUTPUT_FILE, ios::app); char firstName; char lastName; char phonenumber; int choice; introMsg(); selectionMsg(choice); if((choice = 1)){ addBlackBook(firstName, lastName, phonenumber, outFile, choice); }/**else if(choice == 2){ deleteFromBlackBook(); } else if (choice == 3){ searchBlackBook(firstName, lastName); } else if (choice == 4){ // inFile.open("abc12.txt"); outFile.open("out1.txt"); outputBlackBook(out); }*/ return 0; } void introMsg(){ cout << "Welcome to your black book\n"; cout << " This program keeps track of your contacts\n"; cout << " You may add, delete, search, and output contacts from this book\n"; } void selectionMsg(int& choice){ cout << "Type 1 to ADD a contact\n"; cout << "Type 2 to DELETE a contact\n"; cout << "Type 3 to SEARCH for contact\n"; cout << "Type 4 to OUTPUT ALL contacts\n"; cout << "Type exit to terminate the program\n"; cin >> choice; } void addBlackBook(char firstName, char lastName, char phonenumber, ofstream& outFile, int& choice){ // for(int j = 0; j < 3; j++ ){ // if(j==0){ cout << "Type the contacts first name:\n"; cin >> firstName; // }else if(j==1){ cout <<"Type the contacts last name:\n"; cin >> lastName; // }else{ cout <<"Type the phonenumber\n"; cin >> phonenumber; // } //cout << j; // } cout << firstName<<" " << lastName << " " <<phonenumber << endl; outFile << firstName<<" " << lastName<<" " << phonenumber<< endl; selectionMsg(choice); } void checkFileOpen(ofstream& aOutFile){ if(aOutFile.fail()) { cout << "output filed opened failed\n"; exit(1); } } } /** char searchBlackBook(char& fName, char& lName) { cout << "SEARCH FOR A CONTACT\n"; cout << "Type the contacts first name\n:"; cin >> fName; cout << "Type the contacts last name\n"; cin >> lName; for(int i = 0; i <=500; i ++){ for(int j = 0; j < 2; j++ ){ if (fName == firstName && lName == lastName) { cout << "fName" << "lName" << endl; } } } } char outputBlackBook(ifstream& aIn) { cout << "OUTPUT ALL CONTACTS\n"; aIn.get(); } void sortContacts() { } void swapContacts() { } */
Last edited by Ancient Dragon; Mar 22nd, 2008 at 12:25 am. Reason: add code tags
lines 19 to 21 are wrong -- that is only allocating one character to each of those variables. You need to make them arrays, like this:
And line 55 is also wrong -- I think you had it right the first time
char lastname[20]; And line 55 is also wrong -- I think you had it right the first time
void addBlackBook(char firstName[], char lastName[], char phonenumber[], ofstream& outFile, int& choice){ Last edited by Ancient Dragon; Mar 22nd, 2008 at 12:29 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
I have edited the progarm to alloacet space for more than one character. but it's still not working.
the code is as below. here i have assumed that user will not type more than 20 letter name or number. but the loop used by me is running for 20 times and is asking for 20 characters. i wanted that user could type as many as 20 characters. if he enters only 4 letter name than even it stops and ask for the last name. but i'm not able to make it possible...
the code is as follows:
the code is as below. here i have assumed that user will not type more than 20 letter name or number. but the loop used by me is running for 20 times and is asking for 20 characters. i wanted that user could type as many as 20 characters. if he enters only 4 letter name than even it stops and ask for the last name. but i'm not able to make it possible...
the code is as follows:
C++ Syntax (Toggle Plain Text)
void addBlackBook(char firstName[], char lastName[], char phonenumber[], ofstream& outFile, int& choice){ cout << "Type the contacts first name:\n"; for (int i=0; i< NUMBER; i++){ cin >> firstName[i]; } cout <<"Type the contacts last name:\n"; for(int i = 0; i < NUMBER; i++){ cin >> lastName[i]; } cout <<"Type the phonenumber\n"; for(int i = 0; i < NUMBER; i++){ cin >> phonenumber[i]; } cout << firstName[i] << lastName[i] << phonenumber[i] << endl; outFile << firstName[i] << lastName[i] << phonenumber[i] << endl; selectionMsg(choice);
above in main i have used it like this:
C++ Syntax (Toggle Plain Text)
#include<iostream> #include<cstring> #include<fstream> using namespace std; const char OUTPUT_FILE[] = "out1.txt"; void introMsg(); void selectionMsg(int& choice); void addBlackBook(char firstName[], char lastName[], char phonenumber[], ofstream& outFile, int& choice); void checkFileOpen(ofstream& aoutFile); //char outputBlackBook(); //void deleteFromBlackBook(); //char searchBlackBook(); const int NUMBER = 20; int main(){ ifstream inFile; ofstream outFile; outFile.open(OUTPUT_FILE, ios::app); char firstName[NUMBER]; char lastName[NUMBER]; char phonenumber[NUMBER]; int choice; introMsg(); selectionMsg(choice); if((choice = 1)){ addBlackBook(firstName, lastName, phonenumber, outFile, choice); }/**else if(choice == 2){ deleteFromBlackBook(); } else if (choice == 3){ searchBlackBook(firstName, lastName); } else if (choice == 4){ // inFile.open("abc12.txt"); outFile.open("out1.txt"); outputBlackBook(out); }*/ return 0; }
•
•
•
•
I have edited the progarm to alloacet space for more than one character. but it's still not working.
the code is as below. here i have assumed that user will not type more than 20 letter name or number. but the loop used by me is running for 20 times and is asking for 20 characters. i wanted that user could type as many as 20 characters. if he enters only 4 letter name than even it stops and ask for the last name. but i'm not able to make it possible...
the code is as follows:
C++ Syntax (Toggle Plain Text)
void addBlackBook(char firstName[], char lastName[], char phonenumber[], ofstream& outFile, int& choice){ cout << "Type the contacts first name:\n"; for (int i=0; i< NUMBER; i++){ cin >> firstName[i]; } cout <<"Type the contacts last name:\n"; for(int i = 0; i < NUMBER; i++){ cin >> lastName[i]; } cout <<"Type the phonenumber\n"; for(int i = 0; i < NUMBER; i++){ cin >> phonenumber[i]; } cout << firstName[i] << lastName[i] << phonenumber[i] << endl; outFile << firstName[i] << lastName[i] << phonenumber[i] << endl; selectionMsg(choice);
Last edited by Ancient Dragon; Mar 22nd, 2008 at 1:22 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
u know it still accepts only one character even if i use char firstname[20] and not use those for statements. have to tried to run that program with char firstName[20]
and without those for loops.
i agree for loops are creating mess in hte code but then something else has to be used to make cin accepts all the characters which user types as a name...
and without those for loops.
i agree for loops are creating mess in hte code but then something else has to be used to make cin accepts all the characters which user types as a name...
•
•
Join Date: Jan 2008
Posts: 3,837
Reputation:
Solved Threads: 503
Not sure what your call is from the main function, but something like this should be your function. No loops, no array indexes. cin and cout can figure out exactly when things start and stop.
If this doesn't work, I'm guessing that the problem is in the way you are calling the function.
C++ Syntax (Toggle Plain Text)
void addBlackBook(char firstName[], char lastName[], char phonenumber[], ofstream& outFile, int& choice) { cout << "Type the contacts first name:\n"; cin >> firstName; cout <<"Type the contacts last name:\n"; cin >> lastName; cout <<"Type the phonenumber\n"; cin >> phonenumber; cout << firstName << " " << lastName << " " << phonenumber << endl; outFile << firstName << lastName << phonenumber << endl; selectionMsg(choice); }
•
•
Join Date: Jan 2008
Posts: 3,837
Reputation:
Solved Threads: 503
•
•
•
•
above in main i have used it like this:
C++ Syntax (Toggle Plain Text)
#include<iostream> #include<cstring> #include<fstream> using namespace std; const char OUTPUT_FILE[] = "out1.txt"; void introMsg(); void selectionMsg(int& choice); void addBlackBook(char firstName[], char lastName[], char phonenumber[], ofstream& outFile, int& choice); void checkFileOpen(ofstream& aoutFile); //char outputBlackBook(); //void deleteFromBlackBook(); //char searchBlackBook(); const int NUMBER = 20; int main(){ ifstream inFile; ofstream outFile; outFile.open(OUTPUT_FILE, ios::app); char firstName[NUMBER]; char lastName[NUMBER]; char phonenumber[NUMBER]; int choice; introMsg(); selectionMsg(choice); if((choice = 1)){ addBlackBook(firstName, lastName, phonenumber, outFile, choice); }/**else if(choice == 2){ deleteFromBlackBook(); } else if (choice == 3){ searchBlackBook(firstName, lastName); } else if (choice == 4){ // inFile.open("abc12.txt"); outFile.open("out1.txt"); outputBlackBook(out); }*/ return 0; }
C++ Syntax (Toggle Plain Text)
if((choice = 1)){ addBlackBook(firstName, lastName, phonenumber, outFile, choice); }
![]() |
Similar Threads
- sequential search (cannot import string functions) (Python)
- problem with phonebook program (C)
- Phonebook program! (C)
- Making searching faster (Java)
- Can't setEnable a button on my Frame (Java)
- No idea how to do this problem... (C++)
- i have a data cable motorola T720 i need a software ringtone (Cellphones, PDAs and Handheld Devices)
Other Threads in the C++ Forum
- Previous Thread: Creating a game Database
- Next Thread: Linked List Problem
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






