Someone just answer for me, but it only got 25%,please change the code below,or
ID: 3784031 • Letter: S
Question
Someone just answer for me, but it only got 25%,please change the code below,or make up your own:
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <stdlib.h>
#include <cstdlib>
using namespace std;
struct saleItem{
string type;
int price; //e.g 100, 2987
string itemStatus; //e.g wanted or for sale.... false or 0 is "for sale"
//saleItem(){}; //constructor
};
int main(int argc, char *argv[]) {
int index = 0;
int counter=0;
int itemsSold=0;
int leftOver=0;
saleItem itemArray[100]; //array from txt file
saleItem wantItemArray[100];
saleItem emptySaleItem;
emptySaleItem.type = "";
emptySaleItem.itemStatus = "no";
emptySaleItem.price = 0;
saleItem indexItem;
for (int r=0; r<100; r++){ //clears array
itemArray[r] = emptySaleItem;
wantItemArray[r] = emptySaleItem;
counter++;
}
if (argc==2){ //checks for argument
ifstream inFile;
string data; // create an instance
inFile.open(argv[1]); //open the file from argument
if (inFile.good()){ //file open error check
//cout<<"opened successfully"<<endl;
while (getline(inFile, data)) { //read / get every line of the file & store it
string token;
stringstream ss(data); // create a string stream variable from string data
ss << data;
int indexTwo = 0;
while (getline(ss, token, ',')){ //delimits by line
if (indexTwo == 0){
indexItem.type = token;
}
else if(indexTwo == 1){
indexItem.itemStatus = token;
}
else {
indexItem.price = std::stoi(token);
}
indexTwo++;
counter++;
}
if (indexItem.itemStatus == " wanted"){
wantItemArray[index]=indexItem;
itemArray[index]=emptySaleItem;
}
else{
itemArray[index]=indexItem;
wantItemArray[index]=emptySaleItem;
}
ss.clear();
index++;
counter++;
}
Explanation / Answer
i think this will work fine
#include<iostream>
#include<iomanip>
#include<fstream>
#include<string>
#include<sstream>
#include<vector>
using namespace std;
struct transaction
{
string item;
int cost;
};
int main()
{
transaction buy[100];
transaction sell[100];
int price;
int w, s;
ifstream myfile.open ("messageBoard.txt");
ofstream results.open ("results.txt");
string line;
}
myfile >> getline(myfile, buy[w], ' ');
if
if(!myfile)
{
cout<<"Error opening input file"<<endl;
return -1;
}
while(myfile, line)
{
if(line.find("wanted") != string::npos);
{
getline(myfile,buy[w].item);
getline(myfile,price);
stringstream(price) >> buy[w].cost;
}
}
while(myfile, line)
{
if(line.find("for sale") != string::npos);
{
getline(myfile,buy[s].item);
getline(myfile,price);
stringstream(price) >> buy[s].cost;
}
}
for(w=0; w<100; w++)
{
for(s=0; s<100; s++)
{
if(buy[w].item == sell[s].item && buy[w].cost <= sell[s].cost)
if (results.is_open())
{
results << buy[w] << " matches " << sell[s] << " ";
}
else
cout << "Unable to open output file." << endl;
buy[w] = 0;
sell[s] = 0;
}
}
results.close();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.