So i have to create program which will download cars from txt file and it will create database. File should have input: Brand Model Year so eg: BMW M3 1996 Mercedes CLA 2015 Fiat Punto 1999
if one of the lines will be improper, program should inform user about that and throw exception, but it should create vector with data for the rest of the data in the file.
So my code looks like this:
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <cstdlib>
#include <vector>
#include <sstream>
#include <regex>
using namespace std;
void modifier(int a);
class car
{
public:
string brand;
string model;
int year;
car(string one, string two, int three)
{
brand = one;
model = two;
year = three;
}
car()
{
brand = "Audi";
model = "R8";
year = 2017;
}
~car()
{
}
};
vector<car> database;
void changer()
{
cout << "Would you like to make any changes?" << endl;
cout << "Press 1 to change the value of any of the created cars" << endl;
cout << "Press 2 to add new car to the database" << endl;
cout << "Press 3 to delete specified car from the database" << endl;
cout << "Press 4 to list all card" << endl;
cout << "Press 5 to skip and generate output file" << endl;
int a;
cin >> a;
modifier(a);
}
void add(string s2, string s3, int s4)
{
car First;
First.brand = s2;
First.model = s3;
First.year = s4;
database.push_back(First);
}
void list()
{
cout << "=======================" << endl;
for (int i = 0; i < database.size(); i++)
{
cout << "Car " << i + 1 << " info - brand: " << database[i].brand << " model: " << database[i].model << " year: " << database[i].year << endl;
}
cout << "=======================" << endl;
}
void read_check(string file, ifstream &in)
{
while (!in.eof())
{
stringstream ss;
string s;
getline(in, s);
ss << s;
string s2, s3, s4, s5;
getline(ss, s2, ' ');
getline(ss, s3, ' ');
getline(ss, s4, ' ');
getline(ss, s5, ' ');
regex check("(\\d{4})");
try
{
if (s5.length())
{
throw 1;
}
else if (!s4.length())
{
throw 1;
}
else if (regex_match(s4, check))
{
int s6;
s6 = atoi(s4.c_str());
add(s2, s3, s6);
}
else
{
throw 1;
}
}
catch (int error)
{
cout << "Error exception 1: Wrong data - please check the input file!" << endl;
}
}
}
void mod1()
{
cout << "Which object would you like to change?" << endl;
int b;
cin >> b;
cout << "If you want to change brand press 1, if you want to change model press 2, if you want to change year press 3" << endl;
int c;
cin >> c;
cout << "Enter the new value for this field" << endl;
string d;
cin >> d;
if (c == 1)
{
database[b - 1].brand = d;
}
if (c == 2)
{
database[b - 1].model = d;
}
if (c == 3)
{
regex check("(\\d{4})");
try
{
if (regex_match(d, check))
{
int y = atoi(d.c_str());
database[b - 1].year = y;
}
else
{
throw 2;
}
}
catch (int error)
{
cout << "Error exception 2: You gave improper production year!" << endl;
cout << "=======================" << endl;
}
}
changer();
}
void mod2()
{
string d, e;
cout << "Specify the brand of the new car" << endl;
cin >> d;
cout << "Specify the model of the new car" << endl;
cin >> e;
cout << "Specify the production year of the new car" << endl;
string f;
cin >> f;
try
{
regex check("(\\d{4})");
if (regex_match(f, check))
{
int y = atoi(f.c_str());
add(d, e, y);
}
else
{
throw 2;
}
}
catch (int error)
{
cout << "Error exception 2: You gave improper production year!" << endl;
cout << "=======================" << endl;
}
changer();
}
void mod3()
{
cout << "Define which car would you like to delete" << endl;
int a;
cin >> a;
database.erase(database.begin() + a - 1);
changer();
}
void modifier(int a)
{
if (a == 1)
{
mod1();
}
else if (a == 2)
{
mod2();
}
else if (a == 3)
{
mod3();
}
else if (a == 4)
{
list();
changer();
}
else if (a == 5)
{
return;
}
else
{
cout << "Choose 1, 2 or 3!" << endl;
}
}
void saver()
{
cout << "Please enter the name of the output file" << endl;
string out;
cin >> out;
ofstream output(out.c_str());
for (int i = 0; i < database.size(); i++)
{
output << database[i].brand << " " << database[i].model << " " << database[i].year << " " << endl;
}
}
void open()
{
string namein;
cout << "Please enter the name of the file to open" << endl;
cin >> namein;
ifstream in(namein.c_str());
read_check(namein, in);
}
void main()
{
open();
list();
changer();
saver();
}
So i have added just changer(); in the 3rd line from the end. So in theory it should go to changer() function and it should print on the screen all options which the user has (to choose from 1 to 5). Unfortunately instead of this the program loops and this crap is printed: http://ift.tt/1YlLXU9
Can someone help me? Why it loops? I would like to go into changer function and ask user one more time to type in the integer if he want to proceed to some option in the menu.
Aucun commentaire:
Enregistrer un commentaire