Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 04-14-2005, 11:07 AM   #1 (permalink)
Popplewell
Registered User
 
Join Date: Apr 2005
Posts: 5
Popplewell is on a distinguished road
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.
Popplewell is offline   Reply With Quote
Old 04-14-2005, 11:21 AM   #2 (permalink)
Popplewell
Registered User
 
Join Date: Apr 2005
Posts: 5
Popplewell is on a distinguished road
Red face

Am I posting in th wrong place??? I am not a seasoned poster.
Popplewell is offline   Reply With Quote
Old 04-14-2005, 12:14 PM   #3 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
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
__________________
Valmont is offline   Reply With Quote
Old 04-14-2005, 12:21 PM   #4 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Wait a minute. What do you want to use?
C or C++?
__________________
Valmont is offline   Reply With Quote
Old 04-14-2005, 01:51 PM   #5 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
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;
}
__________________
Valmont is offline   Reply With Quote
Old 04-14-2005, 02:14 PM   #6 (permalink)
Popplewell
Registered User
 
Join Date: Apr 2005
Posts: 5
Popplewell is on a distinguished road
Many thanks! You're a star! I'm going to try it out later today
Popplewell is offline   Reply With Quote
Old 04-14-2005, 02:50 PM   #7 (permalink)
Popplewell
Registered User
 
Join Date: Apr 2005
Posts: 5
Popplewell is on a distinguished road
I got home to look at it properly, and it's too complicated for me! Rats! Thanks again for your time though
Popplewell is offline   Reply With Quote
Old 04-14-2005, 03:13 PM   #8 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
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.
__________________
Valmont is offline   Reply With Quote
Old 04-14-2005, 11:20 PM   #9 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
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.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 04-14-2005, 11:35 PM   #10 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
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.
__________________
Valmont is offline   Reply With Quote
Old 04-15-2005, 12:41 AM   #11 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
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....
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 04-15-2005, 05:59 AM   #12 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
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.
__________________
Valmont is offline   Reply With Quote
Old 04-15-2005, 06:39 AM   #13 (permalink)
Popplewell
Registered User
 
Join Date: Apr 2005
Posts: 5
Popplewell is on a distinguished road
Thanks for the words of wisdom!!
Popplewell is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
JSP code problem j.gohel Java 7 04-15-2005 03:07 PM
Output problem with file manipulation (newbie) crazyant Java 4 03-11-2005 02:03 PM
Hashing problem jodders Standard C, C++ 1 02-09-2005 02:51 PM
Problem Assignment (Urgent help req.) Boltress Standard C, C++ 0 01-12-2005 08:59 AM
Help debugging a power problem Belisarius Lounge 0 10-25-2003 05:44 PM


All times are GMT -8. The time now is 06:14 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting