Code won't edit/overwrite the text file, even though everything appears to be working correctly.

edit: ignore this initial post. Check my latest comment - the issue below is resolved.

Hello all. The file is opening and is outputting numbers, but it isn't outputting strings. It worked on a Windows but now it's not compatible with my Mac and I have to fix a lot of stuff before I get to work on it. I can't get on a Windows between now and the time it's due.

I wrote this code on a Windows at school in Codeblocks, eventually copied and pasted it on my Mac and I finally got the file opening part done and it builds and runs. But now it's not wanting to read the strings in the file. (There's another function to this code but that's not relevant to my current question, so there's a lot of stuff backslashed out right now)

line 55 is where it's going wrong. I checked the text file after running this and the lines are still there.

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
//Collect Pokemon info about levels and names and types.
//
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

//delcaring
void displayPokemon();
bool deletePokemon();

int main()
{
    char viewPokemonChoice = ' ';
    char removePokemonChoice = ' ';
    //char cont = ' ';
    cout << "This program collects the Pokemon you catch and their stats. \nWould you like to view them? (Y/N)" << endl;
    cin >> viewPokemonChoice;
    cin.ignore();
    if (toupper(viewPokemonChoice) == 'Y')
    {
        cout << "Here are your Pokemon:" << endl;
        displayPokemon();
    }
    cout << "Would you like to delete a Pokemon?" << endl;
    cin >> removePokemonChoice;
    if (toupper(removePokemonChoice) == 'Y')
    {
        deletePokemon();
        //cout << "Would you like to view your new list?" << endl;
      //  cin >> cont;
      //  if (toupper(cont) == 'Y')
       //     displayPokemon();
    }

    return 0;
}
void displayPokemon()
{
    string name;
    string type;
    int level;
    ifstream fin;
    fin.open("pokemon.txt");
    if (fin.is_open())
    {
        while (isalnum(fin.peek()))
        {
            getline(fin, name);
            getline(fin, type);
            fin >> level;
            fin.ignore(5, '\n');
            fin.clear();
            cout << name << " is a " << type << " type and is at level " << level << ". \n"; //it outputs " is a  type and is at a level 42", it just jumps over the strings.
        }
        fin.close();
    }
}


I'm not sure how to post text files, so here's just a brief list of what I have:

1
2
3
4
5
6
7
8
9
Fennekin
fire
42
Fletchling
normal/flying
32
Caterpie
bug
15
Last edited on
Help me soon!!

This is a problem specifically just on my computer. I tried to do the same thing (reading a text file) and it's giving silly results - even though it worked on a Windows. I will try to get my hands on a windows soon and try to code this on there.
Last edited on
closed account (SECMoG1T)
try this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

 ifstream fin("pokemon.txt");
    if (fin)
    {
        while (fin)
        {
            getline(fin, name);
            getline(fin, type);
            fin >> level;
            fin.ignore(5, '\n');
            fin.clear();
            cout << name << " is a " << type << " type and is at level " << level << ". \n";
        }
    }

 else
  cout<<"pokemon.txt not found";
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void displayPokemon()
{
    std::ifstream fin( "pokemon.txt" );

    std::string name;
    std::string type;
    int level;

    while( std::getline( fin, name ) &&
           std::getline( fin, type ) &&
           fin >> level )
    {
        std::cout << name << " is a " << type << " type and is at level " << level << ".\n";
        fin.ignore( 1000, '\n' );
    }
}
The problem fixed itself when I switched operating systems. weird! but thank you for the generous help!

My current problem now is the deletePokemon function. It will skip over the "Which Pokemon would you like to delete?" and ends the program with an incorrect return statement.

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
//Collect Pokemon info about levels and names and types.
//
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

//delcaring
void displayPokemon();
bool deletePokemon();

int main()
{
    char viewPokemonChoice = ' ';
    char removePokemonChoice = ' ';
    //char cont = ' ';
    cout << "This program collects the Pokemon you catch and their stats. \nWould you like to view them? (Y/N)" << endl;
    cin >> viewPokemonChoice;
    cin.ignore();
    if (toupper(viewPokemonChoice) == 'Y')
    {
        cout << "Here are your Pokemon:" << endl;
        displayPokemon();
    }
    cout << "Would you like to delete a Pokemon? Y/N" << endl;
    cin >> removePokemonChoice;
    if (toupper(removePokemonChoice) == 'Y')
    {
        deletePokemon();
        //cout << "Would you like to view your new list?" << endl;
      //  cin >> cont;
      //  if (toupper(cont) == 'Y')
       //     displayPokemon();
    }

    return 0;
}
void displayPokemon()
{
    string name;
    string type;
    int level;
    ifstream fin;
    fin.open("pokemon.txt");
    if (fin.is_open())
    {
        while (isalnum(fin.peek()))
        {
            getline(fin, name);
            getline(fin, type);
            fin >> level;
            fin.ignore(5, '\n');
            fin.clear();
            cout << name << " is a " << type << " type and is at level " << level << ". \n";
        }
        fin.close();
    }
}

