This is messy because I am new to c++. I made a function that returns the value of a vector getting it's elements from a .txt file. I made seperate vectors to sort it but that is not relevant to this question.
vector<string> txtToArr(int c) {
ifstream unList;
unList.open("unorderedList");
vector<string> names;
vector<double> times;
vector<int> bNum;
string tempS;
double tempD;
int tempI;
// Adds data to their respective vectors
int count = 0;
while (!unList.eof()) {
if (count == 1) {
unList >> tempS;
names.push_back(tempS);
}
else if (count == 2) {
unList >> tempD;
times.push_back(tempD);
}
else if (count == 3) {
unList >> tempI;
bNum.push_back(tempI);
count = -1;
}
count++;
}
int small;
string temp;
double temp2;
int temp3;
// Sorts vectors using selection sort
for (int i = 0; i < c - 1; i++) {
small = i;
for (int j = i + 1; j < c; j++) {
if (times[j] < times[small]) {
small = j;
}
}
temp = names[i];
temp2 = times[i];
temp3 = bNum[i];
times[i] = times[small];
names[i] = names[small];
bNum[i] = bNum[small];
names[small] = temp;
times[small] = temp2;
bNum[small] = temp3;
}
// Compiles all of the vectors together
vector<string> all;
for (unsigned int i = 0; i < names.size(); i++) {
all.push_back(to_string(i+1));
all.push_back(names[i]);
all.push_back(to_string(times[i]));
all.push_back(to_string(bNum[i]));
}
unList.close();
return all;
}
I then tried outputting it to the console using the following:
// Outputs vectors (This is in the main btw)
int count2 = 0;
for (unsigned int i = 0; i < txtToArr(count).size(); i++) {
if (count2 == 3) {
cout << "\n";
count2 = -1;
}
cout << txtToArr(count).at(i) << " ";
count2++;
}
But for some reason it will not output to the console. I'm sure it's some minor mistake I made but I can't seem to find it.
Thanks for your help!
Here is the full code if it helps:
// This program takes your name and your fastest 5k(running) times. It puts that data in a .txt file, and reads that data.
// It then outputs a ranking compared to other people entered in and displays a randomly generated bib or racing number.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <string>
#include <vector>
using namespace std;
// Generates random bib number between 1 and 100
int bibNum() {
return rand() % 100 + 1;
}
vector<string> txtToArr(int c) {
ifstream unList;
unList.open("unorderedList");
vector<string> names;
vector<double> times;
vector<int> bNum;
string tempS;
double tempD;
int tempI;
// Adds data to their respective vectors
int count = 0;
while (!unList.eof()) {
if (count == 1) {
unList >> tempS;
names.push_back(tempS);
}
else if (count == 2) {
unList >> tempD;
times.push_back(tempD);
}
else if (count == 3) {
unList >> tempI;
bNum.push_back(tempI);
count = -1;
}
count++;
}
int small;
string temp;
double temp2;
int temp3;
// Sorts vectors using selection sort
for (int i = 0; i < c - 1; i++) {
small = i;
for (int j = i + 1; j < c; j++) {
if (times[j] < times[small]) {
small = j;
}
}
temp = names[i];
temp2 = times[i];
temp3 = bNum[i];
times[i] = times[small];
names[i] = names[small];
bNum[i] = bNum[small];
names[small] = temp;
times[small] = temp2;
bNum[small] = temp3;
}
// Compiles all of the vectors together
vector<string> all;
for (unsigned int i = 0; i < names.size(); i++) {
all.push_back(to_string(i+1));
all.push_back(names[i]);
all.push_back(to_string(times[i]));
all.push_back(to_string(bNum[i]));
}
unList.close();
return all;
}
int main()
{
// Only prints out 2 digits after the decimal
cout << setprecision(2) << fixed;
// Variables
string name;
double PR;
int input;
int count = 0;
ofstream unorderedList;
unorderedList.open("unorderedList.txt");
cout << "This program takes your name and your fastest 5k(running) times.\nIt puts that data in a .txt file, and reads that\ndata. It then outputs a ranking compared to other people entered\nin and displays a randomly generated bib or racing number.\n\n";
do {
count++;
unorderedList << count << " ";
cout << "Enter your name: ";
cin >> name;
unorderedList << name << " ";
cout << "\n\nEnter in your fastest 5k. For example 17:41 = 17.41: ";
cin >> PR;
unorderedList << PR << " " << bibNum() << "\n";
cout << "1 to continue entering times / 2 to see the data";
cin >> input;
} while (input == 1);
if (input == 2) {
// Outputs vectors
int count2 = 0;
for (unsigned int i = 0; i < txtToArr(count).size(); i++) {
if (count2 == 3) {
cout << "\n";
count2 = -1;
}
cout << txtToArr(count).at(i) << " ";
count2++;
}
exit(0);
}
}
Aucun commentaire:
Enregistrer un commentaire