I'm in a beginner c++ class and I need help with this program cause I don't know how to fix the errors, please help!

Apr 19, 2016 at 4:14am
This is my objective for the assignment :
Read an 8 digit number from the input file. Separate it into 8 single
digit numbers. Store those 8 single digit numbers into the output file.
Step 1.
Open an input and an output file.
Step 2.
Read contents (your student ID without L) from the input file.
Step 3.
Split the student ID (8 digits number) into 8 single digits numbers.
(See the output example below.)
Step 4.
Store the number read from the input file as well as the splitted 8 single
digit numbers to the output file.
Step 5.
Close both input and output files.

I had to get some help for this assignment
and this is the program I have so far:

#include<iostream.h>
#include<fstream.h>

int main()
{
char file_name[50];
char quit = 'n';
int count = 0;
while (quit == 'n')
{
cout << "Enter the file name:\n";
cin >> file_name;
//opening text file
ifstream fin(file_name);
if (fin)
cout << "File successfully opened\n";
else
cout << "File didn't open\n";

char ch;


fin.seekg(0, ios::end); //bring file pointer position to end of file

fin.seekg(0, ios::beg); //bring position of file pointer to begining of file
cout << "The input number :";
while (fin)
{
fin.get(ch);
cout << ch;
}
cout << "\n";
fin.close();
ifstream fin2(file_name);
fin2.seekg(0, ios::end);
fin2.seekg(0, ios::beg);
while (fin2)
{
count = count + 1;
fin2.get(ch);
if (ch == '\0')
break;
cout << "The number " << count << " is " << ch << "\n";
}
fin2.close();
cout << "Do you want to quit...Enter y for yes n for no::";
cin >> quit;
}

return 0;

}
Apr 19, 2016 at 4:38am
Did you say C++?

Something like this, perhaps (you may need to add error-handling):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <string>
#include <fstream>

int main()
{
    // Step 1. Open the input file.
    std::ifstream input_file( "input.txt" ) ;

    // Step 2. Read contents (your student ID without L) from the input file.
    unsigned int student_id ;
    input_file >> student_id ;

    // Step 3.Split the student ID (8 digits number) into 8 single digits numbers.
    // http://en.cppreference.com/w/cpp/string/basic_string/to_string
    std::string num_string = std::to_string(student_id) ; // get it into a string, each character is one digit

    // Step 4. Store the number read from the input file as well as the splitted 8 single digits
    std::ofstream output_file( "output.txt" ) ; // Step 4a: open the output file

    output_file << student_id << '\n' ; // Step 4b: write the number to the output file

    // http://www.stroustrup.com/C++11FAQ.html#for
    for( char digit : num_string ) output_file << digit << ' ' ; // Step 4c: write out the individual digits
    output_file << '\n' ; // // Step 4d: add a new line at the end

    // Step 5. Close both input and output files.
    // nothing to be done, the files will be closed automagically (destructors)
}
Topic archived. No new replies allowed.