19.05.2014 / 20:57 |  | 
Pauk52    Пользователь  
   Сейчас: Offline 
 Имя: Володимир Откуда: Пески-радьковские Регистрация: 25.01.2011
   | делаю лабу с использованием структур и накнулся на проблему удаления елементов из vector, способом котрым я удаляю элемент а пыхает туда мусор   #include <iostream>  
#include <string>  
#include <vector>  
#include <iterator>  
#include <algorithm>  
#include <stdlib.h>   
using namespace std;  
   
int count=0;  
   
struct student{  
    string name;  
    int firstMark;  
    int secondMark;  
    int thirdMark;  
};  
   
student get_student(){  
    string buffer;  
    int buff;  
    student studentBase;  
   
    cout << "Enter student name\n";  
    cin >> buffer;  
studentBase.name = buffer;  
    cout << "Enter student first mark\n";  
    cin >> buff;  
studentBase.firstMark = buff;  
    cout << "Enter student second mark\n";  
    cin >> buffer;  
studentBase.secondMark = buff;  
    cout << "Enter student third mark\n";  
    cin >> buffer;  
studentBase.thirdMark = buff;  
return studentBase;  
}  
   
void showStudent(int id, const vector<student> & base){  
    cout << "Student name is " << base[id].name << '\n';  
    cout << "mark 1 " << base[id].firstMark << '\n';  
    cout << "mark 2 " << base[id].secondMark << '\n';  
    cout << "mark 3 " << base[id].thirdMark << "\n\n";  
}  
int main(int argc, char **argv){  
    vector<student> studentBase;  
    int chose;  
    bool contin = true;  
    while(contin){  
        cout << "***Welcome in Student DataBase***\n";  
        cout << "Please chose what you want\n";  
        cout <<"1.Add Student\n";  
        cout <<"2.Show Student\n";  
        cout <<"3.delete Student\n";  
        cout <<"4.Exit\n";  
        cin >> chose;  
        switch(chose){  
            case 1:  
                studentBase.push_back(get_student());  
                break;  
            case 2:  
                int sid;  
                cin >> sid;  
                showStudent(sid,studentBase);  
                break;  
            case 3:  
                int idx;  
                cin >> idx;  
                studentBase.erase(studentBase.begin() + idx);  
                break;  
        }  
   
    }  
    return 0;  
}  
 вывод: [vova@lenovo struct]$ ./main   
***Welcome in Student DataBase***  
Please chose what you want  
1.Add Student  
2.Show Student  
3.delete Student  
4.Exit  
1  
Enter student name  
qw  
Enter student first mark  
1  
Enter student second mark  
2  
Enter student third mark  
3  
***Welcome in Student DataBase***  
Please chose what you want  
1.Add Student  
2.Show Student  
3.delete Student  
4.Exit  
3  
0  
***Welcome in Student DataBase***  
Please chose what you want  
1.Add Student  
2.Show Student  
3.delete Student  
4.Exit  
2  
0  
Student name is qw,�G    �  
  | 
  23.05.2014 / 12:40 |  | 
dzanis    Пользователь  
   Сейчас: Offline 
 Имя: Жаник Откуда: Rezekne Регистрация: 18.11.2010
   | Выход за пределы ведь.В примере то удален весь список. Проверь так [code] [/code]try { ...      ...        cout << "Student name is " << base.at(id).name << '\n'; } catch (std::out_of_range& oor) {              cout << "error  : out ofrange id " <<id<< '\n'; }
   | 
  26.05.2014 / 19:20 |  | 
Gaucer    Пользователь  
   Сейчас: Offline 
 Имя: Денис Регистрация: 23.04.2012
   | привет. пишу вот прогу,но она после ввода первых данных закрывается. пишу в Qt Creator #include <iostream>  
#include <stdio.h>  
#include <string.h>  
using namespace std;  
   
class mainer {  
public:  
   
   struct _man {  
       char name[200], city[70], street[100], num[12], pre[5];  
   }Man;  
struct _man *m;  
mainer() {  
    cout << "Please enter smth";  
}  
};  
   
class city:public mainer {  
public:  
    city() {  
        cout << endl << "Enter City: ";  
        cin >> m->city;  
    }  
};  
   
class name:public mainer {  
public:  
    name() {  
        cout << endl << "Enter Name: ";  
        cin >> m->name;  
    }  
};  
   
class street:public mainer {  
public:  
    street() {  
        cout << endl << "Enter Street: ";  
        cin >> m->street;  
        }  
 };  
   
