Text Manipulation

Hi, and thanks for any help you can offer in advance! I'm very new to C++ and coding in general, and I'm having trouble figuring out what is wrong with this program. I'm trying to replace all occurrences of the string #N# in an input file with the first and last name entered by the user. I'm terrible with loops, so that's probably the issue I just can't tell what. The output just gives me the first and last name over and over, it never writes the other characters to the output:

**EDIT: I realized that I should have used == instead of = in the if statements, and that fixed it. But I'm wondering how I can check to see if its the first occurrence of #N#, because I'd like to have only the first name for the following occurrences. **

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
  //$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
//
//  Name: Kenzie Thomas
//  Date: 3/12/14
//  Program: junkmail.cc
//
//  Description: This program generates personalized junk mail using input
//               from a file and the keyboard.
//
//$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
  
#include <iostream>    
#include <cstdlib>
#include <fstream>
#include <string>
using namespace std;
    
void junkmail (istream& in_file, ofstream& out_file);
//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
//*
//* Function: junkmail
//* Purpose: Read input file and replace #N# with input from user, creating
//*          new output
//* Paramters: fin and fout, the input file and output file streams
//* Calls: <fstream> <string> <cstdlib>
//*
//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  
int main()
{ 
  ifstream fin;
  ofstream fout;
  
  fin.open("junk.dat");
      
  if (fin.fail())
  {
    cout << " \n Input file failed to open. \n";
    exit (1);
  }
  
  fout.open("junk.out");


  if (fout.fail())    
  {          
    cout << " \n Output file failed to open. \n";
    exit (1);  
  }
  
  junkmail(fin, fout);
  
  fin.close();
  fout.close();
   
  return(0);
}    

void junkmail (istream& in_file, ofstream& out_file)
{         
  char current;         // character currently being read
  char one='#';
  char two='N';
  char three='#';
  string first;       // first name
  string last;        // last name
             
  cout << "\n\n Enter the first name: ";
  cin >> first;
  cout << "\n Enter the last name: ";
  cin >> last;
   
  in_file.get(current);
  
  while(!in_file.eof())
  {
    if(current=one)
    {
      in_file.get(current);
  
        if(current=two)
          in_file.get(current);
  
          if(current=three)
          {
            out_file << first;
            out_file << " ";
            out_file << last;     
          }  
     } 
     else
       out_file << current;
     in_file.get(current);
  }
} 

Last edited on
Topic archived. No new replies allowed.