Get/Put Functions

Im having trouble getting the program to execute. Basically the program is meant to read from an input file then create an output file with the users information. The input file is called UserInfo.txt with the following text.

"User Personal Information:
Name: #N#
Phone: $$$
This is the end of the information."

I'm not sure whether I simply have misplaced brackets or whether I have other logic errors. But Im only able to enter the file name, then there are no other prompts given, or changes made to the output file.

If anyone could please help, I would greatly appreciate it.

This is what I have so far, along with the guides given.
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
  //Include all libraries you will need
#include <fstream>
#include <cstdlib>
#include <cctype>
#include <iostream>

using namespace std;

void update(ifstream& in_file, ofstream& out_file);

int main()
{

  char filename[16];  //This is the file name that the user will input
  //Prompt the user to enter the file name
        cout << "Enter the file name \n";
        cin >> filename;
  //Declare input and output stream objects
        ifstream in_file;
        ofstream out_file;
  // Connect your input file to file name
        in_file.open (filename);
  // Connect your output file to UserInfoUpdated.txt
        out_file.open ("UserInfoUpdated.txt");
  // Check if input file fails to open
    if (in_file.fail())
    {
        cout << "Input file has failed to open!" << endl;
        exit(1);
    }
  // Check if output file fails to open
    if (out_file.fail())
    {
        cout << "Output file has failed to open!" << endl;
        exit(1);
    }
  // Call the function update
        update(in_file,out_file);
  // Close input file
        in_file.close();
  // Close output file
        out_file.close();
    return 0;
}  // end of main

void update(ifstream& in_file, ofstream& out_file)
{
    char name;
    char next;
    char symbol;
    char phonenumber;

  // Get the first character from the input file

    in_file.get (next);
  // while(you have not reached the end of the file)
    while (!in_file.eof())
    {
  // Check if the character is #
        if (next=='#')
            {
  // If the next character is #, get the second character
            in_file.get(next);
  // Check if second character is N
                if (next=='N')
                    {
  // If second character is N, get the third character
                    in_file.get(next);
  // Check if third character is #
                        if (next='#')
                            {
                                cout<< "Enter your name followed by any digit from 1-9"<< endl;
                                cin.get (name);
                                cin.get (name);
                                while (!isdigit(next));

                            }
                    }
            }
  // If the third character is #, prompt the user to enter his name followed by digit number

  // Get two characters to skip the first new line character

  // while(the next character is not digit)

  // outfile.put(character) and get the next character; the loop goes on until it reads a digit number

  //Else if the character is not #, check if it is $, and if yes, get the second character
    else if (next==!'#' && next=='$')
            {
            in_file.get(next);
  // Check if second character is $, if yes, get the third character
                if (next =='$')
                    {
                    in_file.get(next);
  // Check if third character is $, if yes, prompt user to enter his phone number
                        if (next =='$')
                            {
                                cout<< " Please enter your phone number without spaces"<< endl;
                                cin.get (phonenumber);
                                cin.get (phonenumber);



  // outfile.put('+');
            out_file.put ('+');
   //outfile.put('1');
            out_file.put ('1');
   // outfile.put('(');
            out_file.put ('(');
   // Get two characters to skip the first new line character
                            }
                    }
    // int i=0;

    // while(the next character is not a space)
        while (!isspace(next));
            {
        int i=0;
     // if(i==3), then outfile.put(')'); outfile.put(symbol); i=100;
        if (i==3)
            {out_file.put(')');
             out_file.put (symbol);
             i=100;
            }
    // else outfile.put(symbol); i++;
        else
                {out_file.put(symbol);
                i++;
    // get the next character
                in_file.get (next);
                }
            }
            }
    //   else if the character is not $ nor #, then simply write character read to output file
        else if ( next ==! '$' || next ==! '#')
        {
            out_file.put (next);

   // Get the next character
            in_file.get (next);
        }
            

}
}
i would like to do this way:

content in inf.txt:
User Personal Information:
Name: aa xyz
Phone: 02-246197
This is the end of the information.

program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <fstream>
using namespace std;
int main ()
{ 	string name, phone;
	char c = ' ';
	ifstream in("inf.txt");
	
	if(in.is_open())
		{
			in.seekg(34); // name position in the file
			getline(in,name); //in >> name; --> first name
			
			in.seekg(48); // phone number position in file
			in >> phone;			
		}	
		
	cout << "name : "<< name << endl;
	cout << "phone : "<< phone << endl;	
	
	in.close();	
	
	return 0;	
}
Last edited on
Topic archived. No new replies allowed.