class num:public mainer {  
public:  
    num() {  
        cout << endl << "Enter Prefix and Number: ";  
        cin >> m->pre >> m->num;  
    }  
};  
   
class Book:public name, city, street, num {  
};  
   
   
int main() {  
Book user;  
return 0;  
}  
  | 
  26.05.2014 / 22:08 |  | 
dzanis    Пользователь  
   Сейчас: Offline 
 Имя: Жаник Откуда: Rezekne Регистрация: 18.11.2010
   | Gaucer, а потому что  тут ничего не вызывает  int main() {  
Book user;  
return 0;  
}  
  | 
  26.05.2014 / 22:22 |  | 
Gaucer    Пользователь  
   Сейчас: Offline 
 Имя: Денис Регистрация: 23.04.2012
   | dzanis, а что вызвать-то? стоит конструктор же..
   | 
  26.05.2014 / 22:43 |  | 
Ксакеп    Модератор форума  
   Сейчас: Offline 
 Регистрация: 20.06.2012
   | Всё гуд, ребят, но кидайте в спойлеры ([spoiler][/..]) длинный код, пожалуйста.
   Изменено Ксакеп (26.05 / 22:44) (всего 1 раз) | 
  27.05.2014 / 01:38 |  | 
dzanis    Пользователь  
   Сейчас: Offline 
 Имя: Жаник Откуда: Rezekne Регистрация: 18.11.2010
   | Gaucer,  Ну так чему там работать? Поле user не юзается,конструкторы разрушаются пока вводишь первый вопрос.QT не стабильный ващето.Напиши всем класам поля,и ваше так не программируй никогда - класс создавать для одного вопроса.Слишком классов много,хватит и одной структуры book.Отправь вопрос  в спец метод который вернет ответ,типа book user;  user.name = get("Enter name")  и т.д
   | 
  3.06.2014 / 19:30 |  | 
Gaucer    Пользователь  
   Сейчас: Offline 
 Имя: Денис Регистрация: 23.04.2012
   | и снова я Открыть спойлер Закрыть спойлер #include <iostream>  
#include <stdio.h>  
#include <string.h>  
using namespace std;  
   
class Match {  
public:  
    struct _table {  
       char team1[50], team2[50], time[5], date[5], stadium[50];  
    };  
    _table *tab;  
   
    void h_team() {  
    cout << "Enter home team: ";  
    cin.getline(tab->team1, 50);  
    //cout << tab->team1 << endl;  
    }  
    void g_team() {  
    cout << "Enter guest team: ";  
    cin.getline(tab->team2, 50);  
    //cout << tab->team2 << endl;  
    }  
    void date() {  
    cout << "Enter date of the match (30/12): ";  
    cin.getline(tab->date, 6);  
    //cout << tab->date << endl;  
    }  
    void time() {  
    cout << "Enter time of the match (00:00): ";  
    cin.getline(tab->time, 6);  
    //cout << tab->time << endl;  
    }  
    void stad() {  
    cout << "Enter stadium: ";  
    cin.getline(tab->stadium, 50);  
    //cout << tab->stadium << endl;  
    }  
    void print() {  
    perror("Program returns");  
    cout << tab->team1 << " - " << tab->team2 << ", " << tab->date << ", " << tab->time << ", " << tab->stadium;  
    }  
   
};  
   
int main() {  
Match One;  
One.h_team();  
One.g_team();  
One.date();  
One.time();  
One.stad();  
One.print();  
   
return 0;  
}  
 программа не выводит данные через функцию print(). Perror() возвращает succes. Выводил данные сразу после ввода, всё норм  | 
  4.06.2014 / 10:51 |  | 
dzanis    Пользователь  
   Сейчас: Offline 
 Имя: Жаник Откуда: Rezekne Регистрация: 18.11.2010
   | Невижу чтоб у тебя консоль останавливалась.Может так принято на qt,но на сях консоль сразу закрывается и увидеть что там пишется просто неуспеть.А так я больше невижу косякав в коде,все таки я бы не юзал qt для таких простых вешей,в которых не нужны плотные циклы вычеслений.
   | 
  4.06.2014 / 14:35 |  | 
Gaucer    Пользователь  
   Сейчас: Offline 
 Имя: Денис Регистрация: 23.04.2012
   | dzanis, не, он просит нажать Ввод, чтоб выйти. Но последнюю строчку не выводит
   |