Question

Hi I was hoping to get some help with my code for this problem. I don't...

Hi I was hoping to get some help with my code for this problem. I don't know how to correctly read a txt file and the difference between using string and cstring. My code is at the bottom please help thanks.

1. Create the following “data.txt” file. Each record consists of customer's name and their balance:

Maria Brown 2100.90

Jeffrey Jackson 200.20

Bernard Smith 223.10

Matthew Davenport 1630.20

Kimberly Macias 19100.90

Amber Daniels 2400.40

The records in the file will be like the text above. You will store them in the struct provided below:

struct PERSON {

   char Name[20];

   float Balance; };

i. We don’t know exactly how many records are in the file. You will have to go through the file a first time and count the number of records (N).

ii . Once you have this information, you can create an array that is large enough to hold all of the records in the file PERSON P[N];

iii. Remember that c_strings or character arrays must be null terminated, so you will either need to set every element to ‘/0’ before writing the name, or make sure you put one after you have written the name.

iv. Display all records in array a. Use calling statement: Display(a, N);

Your output should look like the following:

                Name            Balance

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

Maria Brown 2100.90

Jeffrey Jackson 200.20

Bernard Smith 223.10

Matthew Davenport 1630.20

Kimberly Macias 19100.90

Amber Daniels 2400.40

////////////////////////////my code

#include <iostream>
#include <string>
#include <cstring>
#include <fstream>
using namespace std;

struct PERSON{
char NAME[20];
float Balance;
  
string sName;
sName = str1 + " " + str2;
strcpy(name, sName.cstr())
}

int main() {
fstream myFile;
string fileIN = "data.txt";
myFile.open(fileIN.c_str());

cout << fileIN << endl;


return 0;
}

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

1>) we need to calculate number of records in the file i.e N.

C++ Program which counts the number of lines in a file can be used here. The program creates an input file stream, reads a line on every iteration of a while loop, increments the count variable and the count variable is the required N.

Here is source code of the C++ program which counts the number of lines in a file.

#include<iostream>
#include<fstream>
using namespace std;

//defining the struct PERSON

struct PERSON {

char name[20];

float balance ;

}

int main()

{
    int count = 0;
    string line;
 
    /* Creating input filestream */ 
    ifstream infile("data.txt");
    while (getline(infile, line))
        count++;
// storing the value of count i.e total records in N
    int N=count;

//creating an array of type PERSON and length N

PERSON P[N];

// my attempt at printing out the contents of the file


  while (!EOF)
 {
    for (int x = 0; x < N; x++)
    { 
       
          string temp;
         infile >> P[x].name; 
         infile >> temp;
         P[x].name + " ";
         P[x].name + temp;// to store both first name and last name 
       inFile >> P[x].balance;
      

      
    }
  }
  //calling the display function to print the array
display(P,N);
  return 0;
}

void display(PERSON *P , int N) {

for(i = 0; i < N; i++ )

    { printf("Name+" "+"Balance);

        printf("%s\t%f\n",

P[i].name, P[i].balance);

    }

};

Please leave feedback , if found usefull, your feedback is valuable.THANKYOU !

Add a comment
Know the answer?
Add Answer to:
Hi I was hoping to get some help with my code for this problem. I don't...
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
  • Description: A program is needed for a third-party payroll clearinghouse that supports the following: Calculate employee...

    Description: A program is needed for a third-party payroll clearinghouse that supports the following: Calculate employee pay feature Company total pay feature The employee and company totals are stored in a text file for each individual company. 1. Create the following “input.txt” file. Each record consists of a first name, last name, employee ID, company name, hours worked, and pay rate per hour. Maria    Brown 10 Intel   42.75 39.0 Jeffrey Jackson 20 Boeing   38.0 32.5 Bernard Smith 30 Douglas   25.0...

  • URGENT. Need help editing some C code. I have done most of the code it just...

    URGENT. Need help editing some C code. I have done most of the code it just needs the following added: takes an input file name from the command line; opens that file if possible; declares a C struct with three variables; these will have data types that correspond to the data types read from the file (i.e. string, int, float); declares an array of C structs (i.e. the same struct type declared in point c); reads a record from the...

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

  • the source code I have is what i'm supposed to fix for the assignment at the...

    the source code I have is what i'm supposed to fix for the assignment at the bottom. cars.cpp source code: /* Program Cars.cpp reads records from file and writes them back to another file with the price member increased by 10%. */ #include <fstream> #include <iostream> #include <string> #include "car.h" // car.h header file is included here using namespace std; /* Reads data from a file and using it to populate a Car struct and then return that Car struct...

  • The code will not run and I get the following errors in Visual Studio. Please fix the errors. err...

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

  • hello there. can you please help me to complete this code. thank you. Lab #3 -...

    hello there. can you please help me to complete this code. thank you. Lab #3 - Classes/Constructors Part I - Fill in the missing parts of this code #include<iostream> #include<string> #include<fstream> using namespace std; class classGrades      {      public:            void printlist() const;            void inputGrades(ifstream &);            double returnAvg() const;            void setName(string);            void setNumStudents(int);            classGrades();            classGrades(int);      private:            int gradeList[30];            int numStudents;            string name;      }; int main() {          ...

  • I need to get this two last parts of my project done by tonight. If you...

    I need to get this two last parts of my project done by tonight. If you see something wrong with the current code feel free to fix it. Thank you! Project 3 Description In this project, use project 1 and 2 as a starting point and complete the following tasks. Create a C++ project for a daycare. In this project, create a class called child which is defined as follows: private members: string First name string Last name integer Child...

  • Hi there, I am working on a binary search tree code in c++. The program must...

    Hi there, I am working on a binary search tree code in c++. The program must store and update students' academic records, each node includes the student name, credits attempted, credits earned and GPA. I have made some progress with the code and written most of the functions in the .cpp file (already did the .h file) but i am struggling with what to put in the main and how to put an update part in the insert function. I...

  • I want to change this code and need help. I want the code to not use...

    I want to change this code and need help. I want the code to not use parallel arrays, but instead use one array of struct containing the data elements, String for first name, String for last name,Array of integers for five (5) test scores, Character for grade. If you have any idea help would be great. #include #include #include #include using namespace std; const int NUMBER_OF_ROWS = 10; //number of students const int NUMBER_OF_COLUMNS = 5; //number of scores void...

  • Hello, I need to implement these small things in my C++ code. Thanks. 1- 10 characters...

    Hello, I need to implement these small things in my C++ code. Thanks. 1- 10 characters student first name, 10 characters middle name, 20 characters last name, 9 characters student ID, 3 characters age, in years (3 digits) 2- Open the input file. Check for successful open. If the open failed, display an error message and return with value 1. 3- Use a pointer array to manage all the created student variables.Assume that there will not be more than 99...

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
ADVERTISEMENT