делает
while (fin >> name >> var1 >> var2 >> var3)
{
cout << name << var1 << var2 << var3 << endl;
}
вы все время переписываете одни и те же переменные, вам нужно поместить значение в вектор, как вы говорите в своем вопросе
вам также необходимо проверить правильность каждого ввода, в настоящее время вы не обнаруживаете недопустимый ввод
и, конечно, вам нужно проверить, что вы открываете файл
Пример :
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
int main()
{
std::ifstream fin("inputFile.txt");
if (!fin.is_open()) {
std::cerr << "cannot open inputFile.txt" << std::endl;
return -1;
}
int numberOfStates, numberOfSymbols, numberOfFinalStates;
if ((! (fin >> numberOfStates >> numberOfSymbols >> numberOfFinalStates))
|| (numberOfStates < 0)
|| (numberOfSymbols < 0)
|| (numberOfFinalStates < 0)) {
std::cerr << "invalid file" << std::endl;
return -1;
}
// final states are int, so memorize them in a vector of int
// because their number is known I can size it rather than
// to 'push_back' each value
std::vector<int> finalStates(numberOfFinalStates);
for (int & ft : finalStates) {
if (! (fin >> ft)) {
std::cerr << "invalid file reading final states" << std::endl;
return -1;
}
}
int numberOfTransitions;
if (!(fin >> numberOfTransitions) || (numberOfTransitions < 0)) {
std::cerr << "invalid file reading the number of transitions" << std::endl;
return -1;
}
// you say a transition contains 2 int and a char,
// I define a structure for
// i1 i2 and c are 'poor' names but I don't know their goal
struct Transition {
int i1, i2;
char c;
};
// the transitions saved in a vector
std::vector<Transition> transitions(numberOfTransitions);
for (Transition & tr : transitions) {
if (!(fin >> tr.i1 >> tr.i2 >> tr.c)) {
std::cerr << "invalid file reading transitions" << std::endl;
return -1;
}
}
// print to check
std::cout << "numberOfStates=" << numberOfStates << std::endl;
std::cout << "numberOfSymbols=" << numberOfSymbols << std::endl;
std::cout << "numberOfFinalStates=" << numberOfFinalStates << "\nfinalStates:";
for (int ft : finalStates)
std::cout << ' ' << ft;
std::cout << std::endl;
std::cout << "numberOfTransitions=" << numberOfTransitions
<< "\ntransitions:" << std::endl;
for (const Transition & tr : transitions)
std::cout << '\t' << tr.i1 << ' ' << tr.i2 << ' ' << tr.c << std::endl;
return 0;
}
Компиляция и исполнение:
[email protected]:/tmp $ g++ -pedantic -Wextra -Wall a.cc
[email protected]:/tmp $ cat inputFile.txt
10
3
1
9
12
0 1 f
0 3 f
1 2 a
2 9 f
3 4 f
3 8 f
4 5 b
5 6 f
6 7 a
8 9 f
[email protected]:/tmp $ ./a.out
invalid file reading transitions
Ошибка возникает из-за того, что файл содержит только 10 переходов, а не 12 ожидаемых, что приводит к изменению файла для ожидания 10 переходов:
[email protected]:/tmp $ ./a.out
numberOfStates=10
numberOfSymbols=3
numberOfFinalStates=1
finalStates: 9
numberOfTransitions=10
transitions:
0 1 f
0 3 f
1 2 a
2 9 f
3 4 f
3 8 f
4 5 b
5 6 f
6 7 a
8 9 f
[email protected]:/tmp $