[help] problem with fstream-stuffs

This is the program I'm currently working on for my homework. Basically, it changes the format of a name.

If input = Mayura Chen, output = Chen, Mayura
If input = Mayura Holy Chen, output = Chen, Mayura H.
(those are some random names ~_~) and this is the program I've done

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
#include <iostream>
#include <string>
#include <cstring>
#include <fstream>
#include <cstdlib>

using namespace std;

int main()
{
    int const MAX_CHAR = 20;
    string first_name, middle_name, last_name, check_char;
    int i = 0;
    char name;

    ifstream input;
    ofstream output;

    output.open("name.txt");

    while (name != '\n')
    {
        cin.get(name);
        output << name;
    }

    output.close();

    input.open("name.txt");

    while (input.get(name))
    {
        if (name == ' ')
        {
            i++;
        }
    }

    while (input >> check_char)
    {
        if ((int)check_char.length() > MAX_CHAR)
        {
            cout << "Error. Maximum character for each name is" << MAX_CHAR << ".";
        }
    }


    if (i == 1)
    {
        input >> first_name >> last_name;
        cout << last_name << ", " << first_name;
    }
    else if (i == 2)
    {
        input >> first_name >> middle_name >> last_name;
        cout << last_name << ", " << first_name << " " << middle_name[0] << ".";
    }

    input.close();


    return 0;
}


I'm sure the problem is within line 50-51 and 55-56 but I don't know how to solve it

And also, in line 39-45, if i give more than 20 characters, it won't give the desired cout :S
Last edited on
much easier to combine into one read in
just a quick pseudo code
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
fstream fin;
ostream fout;
fout.(outfile);
fin.open(file);
int count = 0;
while (getline(temp,fin) // this is set up to read until endline
{    
      // count spaces in string
      for (loop through string)
         if (temp[i] is " ")
             count++;

      // break the string up into 2 or three based on space count
          for (int i = 0; i < temp.getlength(); i++)
               until first " "
                   add to first
               until second " "
                   add to second
               if count == 2
                    until end 
                       add third;     
           first = first_name;
           if count is 1
           {
               second = last_name
           }
           if count is 2 
          {
                second = middle_name
                third = last_name
           }             

      // display
      fout last_name << "," << first_name;
      if (count == 2)
          fout << " " << middle_name[0] << ".";
     fout << endl;
     // erase for next time
      first.erase();
      second.erase();
      third.erase();
      count = 0;
}
fout.close();
fin.close();
Last edited on
Topic archived. No new replies allowed.