|
 |
|
 |
04-14-2005, 11:07 AM
|
#1 (permalink)
|
|
Registered User
Join Date: Apr 2005
Posts: 5
|
Newbie with a dynamic problem
I am just starting a computing course and have been doing an assignment on dynamic arrays. I have some code but i don't see where I am going wrong, if someone could show me the error of my ways i would be really greatful. Any advice will be much appreciated.
It's a phone book entry system that needs a number of entry's then be able to search for a name and present its phone number or an unavailable message.
Code:
//Data Strucures Assignment 1
#include <iostream.h>
#include <cstring.h>
#include <conio.h>
struct contact{
string name;
string number;
};
void main(){
struct contact *entry;
int i,number;
char namesearch;
char c;
cout<<"How many entrys? ";
cin>>number;
cin.get(c);
entry = new struct contact[number];
for(i=1;i<number;i++){
cout<<"Name ";
getline(cin, entry[i].name);
cout<<"Phone number ";
cin>>entry[i].number;
cin.get(c);
};
clrscr();
for (i=1;i<number;i++);
cout<<entry[i].name<<" "<<entry[i].number<<endl;
cout<<"Search for a contact: "<<endl;
cout<<"(Press Z to stop) Contact Name: ";
cin.getline(namesearch);
while (char(namesearch, "Z")<0);{
clrscr();
string(entry[0].name = namesearch);
i = number;
while (char(entry[i].name = namesearch) !=0)
i--;
if (i==0)
cout<<"Not available"<<endl;
cout<<"Press enter to search again";
cin.get(c);
}
else
{
cout<<"Conatact Name: "<<entry[i].name<<endl;
cout<<"Phone Number: "<<entry[i].number<<endl;
cout<<"Press enter to search again";
cin.get(c);
}
clrscr();
cout<<"Search for a contact "<<endl;
cout<<"(Press Z to stop) Contact Name: "<<endl;
cin.getline(namesearch);
}
Thanks for your time x
Last edited by Valmont; 04-14-2005 at 12:16 PM.
|
|
|
04-14-2005, 11:21 AM
|
#2 (permalink)
|
|
Registered User
Join Date: Apr 2005
Posts: 5
|
Am I posting in th wrong place??? I am not a seasoned poster.
|
|
|
04-14-2005, 12:14 PM
|
#3 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
Quote:
|
Originally Posted by Popplewell
Am I posting in th wrong place??? I am not a seasoned poster.
|
No, but you should use the [code] tags.
Read the sticky topic on rules and such.
About the Standard C++ Forum
__________________
|
|
|
04-14-2005, 12:21 PM
|
#4 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
Wait a minute. What do you want to use?
C or C++?
__________________
|
|
|
04-14-2005, 01:51 PM
|
#5 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
In the mean time, here is a starter. See if it is any use to you.
Code:
#include <iostream>
#include <cstring>
#include <conio.h>
#include <cstdlib>
//--
using std::cout;
using std::cin;
using std::endl;
using std::getline;
using std::string;
//--
struct Contact
{
string name;
string number;
};
int main()
{
std::size_t RecordSize = 3;
/*cout<<"Enter number of Contact entries: ";
cin>>RecordSize;
cin.get();*/
Contact* ContactRecord = new Contact[RecordSize];
/*for(std::size_t i = 0; i < RecordSize; ++i)
{
cout<<"Enter Name: ";
getline(cin, ContactRecord[i].name);
cout<<"Phone number ";
cin>>ContactRecord[i].number;
cin.get();
}*/
ContactRecord[0].name = "Val"; ContactRecord[0].number = "123";
ContactRecord[1].name = "Alex"; ContactRecord[1].number = "456";
ContactRecord[2].name = "Val"; ContactRecord[2].number = "789";
system("cls");
//Let's review all Contact Records.
cout<<"Current Contact Records:"<<endl;
for (std::size_t i = 0; i < RecordSize; ++i)
{
cout<<ContactRecord[i].name<<" "<<ContactRecord[i].number<<endl;
}
char MenuController ;
string ContactName;
bool FoundContacts(false);
//Contact lookup menu loop.
while(true)
{
cout<<endl;
cout<<"Would you like to lookup a contact (y = yes, other = no)?"<<endl;
cin.get(MenuController);
cin.get();
system("cls");
if(MenuController != 'y')
{
//User does not want to lookup contacts: exit contact lookup menu loop.
break;
}
//User wants to lookup contacts.
cout<<"Enter the name of the contact."<<endl;
getline(cin, ContactName);
for(std::size_t i = 0; i < RecordSize; ++i)
{
if(ContactRecord[i].name == ContactName)
{
FoundContacts = true;
cout<<"Found match in record "<<i+1<<": "<<ContactName<<" - "
<<ContactRecord[i].number<<endl;
}
}
if(FoundContacts == false)
{
cout<<"No contacts found with the specified name."<<endl;
}
FoundContacts = false;
}
//Free the memory resources. Observe square brackets in delete[].
delete[] ContactRecord;
return 0;
}
__________________
|
|
|
04-14-2005, 02:14 PM
|
#6 (permalink)
|
|
Registered User
Join Date: Apr 2005
Posts: 5
|
Many thanks! You're a star! I'm going to try it out later today
|
|
|
04-14-2005, 02:50 PM
|
#7 (permalink)
|
|
Registered User
Join Date: Apr 2005
Posts: 5
|
I got home to look at it properly, and it's too complicated for me! Rats! Thanks again for your time though
|
|
|
04-14-2005, 03:13 PM
|
#8 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
Quote:
|
Originally Posted by Popplewell
I got home to look at it properly, and it's too complicated for me! Rats! Thanks again for your time though
|
Take it step by step. Grow in it. C++ code is hard to understand in general.
__________________
|
|
|
04-14-2005, 11:20 PM
|
#9 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
|
Quote:
|
Originally Posted by Valmont
C++ code is hard to understand in general.
|
Any programming language is hard to understand, untill you get some grasp of it.
|
|
|
04-14-2005, 11:35 PM
|
#10 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
I've never seen such tricky languages as C and C++.
In fact, when I encountered C for the first time (pre ansi stuff), I was so horryfied, I skipped it and stayed with Pascal. Then I did VB. And muuch later I finally did C++ for reasons of design and it was the only language I could promote to my boss. Otherwise I would have taken Ruby.
The final result is that I am still a C newb. LOL.
__________________
|
|
|
04-15-2005, 12:41 AM
|
#11 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
|
Thats funny, I have the exact opposite.. (If I don't count ML and Lisp) C/C++ was the easiest to get used to, but that could be the fact that I started out with ML/Lisp then C/C++ and then came the other languages, so some of the knowledge from C was transfered and compared with everything that came after C/C++.
So my angle on it could have been, Why can't I do it like I normaly would in C....
|
|
|
04-15-2005, 05:59 AM
|
#12 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
Well, it's where your priorities are. If your employer gives you a hint, then suddenly you can C++ too  .
Same goes for me with C. Luckily that won't happen where I am now, but I don't know what's coming up.
__________________
|
|
|
04-15-2005, 06:39 AM
|
#13 (permalink)
|
|
Registered User
Join Date: Apr 2005
Posts: 5
|
Thanks for the words of wisdom!!
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 06:14 AM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|