473,322 Members | 1,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Crashes when runs?

Expand|Select|Wrap|Line Numbers
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <cmath>
  5. #include <string>
  6. #include <vector>
  7. using namespace std;
  8.  
  9. class PhoneNumber
  10. {    
  11.      int areaCode;
  12.      int number;
  13.      public:
  14.      int getAreaCode()
  15.      {
  16.          return(areaCode);
  17.      }
  18.      int getNumber()
  19.      {
  20.          return(number);
  21.      }
  22.      void setAreaCode(int theAreaCode)
  23.      {
  24.           areaCode=theAreaCode;
  25.      }
  26.      void setNumber(int theNumber)
  27.      {
  28.           number=theNumber;
  29.      }
  30.      PhoneNumber(int theAreaCode,int theNumber)
  31.      { 
  32.        areaCode=theAreaCode;
  33.        number=theNumber;
  34.      }     
  35. };
  36. /////////////////////////////////////////////////////////////////////
  37. class Person{
  38.       string firstName;
  39.       string lastName;
  40.       PhoneNumber phoneNumber;
  41.       static void num2str(int number,char numberStr[])
  42.       {
  43.              int i=0, j=0; 
  44.              char tmpNumberStr[128]; 
  45.              if(number<0)// ignore negative values
  46.               number=-number; 
  47.               do
  48.               { 
  49.               int lastDigit = number%10; 
  50.                tmpNumberStr[i++]='0'+lastDigit; 
  51.                number= number/10; 
  52.                 }while( number>0); 
  53.                 //reverse
  54.                 while(i>0) 
  55.                 numberStr[j++]=tmpNumberStr[--i];  
  56.                 numberStr[j]='\0'; 
  57.                   }
  58.  
  59.  
  60.       public:
  61.       string getFirstName()
  62.       {
  63.        return(firstName);
  64.       } 
  65.       string getLastName()
  66.       {
  67.        return(lastName);
  68.       }
  69.       void setFirstName(string theFirstName)
  70.       {
  71.            firstName=theFirstName;
  72.       } 
  73.       void setLastName(string theLastName)
  74.       {
  75.            lastName =theLastName; 
  76.       }    
  77.       PhoneNumber getPhoneNumber()
  78.       {
  79.            return(phoneNumber);
  80.       }
  81.       void setPhoneNumber(int theAreaCode, int theNumber)
  82.       {
  83.            phoneNumber.setAreaCode(theAreaCode);
  84.            phoneNumber.setNumber(theNumber);
  85.       }
  86.       string toString()
  87.       {
  88.           string everything;
  89.           string a,b;
  90.           a=(char)phoneNumber.getAreaCode();
  91.           b=(char)phoneNumber.getNumber();
  92.           everything=firstName+" "+lastName+" "+a+"-"+b; 
  93.           return(everything);
  94.  
  95.       }
  96.       Person(string theFirstName,string theLastName,int areaCode, int theNumber):phoneNumber(areaCode,theNumber)
  97.       {
  98.          firstName=theFirstName;
  99.          lastName=theLastName;
  100.       }
  101.  
  102.  
  103.  
  104.  
  105. };
  106. ///////////////////////////////////////////////////////////////////
  107.  
  108. class Database : public vector<Person>
  109. {
  110.       public:
  111.       bool isExist(string theFirstName, string theLastName)
  112.       {    
  113.          string a,b;
  114.  
  115.          bool exist=false; 
  116.          for(int i=0;i<=10;i++){
  117.          a=at(i).getFirstName();
  118.          b=at(i).getLastName();
  119.          if(a.compare(theFirstName)==0||(b.compare(theLastName)==0))
  120.          {
  121.             exist=true;
  122.  
  123.            return exist;
  124.          }
  125.          else {return exist;}
  126.          }}
  127.       void push_back (Person& newPerson)
  128.       { 
  129.       if( isExist( newPerson.getFirstName(), newPerson.getLastName())==false) 
  130.  
  131.           { 
  132.           vector<Person>::push_back(newPerson );
  133.           }
  134.       }
  135.       void print(){
  136.  
  137.            for(int i=0;i<=10;i++)
  138.            { 
  139.  
  140.                cout<<at(i).toString()<<endl;
  141.  
  142.  
  143.            }
  144.  
  145.       }
  146.  
  147.  
  148.  
  149.  
  150. };
  151. //////////////////////////////////////////////////////////////
  152.  
  153. int main(){
  154. Person p=Person("A","B",1,2);
  155. Database database; 
  156.  
  157. database.push_back(p);
  158.  
  159.  
  160.  
  161.              system("PAUSE");
  162.              return 0;
  163. }
  164.  

