Scope of variables
Nov 27, 2015 at 5:45am UTC
Hello everyone I'm a newbie to c++ programming. I was given an assignment to create a address book with 4 functions.(adding new contact, delete contact, edit relationship based on address, and edit phone number based on the name of contact). However, when i export the file(to new_contact.txt), it cannot sense to changes of the contact book. Can someone please help?
Here's the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string peo;
string mob;
string add;
string rel;
string phone;
string format;
string change;
string personal;
int testfor1=-1;
int testfor2=-1;
int testfor3=-1;
int testfor4=-1;
int sup;
int test;
int orange;
int n;
test=2;
int az;
int sake;
int i=0;
int q;
int a;
int loop;
int ed;
int input;
cout<<"Options:" <<endl;
cout<<"1. Add a contact" <<endl;
cout<<"2. Delete a contact" <<endl;
cout<<"3. Edit relationship based on the address" <<endl;
cout<<"4. Edit the phone number of a contact" <<endl;
cout<<"0. Exit" <<endl;
while (test>1)
{
ifstream addressbook;
addressbook.open("contact.txt" );
int j=0;
string name[50];
string mobile[50];
string address[50];
string relationship[50];
while (addressbook>>name[i])
{
addressbook>>mobile[i]>>address[i]>>relationship[i];
j=j+1;
i=i+1;
}
cout<<"Select an option <0-4>: " ;
cin>>input;
switch (input)
{
case 0:
{
ofstream output;
output.open("new_contact.txt" );
{
for (ed=0;ed<i;ed++)
{
output<<name[ed]<<" " <<mobile[ed]<<" " <<address[ed]<<" " <<relationship[ed]<<endl;
}
}
return 0;
}
break ;
case 1:
{
int apple;
apple=0;
orange=0;
int pear;
pear=0;
int grape;
grape=0;
n=i;
string bu;
cout<<"Input the name, phone number, address, relationship one by one:" <<endl;
cin>>name[n];
cin>>mobile[n];
cin>>address[n];
cin>>relationship[n];
if (i>=49)
{
cout<<"Too many contacts!" <<endl;
}
else
{
for (a=0;a<i;a++)
{
if (name[a]!=name[n])
{
orange=1;
}
else
{
orange=0;
break ;
}
}
if (orange==0)
{
cout<<"The contact has already in the address book!" <<endl;
name[n]=" " ;
mobile[n]=" " ;
address[n]=" " ;
relationship[n]=" " ;
}
else
{
n++;
i++;
peo=name[n];
mob=mobile[n];
add=address[n];
rel=relationship[n];
testfor1=2;
}
}
}
break ;
case 2:
{
int vanish;
int x=0;
string temp;
string qwerty;
cout<<"Input the name of the contact you want to delete:" <<endl;
cin>>qwerty;
for (q=0;q<i;q++)
{
if (qwerty==name[q])
{
x=1;
break ;
}
else
{
x=0;
}
}
if (x==1)
{
cout<<qwerty<<" deleted!" <<endl;
for (int again=0;q<i;q++)
{
name[q]=name[q+1];
mobile[q]=mobile[q+1];
address[q]=address[q+1];
relationship[q]=relationship[q+1];
}
i=i-1;
testfor2=2;
}
else
{
cout<<"The contact doesn't exist!" <<endl;
}
}
break ;
case 3:
{
cout<<"Input the substring of the address you want to find and the relationship you want to change to:" <<endl;
cin>>change;
cin>>format;
for (int p=0;p<i;p++)
{
if (change==address[p])
{
relationship[p]=format;
testfor3=2;
}
else
{
continue ;
}
}
}
break ;
case 4:
{
cout<<"Input the name of the contact and the phone number you want to change to:" <<endl;
cin>>personal;
cin>>phone;
int lol;
lol=0;
az=0;
for (int cheers=0;az<i;az++)
{
if (personal!=name[az])
{
lol=1;
}
else
{
lol=0;
break ;
}
}
if (lol==0)
{
mobile[az]=phone;
testfor4=2;
}
else
{
cout<<"The contact doesn't exist!" <<endl;
}
lol=0;
}
break ;
}
}
}
Nov 27, 2015 at 8:07am UTC
You read from contact.txt but write to new_contact.txt. These are two different files.
Nov 27, 2015 at 8:36am UTC
The problem is that you read from the file ["contact.txt"] each time before you make changes, but you do not write those changes back [to "contact.txt"].
So either you read the data only once before the loop or you write back the changes.
Topic archived. No new replies allowed.