Reputation: 33
I´ve made a program in which people create an auction which is then saved as a .txt file that looks like this:
Auction name: Books
Item to sell: Lord of the rings
Description: words........
Reserve price: 12
Then, on a separate project, I made a similar thing but with Bids which are also saved on a .txt file with the structure:
// "personName.txt"
24 ( <-- Example number of the person's bid)
The problem is that now I have to make multiple "bid" files and compare them with each other and the auction file to determine who won the auction.
How can this be accomplished?
Upvotes: 1
Views: 75
Reputation: 664
I'm not sure why you'd do this task with files, as it is very expensive to load files into the program. However, your task can be accomplished (that is if I accurately understand what you're asking; your question was not written very clearly).
First, make sure to include your proper headers from the STL
#include <iostream> // Assuming you have console IO functionality
#include <filesystem> // Accessing all files in a directory for searching
#include <fstream> // Loading files into the program
#include <string> // Assuming you have names for bidders
#include <vector> // Load the files' contents into a vector
As you may be able to somewhat tell from the includes and their explanations, here's my plan for the program:
std::filesystem::directory_iterator
, search through all the files in the directory from above and save their contents to an std::vector
In practice, that means we may want a vector of structs that have (1) the name of the bidder and (2) their bid. Then we compare all their bids using a max function (like stated above). When we find the max, we will be able to very easily find the name bidder that goes with that bid and output to the screen.
The .txt
files (for my solution) have strictly two lines per file. The first line is the bidder's name, and the second line is the bidder's bid. This will line up exactly with the struct we'll use for loading these files into the program.
So, here's the program (make sure you're using C++17
):
#include <filesystem> // std::filesystem : path, directory_iterator, current_path, parent_path
#include <fstream> // std::ifstream
#include <iomanip> // std::setpercision
#include <iostream> // std::cout, std::cerr, std::endl
#include <limits> // std::numeric_limits
#include <string> // std::string
#include <vector> // std::vector
namespace fs = std::filesystem;
// The max size of a file stream saved into this variables
constexpr auto STREAM_MAX = std::numeric_limits<std::streamsize>::max();
// The directory to the folder with all the bidders in it
// What I've done here is used the a folder in the same parent
// directory as this file. So we go one step out of the
// current path, then go into the "bids" folder. However,
// I compiled this in a different directory hierarchy, so I
// commented the '.parent_path()' part out.
const auto BIDDING_PATH =
fs::path(fs::current_path()/*.parent_path()*/ / "bids");
/**
This will be the struct we use in the vector
@param name The name of the bidder
@param bid The price of the bidder's bid
*/
struct Bidder
{
std::string name;
double bid;
// Define a constructor for std::vector::emplace_back
Bidder(std::string name, double bid) :
name(std::move(name)), bid(bid)
{}
};
std::vector<Bidder> load_bids()
{
std::vector<Bidder> all_bidders;
// Loop through every file in the 'BIDDING_PATH' directory
// (not recursively)
for (auto file : fs::directory_iterator(BIDDING_PATH))
{
std::ifstream cur_file(file.path().c_str());
if (cur_file.is_open())
{ // Load file into a struct and add it to `all_bidders`
std::string cur_name;
double cur_bid;
// Read the contents of the file into the above variables
std::getline(cur_file, cur_name);
cur_file >> cur_bid;
// Now get rid of the newline character left behind by the '>>'
// operator from 'cur_file >> cur_bid'
cur_file.ignore(STREAM_MAX, '\n');
// Add the bidder to the `all_bidders` vector
all_bidders.emplace_back(cur_name, cur_bid);
// Some debugging feedback
std::cout
<< "File loaded: " << file.path().string() << '\n'
<< "Bidder's name: " << all_bidders.back().name << '\n'
<< "Bidder's bid: " << all_bidders.back().bid << '\n'
<< std::endl;
}
else
{
std::cerr
<< "Error opening file: " << file.path().string()
<< std::endl;
}
}
return all_bidders;
}
Bidder find_max_bidder(const std::vector<Bidder>& bidders)
{
// Keeps track of the winning bidder
// Intially set to a null value
Bidder max_bidder("", -1.0);
for (const auto& cur_bidder : bidders)
{
if (cur_bidder.bid > max_bidder.bid)
{
max_bidder.name = cur_bidder.name;
max_bidder.bid = cur_bidder.bid;
}
}
// Check if no max was found
if (max_bidder.bid == -1.0)
{
std::cerr
<< "Error: No max bidder was found\n"
<< "Are there any bidding files in the given directory?\n"
<< std::endl;
}
return max_bidder;
}
int main()
{
// Load all bidders into the `all_bidders` vector, then
// calculate the max bid
std::vector<Bidder> all_bidders = load_bids();
Bidder winning_bidder = find_max_bidder(all_bidders);
// Print out the max bid
std::cout
<< "The winner of this auction is " << winning_bidder.name << "!\n"
<< "They bid $" << winning_bidder.bid << std::setprecision(2) << "!\n"
<< std::endl;
// Keep the console from closing
std::cin.get();
return 0;
}
Please note a few things:
C++17
; I used MSVC with Visual Studio 2019#include
s that weren't needed to explain our gameplan, but I added for formatting or making my life easier or something// Some debugging feedback
if you wantAdditionally, I generated a random set of data with this Batch file:
@echo off
setlocal EnableDelayedExpansion
set wkdir=!cd!
goto :begin
:begin
cd "..\bids"
set rand_bid=
for /l %%a in (1,1,100) do (
echo Bidder%%a > bidder%%a.txt
set /a rand_bid=!random! %% 1000
echo !rand_bid!.00 >> bidder%%a.txt
)
cd !wkdir!
goto :end
:end
exit
After running that Batch file and then running my program, here was my output:
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder1.txt
Bidder's name: Bidder1
Bidder's bid: 219
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder10.txt
Bidder's name: Bidder10
Bidder's bid: 693
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder100.txt
Bidder's name: Bidder100
Bidder's bid: 275
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder11.txt
Bidder's name: Bidder11
Bidder's bid: 669
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder12.txt
Bidder's name: Bidder12
Bidder's bid: 720
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder13.txt
Bidder's name: Bidder13
Bidder's bid: 741
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder14.txt
Bidder's name: Bidder14
Bidder's bid: 342
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder15.txt
Bidder's name: Bidder15
Bidder's bid: 124
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder16.txt
Bidder's name: Bidder16
Bidder's bid: 627
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder17.txt
Bidder's name: Bidder17
Bidder's bid: 244
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder18.txt
Bidder's name: Bidder18
Bidder's bid: 61
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder19.txt
Bidder's name: Bidder19
Bidder's bid: 79
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder2.txt
Bidder's name: Bidder2
Bidder's bid: 979
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder20.txt
Bidder's name: Bidder20
Bidder's bid: 211
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder21.txt
Bidder's name: Bidder21
Bidder's bid: 168
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder22.txt
Bidder's name: Bidder22
Bidder's bid: 44
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder23.txt
Bidder's name: Bidder23
Bidder's bid: 902
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder24.txt
Bidder's name: Bidder24
Bidder's bid: 753
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder25.txt
Bidder's name: Bidder25
Bidder's bid: 975
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder26.txt
Bidder's name: Bidder26
Bidder's bid: 220
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder27.txt
Bidder's name: Bidder27
Bidder's bid: 576
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder28.txt
Bidder's name: Bidder28
Bidder's bid: 541
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder29.txt
Bidder's name: Bidder29
Bidder's bid: 249
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder3.txt
Bidder's name: Bidder3
Bidder's bid: 788
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder30.txt
Bidder's name: Bidder30
Bidder's bid: 661
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder31.txt
Bidder's name: Bidder31
Bidder's bid: 98
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder32.txt
Bidder's name: Bidder32
Bidder's bid: 307
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder33.txt
Bidder's name: Bidder33
Bidder's bid: 698
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder34.txt
Bidder's name: Bidder34
Bidder's bid: 287
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder35.txt
Bidder's name: Bidder35
Bidder's bid: 489
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder36.txt
Bidder's name: Bidder36
Bidder's bid: 685
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder37.txt
Bidder's name: Bidder37
Bidder's bid: 196
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder38.txt
Bidder's name: Bidder38
Bidder's bid: 321
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder39.txt
Bidder's name: Bidder39
Bidder's bid: 332
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder4.txt
Bidder's name: Bidder4
Bidder's bid: 267
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder40.txt
Bidder's name: Bidder40
Bidder's bid: 593
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder41.txt
Bidder's name: Bidder41
Bidder's bid: 485
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder42.txt
Bidder's name: Bidder42
Bidder's bid: 863
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder43.txt
Bidder's name: Bidder43
Bidder's bid: 869
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder44.txt
Bidder's name: Bidder44
Bidder's bid: 612
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder45.txt
Bidder's name: Bidder45
Bidder's bid: 165
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder46.txt
Bidder's name: Bidder46
Bidder's bid: 889
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder47.txt
Bidder's name: Bidder47
Bidder's bid: 554
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder48.txt
Bidder's name: Bidder48
Bidder's bid: 783
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder49.txt
Bidder's name: Bidder49
Bidder's bid: 747
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder5.txt
Bidder's name: Bidder5
Bidder's bid: 966
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder50.txt
Bidder's name: Bidder50
Bidder's bid: 322
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder51.txt
Bidder's name: Bidder51
Bidder's bid: 775
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder52.txt
Bidder's name: Bidder52
Bidder's bid: 306
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder53.txt
Bidder's name: Bidder53
Bidder's bid: 376
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder54.txt
Bidder's name: Bidder54
Bidder's bid: 895
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder55.txt
Bidder's name: Bidder55
Bidder's bid: 374
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder56.txt
Bidder's name: Bidder56
Bidder's bid: 166
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder57.txt
Bidder's name: Bidder57
Bidder's bid: 332
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder58.txt
Bidder's name: Bidder58
Bidder's bid: 665
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder59.txt
Bidder's name: Bidder59
Bidder's bid: 177
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder6.txt
Bidder's name: Bidder6
Bidder's bid: 888
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder60.txt
Bidder's name: Bidder60
Bidder's bid: 399
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder61.txt
Bidder's name: Bidder61
Bidder's bid: 271
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder62.txt
Bidder's name: Bidder62
Bidder's bid: 996
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder63.txt
Bidder's name: Bidder63
Bidder's bid: 487
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder64.txt
Bidder's name: Bidder64
Bidder's bid: 874
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder65.txt
Bidder's name: Bidder65
Bidder's bid: 499
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder66.txt
Bidder's name: Bidder66
Bidder's bid: 72
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder67.txt
Bidder's name: Bidder67
Bidder's bid: 902
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder68.txt
Bidder's name: Bidder68
Bidder's bid: 66
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder69.txt
Bidder's name: Bidder69
Bidder's bid: 64
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder7.txt
Bidder's name: Bidder7
Bidder's bid: 246
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder70.txt
Bidder's name: Bidder70
Bidder's bid: 167
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder71.txt
Bidder's name: Bidder71
Bidder's bid: 961
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder72.txt
Bidder's name: Bidder72
Bidder's bid: 250
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder73.txt
Bidder's name: Bidder73
Bidder's bid: 280
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder74.txt
Bidder's name: Bidder74
Bidder's bid: 185
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder75.txt
Bidder's name: Bidder75
Bidder's bid: 962
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder76.txt
Bidder's name: Bidder76
Bidder's bid: 174
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder77.txt
Bidder's name: Bidder77
Bidder's bid: 157
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder78.txt
Bidder's name: Bidder78
Bidder's bid: 221
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder79.txt
Bidder's name: Bidder79
Bidder's bid: 312
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder8.txt
Bidder's name: Bidder8
Bidder's bid: 189
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder80.txt
Bidder's name: Bidder80
Bidder's bid: 842
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder81.txt
Bidder's name: Bidder81
Bidder's bid: 415
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder82.txt
Bidder's name: Bidder82
Bidder's bid: 313
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder83.txt
Bidder's name: Bidder83
Bidder's bid: 220
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder84.txt
Bidder's name: Bidder84
Bidder's bid: 200
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder85.txt
Bidder's name: Bidder85
Bidder's bid: 738
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder86.txt
Bidder's name: Bidder86
Bidder's bid: 323
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder87.txt
Bidder's name: Bidder87
Bidder's bid: 529
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder88.txt
Bidder's name: Bidder88
Bidder's bid: 196
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder89.txt
Bidder's name: Bidder89
Bidder's bid: 991
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder9.txt
Bidder's name: Bidder9
Bidder's bid: 917
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder90.txt
Bidder's name: Bidder90
Bidder's bid: 441
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder91.txt
Bidder's name: Bidder91
Bidder's bid: 691
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder92.txt
Bidder's name: Bidder92
Bidder's bid: 578
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder93.txt
Bidder's name: Bidder93
Bidder's bid: 234
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder94.txt
Bidder's name: Bidder94
Bidder's bid: 638
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder95.txt
Bidder's name: Bidder95
Bidder's bid: 569
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder96.txt
Bidder's name: Bidder96
Bidder's bid: 267
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder97.txt
Bidder's name: Bidder97
Bidder's bid: 788
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder98.txt
Bidder's name: Bidder98
Bidder's bid: 790
File loaded: C:\Dev\C++\Others\DevBox\Testing\bids\bidder99.txt
Bidder's name: Bidder99
Bidder's bid: 781
The winner of this auction is Bidder62 !
They bid $996!
I hope that helps! If you have any questions, feel free to ask!
Upvotes: 1