If someone can point out the reason why it crashes on run that would be great.

I figured out what makes this program crash is this line

117 a=at(i).getFirstName();

can anyone tell me how i can fix this line to make it work.
Feb 23 '12 #1
5 1786
Banfa
9,065 Expert Mod 8TB
I don't know about crashing I doubt it even compiles the function at line 41 is not complete.
Feb 23 '12 #2
I figured out what makes this program crash is this line

117 a=at(i).getFirstName();

can anyone tell me how i can fix this line to make it work.
Feb 24 '12 #3
Banfa
9,065 Expert Mod 8TB
The most likely reason is that you don't have 10 items in the vector so at some point you call at with an index that is out of range. Rather than use a hard coded size (10) use the size of the vector (call vector<>::size()).
Feb 24 '12 #4
Rabbit
12,516 Expert Mod 8TB
The array at() is never declared nor populated.
Feb 24 '12 #5
Banfa
9,065 Expert Mod 8TB
That is because at() isn't an array, if it was it would have to be at[].

What you are seeing is calls to vector<>::at(), Database sub-classes vector<> so has access to its member functions directly.
Feb 27 '12 #6

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

Similar topics

3
by: 8leggeddj | last post by:
Hello, I am having a problem when using access xp as a frontend for sql server 2000. I have been trying to update a number of stored procedures (Just simple adding fields etc) which results in...
0
by: Google Groups | last post by:
Hi, I have the following configuraiton: Server side: Apache 2.x with deflate module configured for cotet-stream. Client Side: IE 6.0.2 SP2 dot net framework v1.1.4322
3
by: TheSebaster | last post by:
I register to a com event under a win32 C# application. Here is the code I wrote: InstrumentUX.StorageControl pStorageControl = null; pStorageControl =...
0
by: Greg | last post by:
I've overriden the DateTimePicker control. Its been working fine so far, but when I override the Format property to hide it from the design time properties in the IDE, the IDE crashes when I try to...
0
by: =?Utf-8?B?QmFjaA==?= | last post by:
Hi, I have a ASP.NET 2.0 web service, which runs beautifully when hosted in the development server of VS2005 (out side of IIS) but crashes when hosted inside IIS 5.1 or IIS 6.0 when it takes...
1
by: drmcgee2000 | last post by:
Greetings, I saw a post from last year where VS 2005 crashes when editing CSS. I am wondering if anyone else is having the same issue, if there is a work around, or a fix (LOL) from Microsoft. ...
41
by: z | last post by:
I use Visual C 2005 to develop my programs. One in particular is crashing in very specific and hard to replicate situations, made worse by the fact it only crashes when run -outside- the dev - as...
7
by: whutto | last post by:
I have a VB.NET app that crashes when run from the Windows scheduled tasks, but runs fine when the run-as user is logged on. The app also runs fine from the scheduled jobs if the run as user is...
2
by: William Johnston | last post by:
Hello, My Web application crashes when using a MediaPlayer object. Any idea what's going on? Thanks, William Johnston
14
by: Steven Kogan | last post by:
I have a problem with Access 2007, SP2, on a particular PC. For a particular database, when using the Relationships view Access crashes sometimes when a table is added. It always crashes when I...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.