//bool addPokemon()
//{
   // bool found = false;
   // return found;
//}
bool deletePokemon()
{
    string name[100];
    string type[100];
    int level[100];
    int pokemonCount = 0;
    string deletedPokemon = "";
    bool found = false;

    ifstream fin;

    cout << "Which Pokemon would you like to delete?" << endl;
    getline(cin, deletedPokemon); //skipping this line

    fin.open("pokemon.txt");
    if (fin.is_open())
    {
        while (isalnum(fin.peek()))
        {
            getline(fin, name[pokemonCount]);
            getline(fin, type[pokemonCount]);
            fin >> level[pokemonCount];
            fin.clear();
            fin.ignore(100, '\n');
            if (deletedPokemon == name[pokemonCount])
            {
                pokemonCount--;
                found = true;
            }
            pokemonCount++;
        }
        fin.close();

    }

    ofstream fout;
    fout.open("pokemon.txt");
    if (fout.is_open())
    {
        for (int i = 0; i < pokemonCount; i++)
        {
            fout << name[i] << "\n";
            fout << type[i] << "\n";
            fout << level[i] << endl;
        }
        fout.close();
        cout << "pokemon removed\n";
    }
    else
       cout << "removal failure";
    return found;
}


This program collects the Pokemon you catch and their stats.
Would you like to view them? (Y/N)
y
Here are your Pokemon:
Fennekin is a fire type and is at level 42
(etc)
Would you like to delete a Pokemon? Y/N
y
Which Pokemon would you like to delete?
pokemon removed

Process returned 0 (0x0)
Last edited on
Using cin>> to read in a character on line 27 leaves a newline (enter) in the buffer; when you go to read a string on line 78, you read that enter press as the string and you end up with nothing/the empty string.

You can use cin.ignore() after the cin>> read to discard the newline manually, or you could just use getline() everywhere, as that will suffice for reading single characters just as well as cin>>
Thank you! That helped. Now it will read the line but it won't really delete the Pokemon now. Any suggestions?

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
//Collect Pokemon info about levels and names and types.
//
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

//delcaring
void displayPokemon();
bool deletePokemon();

int main()
{
    char viewPokemonChoice = ' ';
    char removePokemonChoice = ' ';
    //char cont = ' ';
    cout << "This program collects the Pokemon you catch and their stats. \nWould you like to view them? (Y/N)" << endl;
    cin >> viewPokemonChoice;
    cin.ignore();
    if (toupper(viewPokemonChoice) == 'Y')
    {
        cout << "Here are your Pokemon:" << endl;
        displayPokemon();
    }
    cout << "Would you like to delete a Pokemon? Y/N" << endl;
    cin >> removePokemonChoice;
    if (toupper(removePokemonChoice) == 'Y')
    {
        deletePokemon();
        cout << "New list:" << endl;
        displayPokemon();
    }

    return 0;
}
void displayPokemon()
{
    string name;
    string type;
    int level;
    ifstream fin;
    fin.open("pokemon.txt");
    if (fin.is_open())
    {
        while (isalnum(fin.peek()))
        {
            getline(fin, name);
            getline(fin, type);
            fin >> level;
            fin.ignore(5, '\n');
            fin.clear();
            cout << name << " is a " << type << " type and is at level " << level << ". \n";
        }
        fin.close();
    }
}

//bool addPokemon()
//{
   // bool found = false;
   // return found;
//}
bool deletePokemon()
{
    string name[100];
    string type[100];
    int level[100];
    int pokemonCount = 0;
    string deletedPokemon = "";
    bool found = false;

    ifstream fin;

    cout << "Which Pokemon would you like to delete?" << endl;
    getline(cin, deletedPokemon);
    cin.ignore(5, '\n');
    cin.clear();

    fin.open("pokemon.txt");
    if (fin.is_open())
    {
        while (isalnum(fin.peek()))
        {
            getline(fin, name[pokemonCount]);
            getline(fin, type[pokemonCount]);
            fin >> level[pokemonCount];
            fin.clear();
            fin.ignore(100, '\n');
            if (deletedPokemon == name[pokemonCount])
            {
                pokemonCount--;
                found = true;
            }
            pokemonCount++;
        }
        fin.close();

    }

    ofstream fout;
    fout.open("pokemon.txt");
    if (fout.is_open())
    {
        for (int i = 0; i < pokemonCount; i++)
        {
            fout << name[i] << "\n";
            fout << type[i] << "\n";
            fout << level[i] << endl;
        }
        fout.close();
        cout << "pokemon removed\n";
    }
    else
       cout << "removal failure";
    return found;
}
I hate to bump, but I'm going to bump. This is due tonight and my teacher disabled commenting on the help forum.

