Problem with writing from file into linked list
Hi! I'm new on this forum but reading you at least 2 year's.
I have problem with inputing some data from file into linked list but I just can't find where's problem. Can you read my code and help me! Thanks!
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
|
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
struct SR_node{
string SR_nazivPro;
int SR_kolicinaProLager, SR_sifraPro;
SR_node *SR_next;
};
void SR_unos(SR_node *&SR_head)
{
char SR_odg;
string SR_word;
ofstream SR_output("SR_skladiste.txt", ios::app | ios::out);
if(SR_output.fail())
cout << "\n\n Opening file failed! ***Press ENTER*** ";
do{
SR_node *SR_temp = new SR_node;
cout << "\n\n Enter code of product (eg. 1234): ";
cin >> SR_temp->SR_sifraPro;
cout << "\n\n Enter name of product (eg. candy): ";
cin.sync();
getline(cin, SR_temp->SR_nazivPro);
cout << "\n\n Enter quantity of product (eg. 1234): ";
cin >> SR_temp->SR_kolicinaProLager;
if(SR_head == 0)
{
SR_head = SR_temp;
SR_head->SR_next = 0;
}
else
{
SR_temp->SR_next = SR_head;
SR_head = SR_temp;
}
SR_output << SR_temp->SR_sifraPro << setw(35) << SR_temp->SR_nazivPro << setw(15) << SR_temp->SR_kolicinaProLager << endl;
cout << "\n\n\t You want to continue with this operation? (D/N)";
cin >> SR_odg;
}while(SR_odg == 'd' || SR_odg == 'D');
SR_output.close();
}
void SR_ispis(SR_node *SR_head)
{
/*ifstream SR_input("SR_skladiste.txt", ios::in);
string SR_line;
if(SR_input.fail())
{
cout << "\n\n Opening file failed! ***Press ENTER*** ";
}
cout << "\n\nData in file: \n";
cout << "\n\nProduct code " << setw(25) << "Product name " << setw(25) << "Product quantity\n\n";
while(!SR_input.eof())
{
getline(SR_input, SR_line);
cout << SR_line + "\n";
}
SR_input.close();*/
cout << "\n\nData in list: \n";
cout << "\n\nProduct code " << setw(25) << "Product name " << setw(25) << "Product quantity\n\n";
if(SR_head == 0)
{
cout << "\n\t Linked list is empty!!";
}
else
{
while(SR_head != 0)
{
cout << setw(8) << SR_head->SR_sifraPro << setw(30) << SR_head->SR_nazivPro << setw(20) << SR_head->SR_kolicinaProLager << endl;
SR_head = SR_head->SR_next;
}
}
cout << "\n\n";
}
void outputList(SR_node *&SR_head)
{
ifstream SR_input("SR_skladiste.txt", ios::in);
string SR_line;
int SR_count = 0;
if(SR_input.fail())
{
cout << "\n\n Opening file failed!";
}
while(SR_input >> SR_line)
{
SR_node *SR_temp = new SR_node;
cout << SR_line << endl;
if(SR_count == 0)
{
//istringstream SR_buffer(SR_line);
//SR_buffer >> SR_temp->SR_sifraPro;
SR_temp->SR_sifraPro = atoi(SR_line.c_str());
SR_count++;
}
else if(SR_count == 1)
{
SR_temp->SR_nazivPro = SR_line;
SR_count++;
}
else if(SR_count == 2)
{
SR_temp->SR_kolicinaProLager = atoi(SR_line.c_str());
if(SR_head == 0)
{
SR_head = SR_temp;
SR_head->SR_next = 0;
}
else
{
SR_temp->SR_next = SR_head;
SR_head = SR_temp;
}
cout << endl;
SR_count = 0;
}
};
SR_input.close();
}
void SR_promjeni(SR_node *&SR_head)
{
SR_node *SR_hodac = SR_head;
int SR_kljuc = 0;
if(!SR_head)
cout << "\n\n\t Memory empty!!\n\n";
else
{
cout << "\n\n Enter code of product which you want to change: ";
cin >> SR_kljuc;
while(SR_kljuc != SR_hodac->SR_sifraPro)
{
SR_hodac = SR_hodac->SR_next;
}
cout << "\n\n Enter new code for choosen product: ";
cin >> SR_hodac->SR_sifraPro;
cout << "\n\n Enter new name for choosen product: ";
cin >> SR_hodac->SR_nazivPro;
cout << "\n\n Enter new quantity for choosen product: ";
cin >> SR_hodac->SR_kolicinaProLager;
}
}
void SR_brisi(SR_node *&SR_head)
{
SR_node *SR_hodac = SR_head, *SR_prethodni = SR_hodac;
int SR_kljuc = 0;
if(!SR_head)
cout << "\n\n\t Memory empty! \n\n";
else
{
cout << "\n\n Enter code of item you want to delete: ";
cin >> SR_kljuc;
while(SR_kljuc != SR_hodac->SR_sifraPro)
{
SR_prethodni = SR_hodac;
SR_hodac = SR_hodac->SR_next;
}
SR_prethodni->SR_next = SR_hodac->SR_next;
delete SR_hodac;
}
}
int main ()
{
SR_node *SR_head = 0;
int SR_in = 0;
cout << "\n\n Data reading.....! " << endl;
ifstream SR_file("SR_skladiste.txt");
if(SR_file.good())
outputList(SR_head);
cout << "\n\t Data reading succesfull! \n\n\n\n";
do{
cout << "\n Choose one of next: "
<< "\n\n - Choose '1' to input some data"
<< "\n\n - Choose '2' to change some data"
<< "\n\n - Choose '3' to delete some data"
<< "\n\n - Choose '4' to see all data"
<< "\n\n - Choose '0' to exit"
<< "\n\n\n Choice: " ;
cin >> SR_in;
switch(SR_in)
{
case 1:
{
SR_unos(SR_head);
break;
}
case 2:
{
SR_promjeni(SR_head);
break;
}
case 3:
{
SR_brisi(SR_head);
break;
}
case 4:
{
SR_ispis(SR_head);
break;
}
case 0:
{
exit(1);
break;
}
default: cout << "\n Sorry, you choose wrong! \n";
}
}while(SR_in != 0);
cin.sync();
cin.get();
return 0;
}
|
Thanks!!
Topic archived. No new replies allowed.