Question

My code doesn't output correctly using a .txt file. How do I clean this up so...

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          : Bill.bultman@uwc.edu

Balance        : 120,000.00

-------------------------------------

Account Number : 1201077

Name           : Jonathan I. Maletic

Email          : jmaletic@ksu.edu

Balance        : 10,000.17

-------------------------------------

Account Number : 1991999

Name           : John Smith

Email          : jsmith@uwrf.edu

Balance        : 5,000.11

------------------------------ WHAT I PUT IN MY TXT FILE ---------------------------------

3

1201077
Jonathan I. Maletic
jmaletic@ksu.edu
10,000.17
1991999
John Smith
jsmith@uwrf.edu
5,000.11
1333333
Bill Bultman
Bill.bultman@uwc.edu
120,000.00

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/* 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;
}
}
}
}

Add a comment
Know the answer?
Add Answer to:
My code doesn't output correctly using a .txt file. How do I clean this up so...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I am having trouble trying to output my file Lab13.txt. It will say that everything is...

    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...

    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...

    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>...

    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,...

  • I am reading a text file and trying to store the information into an array so...

    I am reading a text file and trying to store the information into an array so I can use this in different parts of my program. So a dynamic array. So far I have been able to retrieve the information from the text file and organize it by category (User name,User ID, User password) I am having troubles allocating an array and storing the information correctly. Since I have different data types (int, string) do I need to create a...

  • Hi, I want to creat a file by the name osa_list that i can type 3...

    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 <...

    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...

    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...

  • I am getting an error that the variable num was not declared in the scope on...

    I am getting an error that the variable num was not declared in the scope on the line of code that has; while(num != 5). Do you know how to fixe this error? #include <iostream> #include <iomanip> #include <string> #include<fstream> #include <cstring> using namespace std; //Declare the structure struct Games { string visit_team; int home_score; int visit_score; }; // Function prototypes int readData(Games *&stats); int menu(); void pickGame(Games *&stats, int size); void inputValid (Games *&stats, int size); void homeTotal(Games *&stats,...

  • Can someone please edit my current code to accept a user input for the text file...

    Can someone please edit my current code to accept a user input for the text file name in lieu of how it currently will read data1.txt. Also if the user enters a data file that isn't ready to be used, cout a statement "cout << "Could not open file " << userInputforData.txt << endl; Another condition that I need to add is if there is a special character anywhere in the word it cannot be an acceptable variable name. could...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT