My code doesn't output correctly using a .txt file. How do I clean this up so it posts correctly?
----------------------------------------------- CODE ----------------------------------------------
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <fstream>
using namespace std;
struct Customer {
int accountNumber;
string customerFullName;
string customerEmail;
double accountBalance;
};
void sortDesc(Customer* customerArray, int size);
void print(Customer customerArray[], int size);
void print(Customer customerArray[], int size)
{
cout << fixed << setprecision(2);
for (int i = 0; i < size; i++)
{
cout << "Account Number: "
<< customerArray[i].accountNumber << "\nName: "
<< customerArray[i].customerFullName
<< "\nEmail: " <<
customerArray[i].customerEmail << "\nBalance: " <<
customerArray[i].accountBalance << endl;
cout <<
"-------------------------------------" << endl;
}
}
int main()
{
Customer* customerArray;
int size;
//open file
ifstream infile("customers.txt");
if (!infile)
{
cout << "File not
found...";
return -1;
}
//read size form file
infile >> size;
infile.ignore();
customerArray = new Customer[size];
for (int i = 0; i < size; i++)
{
infile >>
customerArray[i].accountNumber;
infile.ignore(1000, '\n');
getline(infile,
customerArray[i].customerFullName);
getline(infile,
customerArray[i].customerEmail);
infile >>
customerArray[i].accountBalance;
}
//sort
sortDesc(customerArray, size);
print(customerArray, size);
//pause
//system("pause");
return 0;
}
//Function definition
void sortDesc(Customer customerArray[], int size)
{
for (int i = 0; i < size; i++)
{
for (int j = i; j < size;
j++)
{
if
(customerArray[i].accountBalance <
customerArray[j].accountBalance)
{
Customer tmp;
customerArray[i] = tmp;
customerArray[i] = customerArray[j];
customerArray[j] = tmp;
}
}
}
}
--------------------------- EXPECTED OUTPUT --------------------------
Account Number : 1333333
Name : Bill Bultman
Email : [email protected]
Balance : 120,000.00
-------------------------------------
Account Number : 1201077
Name : Jonathan I. Maletic
Email : [email protected]
Balance : 10,000.17
-------------------------------------
Account Number : 1991999
Name : John Smith
Email : [email protected]
Balance : 5,000.11
------------------------------ WHAT I PUT IN MY TXT FILE ---------------------------------
3
1201077
Jonathan I. Maletic
[email protected]
10,000.17
1991999
John Smith
[email protected]
5,000.11
1333333
Bill Bultman
[email protected]
120,000.00
/* Here is your error free code*/
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <fstream>
using namespace std;
struct Customer {
double accountNumber;
string customerFullName;
string customerEmail;
string accountBalance;
};
void sortDesc(Customer* customerArray, int size);
void print(Customer customerArray[], int size);
void print(Customer customerArray[], int size)
{
cout << fixed << setprecision(2);
for (int i = 0; i < size; i++)
{
cout << "Account Number: " <<
customerArray[i].accountNumber << "\nName: " <<
customerArray[i].customerFullName
<< "\nEmail: " << customerArray[i].customerEmail
<< "\nBalance: " << customerArray[i].accountBalance
<< endl;
cout << "-------------------------------------" <<
endl;
}
}
int main()
{
Customer* customerArray;
int size;
//open file
ifstream infile("a.txt");
if (!infile)
{
cout << "File not found...";
return -1;
}
//read size form file
infile >> size;
infile.ignore();
customerArray = new Customer[size];
for (int i = 0; i < size; i++)
{
infile >> customerArray[i].accountNumber;
infile.ignore(1000, '\n');
getline(infile, customerArray[i].customerFullName);
getline(infile, customerArray[i].customerEmail);
string word;
getline(infile, word);
for (int i1 = 0; i1 < word.length(); ++i1) {
if (word[i1] == ',')
word[i1] = '\0';
}
customerArray[i].accountBalance=word;
}
//sort
sortDesc(customerArray, size);
print(customerArray, size);
//pause
//system("pause");
return 0;
}
//Function definition
void sortDesc(Customer customerArray[], int size)
{
for (int i = 0; i < size; i++)
{
for (int j = i; j < size; j++)
{
if (stod(customerArray[i].accountBalance) <
stod(customerArray[j].accountBalance))
{
Customer tmp;
tmp=customerArray[i];
customerArray[i] = customerArray[j];
customerArray[j] = tmp;
}
}
}
}
My code doesn't output correctly using a .txt file. How do I clean this up so...
I am having trouble trying to output my file Lab13.txt. It will say that everything is correct but won't output what is in the file. Please Help Write a program that will input data from the file Lab13.txt(downloadable file); a name, telephone number, and email address. Store the data in a simple local array to the main module, then sort the array by the names. You should have several functions that pass data by reference. Hint: make your array large...
How do I complete my code using an external file? External file: data.txt //the number 6 is in the external file Incomplete code: #include <iostream> #include <fstream> using namespace std; class example { int data[1]; public: example(); void read(ifstream &); }; example::example() { ifstream infile; infile.open("data.txt"); } void example::read(ifstream &infile) { infile >> data[1]; cout << data[1] << endl; } int main() { example example, read; //system("pause"); return 0;...
Hi! 1. I need some help with sorting string in a text file. My goal is to 1 shift left strings for string.length time. I was able to do that successfully. However, I am also trying to sort, using insertion sort , the resulting shifts. I store all shifts in a vector (or is it better to use an array?!) , and try to sort them that way, but my output is just the shifted strings but not sorted. Can...
fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string> #include <iomanip> using namespace std; struct book { int ISBN; string Author; string Title; string publisher; int Quantity; double price; }; void choice1(book books[], int& size, int MAX_SIZE) { ifstream inFile; inFile.open("inventory.txt"); if (inFile.fail()) cout <<"file could not open"<<endl; string str; while(inFile && size < MAX_SIZE) { getline(inFile, str); books[size].ISBN = atoi(str.c_str()); getline(inFile, books[size].Title); getline(inFile, books[size].Author); getline(inFile, books[size].publisher); getline(inFile,...
Hi, I want to creat a file by the name osa_list that i can type 3 things and store there ( first name, last name, email). then i want to know how to recal them by modifying the code below. the names and contact information i want to store are 200. #include <iostream> #include <fstream> #include <cstdlib> #include <iomanip> using namespace std; class Student { private: string fname; string lname; string email; public: Student(); Student(string fname, string lname,...
C++ how can I fix these errors this is my code main.cpp #include "SpecialArray.h" #include <iostream> #include <fstream> #include <string> using namespace std; int measureElementsPerLine(ifstream& inFile) { // Add your code here. string line; getline(inFile, line); int sp = 0; for (int i = 0; i < line.size(); i++) { if (line[i] == ' ') sp++; } sp++; return sp; } int measureLines(ifstream& inFile) { // Add your code here. string line; int n = 0; while (!inFile.eof()) { getline(inFile,...
Can some one fix my code so that the output result same as below? BAR PLOT OF CYLINDER PRESSURE -VS- TIME pressure is on horizontal axis - units are psi time is on vertical axis - units are msec 0.0 20.0 40.0 60.0 80.0 100.0 120.0 +---------+---------+---------+---------+---------+---------+ 0.0|************************* 1.5|********************************* 3.0|***************************************** 4.4|************************************************** 05.9|********************************************* 7.4|******************************** 8.9|*********************** 10.4|***************** 11.9|************** 13.3|************* 14.8|************ 16.3|********* 17.8|******* 19.3|****** 20.7|****** 22.2|******* 23.7|******* .......... ===================== her is my code //#include"stdafx.h" #include #include #include #include #include #include using...
Hey, so i am trying to have my program read a text file using a structure but i also want to be able to modify the results(I kinda have this part but it could be better). I cant seem to get it to read the file(all the values come up as 0 and i'm not sure why because in my other program where it wrote to the txt file the values are on the txt file) i copied and pasted...
I need to make a few changes to this C++ program,first of all it should read the file from the computer without asking the user for the name of it.The name of the file is MichaelJordan.dat, second of all it should print ,3 highest frequencies are: 3 words that occure the most.everything else is good. #include <iostream> #include <map> #include <string> #include <cctype> #include <fstream> #include <iomanip> using namespace std; void addWord(map<std::string,int> &words,string s); void readFile(string infile,map<std::string,int> &words); void display(map<std::string,int>...
The code will not run and I get the following errors in Visual Studio. Please fix the errors. error C2079: 'inputFile' uses undefined class 'std::basic_ifstream<char,std::char_traits<char>>' cpp(32): error C2228: left of '.open' must have class/struct/union (32): note: type is 'int' ): error C2065: 'cout': undeclared identifier error C2065: 'cout': undeclared identifier error C2079: 'girlInputFile' uses undefined class 'std::basic_ifstream<char,std::char_traits<char>>' error C2440: 'initializing': cannot convert from 'const char [68]' to 'int' note: There is no context in which this conversion is possible error...