Trying to add an addPokemon function and it isn't working either. There must be a common problem.

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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
//Collect Pokemon info about levels and names and types.
//
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

//delcaring
void displayPokemon();
bool deletePokemon();
void addPokemon();

int main()
{
    char viewPokemonChoice = ' ';
    char removePokemonChoice = ' ';
    char addPokemonChoice = ' ';
    cout << "This program collects the Pokemon you catch and their stats. \nWould you like to view them? (Y/N)" << endl;
    cin >> viewPokemonChoice;
    cin.ignore();
    if (toupper(viewPokemonChoice) == 'Y')
    {
        cout << "Here are your Pokemon:" << endl;
        displayPokemon();
    }
    cout << "Would you like to delete a Pokemon? Y/N" << endl; //if I select N on this option, it won't let me answer the next question.
    cin >> removePokemonChoice;
    if (toupper(removePokemonChoice) == 'Y')
    {
        deletePokemon();
        cout << "New list:" << endl;
        displayPokemon();
    }
    cout << "Would you like to add a Pokemon? (Y/N)" << endl;
    cin >> addPokemonChoice;
    if (toupper(addPokemonChoice) == 'Y')
    {
        addPokemon();
        cout << "New list:" << endl;
        displayPokemon();
    }

    return 0;
}
void displayPokemon() //this function seems to work fine.
{
    string name;
    string type;
    int level;
    ifstream fin;
    fin.open("pokemon.txt");
    if (fin.is_open())
    {
        while (isalnum(fin.peek()))
        {
            getline(fin, name);
            getline(fin, type);
            fin >> level;
            fin.ignore(5, '\n');
            fin.clear();
            cout << name << " is a " << type << " type and is at level " << level << ". \n";
        }
        fin.close();
    }
}

void addPokemon() //this function doesn't add to the list but it keeps saying it was successful.

{
    string name[100];
    string type[100];
    int level[100];
    string newPokemon;
    string newType;
    int newLevel;
    ifstream fin;
    ofstream fout;
    int numberOfPokemon = 0;
    //Input Pokemon Info
    cout << "Name of Pokemon: ";
    getline(cin, newPokemon);
    cin.ignore(100, '\n');
    cout << "Pokemon type: ";
    getline(cin, newType);
    cin.ignore(100, '\n');
    cout << "Pokemon level: "; //weird gap between line 85 and like 87 when I run this. I have to press enter on line 84 to get the next question.
    cin >> newLevel;
    cin.ignore(5, '\n');
    fin.open("pokemon.txt");
    //Put file in array
    if (fin.is_open())
    {
        while (isalnum(fin.peek()) && numberOfPokemon < 100)
        {
            getline(fin, name[numberOfPokemon]);
            getline(fin, type[numberOfPokemon]);
            fin >> level[numberOfPokemon];
            fin.ignore(100, '\n');
            if (name[numberOfPokemon] != newPokemon)
                numberOfPokemon++;
        }
        fin.close();
    }
    //Output file
    fout.open("pokemon.txt");
    if (fout.is_open())
    {
        for (int i = 0; i < numberOfPokemon; i++)
        {
            fout << name[i] << "\n";
            fout << type[i] << "\n";
            fout << level[i] << "\n";

        }
        //Tack on new piece
        fout << newPokemon << "\n";
        fout << newType << "\n";
        fout << newLevel << "\n";
        fout.close();
        cout << "Add Successful\n";
    }
    else
    {
        cout << "Add Failure\n";
    }
}

bool deletePokemon()

{
    string name[100];
    string type[100];
    int level[100];
    int pokemonCount = 0;
    string deletedPokemon = "";
    bool found = false;

    ifstream fin;

    cout << "Which Pokemon would you like to delete?" << endl;
    getline(cin, deletedPokemon);
    cin.ignore(5, '\n');

    fin.open("pokemon.txt");
    if (fin.is_open())
    {
        while (isalnum(fin.peek()))
        {
            getline(fin, name[pokemonCount]);
            getline(fin, type[pokemonCount]);
            fin >> level[pokemonCount];
            fin.clear();
            fin.ignore(100, '\n');
            if (deletedPokemon == name[pokemonCount])
            {
                pokemonCount--;
                found = true;
            }
            pokemonCount++;
        }
        fin.close();
        cout << "ya the file opened" << endl;
    }

    ofstream fout;
    fout.open("pokemon.txt");
    if (fout.is_open())
    {
        for (int i = 0; i < pokemonCount; i++)
        {
            fout << name[i] << "\n";
            fout << type[i] << "\n";
            fout << level[i] << endl;
        }
        fout.close();
        cout << "pokemon removed\n";
        cout << "the file opened."; //it is returning that the file opened in both occasions in this function but nothing is happening!
    }
    else
    {
       cout << "removal failure";
       cout << "The file didn't open";
    }
    return found;
}
Last edited on
Topic archived. No new replies allowed.