Specific number of records from a file. due at midnight please help...

Hello, I have a hw assignment due at midnight. I need help I have two problems and I am stuck on the first one. this is the first problem.

. Write a main method that prompts the user for the name of an input file. The input file will include a list of numbers, each on a separate line. The first number in the file will contain the number of integers in the file to add. The program will add up all the numbers in the file and cout to the user the total. E.g. a file containing "3 10 20 30" will result in the program printing that the sum is 60. In this program you must create the ifstream object with the file name. Your program must verify that the file exists.

This is what I have so far. I am having trouble getting it to do the math once it reads the number 3 in the file.

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

int main()
{
int num_data_pts, x;
double sum, n;
string filename;
ifstream datain2;

cout << "Please enter the name of the data file to be analyzed" << endl;
cin >> filename;

if (datain2.fail())
{
cerr << "The file was not opened correctly" << endl;
exit(1);
}
datain2.open(filename);
datain2 >> x;
datain2 >> num_data_pts;
for (x; x < num_data_pts; x++)
{
sum = x + x;
cout << "Sum=" << sum << endl;
}
datain2.close();
return 0;
}



I got the first one. It seems to work. Now have 40 mins. to grab that second lol.

before you run this you must create a text file containing 4 numbers like this:

3
10
20
30

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

int main()
{
int num_data_pts, x;
double sum = 0;
string filename;
ifstream datain2;

cout << "Please enter the name of the data file to be analyzed" << endl;
cin >> filename;

datain2.open(filename);
if (datain2.fail())
{
cerr << "The file was not opened correctly" << endl;
exit(1);
}

datain2 >> num_data_pts;
datain2 >> x;
for (x; num_data_pts > 0; num_data_pts--)
{
sum = x + sum;
datain2 >> x;
}
cout << "Sum=" << sum << endl;
datain2.close();
return 0;
}


Then this is problem 2 and I don't even know where to start. we haven't really gone over this is class, I have been working on this for days, I should have signed up for this forum sooner.... Any help would be great!

Write a main method that prompts the user for the name of two files, an input file and an output file. The program will read in the input file and remove all empty spaces, tabs, new lines, commas, periods, excalamation marks and question marks. It will write the new output to the output file. E.g. if the input file contained "Hi, my name is Ibtsam! What is your name?", then the output file will contain "HimynameisIbtsamWhatisyourname". In this program, you must use open() to open the input file. Your program must verify that the file
exists.
Last edited on
/*
*file_name: main.cpp
*/

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

int line_size = 0; // variable for holding the first number on a line
double sum = 0.0; // for storing the result
char file_name[50]; // name of the file to be processed
void print_content(ifstream&); // a function for printing the content of the file.
void eval_file(ifstream&); // a function for summing each set of digits per line.


int main(int argc, char** argv) {
cout << "Enter the name of the file to be process: ";
cin >> file_name;
cout << endl;

ifstream in(file_name); //input stream
if(!file_name){ // checking validity of file
cout << "Unable to open the file '" << file_name << "'" << endl;
return 1;
}

print_content(in);
in.close();
in.open(file_name);
eval_file(in);
in.close();

int delay;
cout << "Enter any number to exit...." << endl;
cin >> delay;
return 0;
}

void print_content(ifstream& in){
cout << "-------------------------------" << endl;
cout << "-------file content -----------" << endl;
cout << "-------------------------------" << endl;
char ch;
while(in.get(ch)){
cout << ch;
}

cout << endl;
}

void eval_file(ifstream& in){
char ch; // variable for holding characters
string digits_str = ""; // for holding each set of digits at a time
double digits_num; // for holding digits_str after it has been converted to double
int line_num = 1; // Line number of the current line of digits

while(in.get(ch)){

/************************************************
* the ternary conditional statement ensures *
* that no white space character is concatenated*
* with the string digits_str. *
* **********************************************
*/
digits_str = !(ch == ' ' || ch == '\t' || ch == '\n')? digits_str+ch : digits_str;

/**************************************************
* since the first digits character is the size *
* of total number of digits on a line, the if *
* block check this. Once line size == 0 and *
* digits_str != \0, then the atol function from *
* cstdlib is used to convert the string *
* digit_str to number and assign it to line size.*
* Digits_str is initialize to "" since line_size *
* is not needed in the calculation. *
* ************************************************
*/
if(line_size == 0 && !(digits_str == "")){
line_size = atol(digits_str.c_str());
digits_str = "";
}

/********************************************************
* peek() fucntion return a reference to next character *
* in a file. In the if block there several conditional *
* test. This give flexibility to the user. That is he *
* or she can type the document any how provided there *
* are only numeric and white space characters on a line.
*NOTE: if these conditions are met it means that we have
* gotten a full digits on a line that we can use for *
* the addition. *
* *******************************************************
*/
if((in.peek() == ' ' || in.peek() == '\n' || in.eof() || in.peek() == '\t') && !(ch == ' ' || ch == '\t' || ch == '\n')){
digits_num = atol(digits_str.c_str()); // convert digits_str to double
sum += digits_num; // add digits_num to sum.
digits_str = ""; // initialize digits_str to "" for the next set of digits
}

/******************************************************
* if a new line or end of file is encounter, write *
* the sum of all the digits on that line to the *
* console. *
* Initialize all the values to their default and start*
* processing a new line if any. *
* ****************************************************
*/
if((in.peek() == '\n' && !(ch == '\n')) || in.eof()){
cout << line_num << ": sum-> " << sum << endl;
digits_str = "";
digits_num = 0;
line_size = 0;
sum = 0;
line_num++;
}
}
}


Sample run:
/*********************************************************************
*Enter the name of the file to be process: data.txt
-------------------------------
------file content -----------
-------------------------------
1 1
2 1 2
3 1 2 3
4 1 2 3 4
5 1 2 3 4 5
6 1 2 3 4 5 6
7 1 2 3 4 5 6 7
8 1 2 3 4 5 6 7 8

9 1 2 3 4 5 6 7 8 9

10 10 20 30 40 50 60 80
1: sum-> 1
2: sum-> 3
3: sum-> 6
4: sum-> 10
5: sum-> 15
6: sum-> 21
7: sum-> 28
8: sum-> 36
9: sum-> 45
10: sum-> 290
11: sum-> 0
**********************************************************************
The above code will do just what you want. But I am sorry I didn't see the last line of your instruction that the file should be of this format. I got through writing the code before i saw it.
3
10
20
30
But a slight modification to the program should do just what you want. Just change the conditions in the while loop in the function eval_file().

For the second assignment, the below code will do just what you want.

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

int main(){
char file_in[50];
char file_out[50];

cout << "Enter the name of the input and output file: ";
cin >> file_in >> file_out;
cout << endl;

ifstream input(file_in);
ofstream output(file_out);

if(!(input && output)){
cout << "unable to open the files " << file_in << " and " << file_out << endl;
return 1;
}

char ch;
while(input.get(ch)){
if(!(ch == '\n' || ch == '\t' || ch == ' ' || ch == '?' || ch == '!' || ch == ';' || ch == ',' || ch == '.')){
output << ch ;
}
}
cout << endl;
cout << "done processing " << endl;
input.close();
output.close();

cout << "Enter any number to exit";
int delay;
cin >> delay;

return 0;
}
Last edited on
Thank you very much that was extremely helpful :)
Topic archived. No new replies allowed.