cin not working

Hey guys. I've got this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 if (izbor==2)
	{
		fstream clientFile("clientFile.txt",ios::app);
		cout<<"Na koi klient jelaete da promenite adresa?";
		cin>>searchIme;
		cout<<"Nov adres: ";
		while (true)
		{
			clientFile>>client.name;
			clientFile>>client.lastname;
			clientFile>>client.address;
			clientFile>>client.gender;
			clientFile>>client.age;
			clientFile>>client.firstInt;
			clientFile>>client.secondInt;
			if (searchIme==client.name)
			{
				cin>>client.address;
				clientFile<<client.address;
			}
		}
	}

Its is supposed to look for a name in a file and then change the address of the person, but the cin for the new address isn't working. It's like I'm not pressing anything.
After I put the cin.clear and cin.ignore I can type but the program doesn't go on. It just lets me type and nothing else.

while(true) will have you looping forever, you could use break; to break out of that loop where needed, or change the while loop condition to something more suitable.
If you want to be able to change value in a file you will really need to use a binary format with fixed sized records for the data. But then things can get involved.

With a text file you would generally read it all into memory, change the data you need to update, and then write it back out. This would be the most straightfoward solution.

Anyway...

Are cin.clear and cin.ignore relevant here? It looks like your code should run without it (if given ok values -- clear/ignore would allow it to handle input errors better.

I found the code only performs a "replacement" if the file is opened without the ios::app (app = append to file.)

File Before (ages are in numbers of messages):

itsallpoison Nowhere    9
andywestken  London  3463
Softrix      Britain  781

Program Ouput:

For which client do you want to change the address?andywestken
Searching...
- itsallpoison
- andywestken
New address: Copenhagen
Made replacment

File After:

itsallpoison Nowhere    9
andywestken  London  3463
Copenhagen   Britain  781

You can see that rather than replace my address, it's replaced Softrix' name!! And if I had moved to Charlottenberg in Sweden it would have been even worse!

itsallpoison Nowhere    9
andywestken  London  3463
Charlottenbergritain  781

Here the final line is now badly formed, so the record is no longer readable.

And here's the code I used to test you loop. I did not repair the while loop as such, but did tweak the code so the loop is exited if a replacement is made or it's gone round too many times (10 is enough for my test case.) I have also added a bit of output so you can see what's going on.

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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

// simplified client info
struct Client {
    string name;
    string address;
    int    age;
};

void updateFile();

int main() {
    fstream clientFile("clientFile.txt"); // was ios::app
    if(!clientFile.is_open()) {
        cout << "Failed to open file!\n";
        return 0;
    }

    cout<<"For which client do you want to change the address?";
    string searchName;
    cin>>searchName;
    cin.ignore(1024, '\n');
    //cout<<"New address: "; moved
    cout<<"Searching...\n";
    Client client;
    bool madeReplacement = false;
    int maxNumToSearch = 10;
    while (true)
    {
        clientFile>>client.name;
        clientFile>>client.address; // you do know >> can't handle spaces in strings?
        clientFile>>client.age;
        cout << "- " << client.name << "\n";
        if (searchName==client.name)
        {
            cout<<"New address: "; // moved here
            cin>>client.address;
            clientFile<<client.address;
            madeReplacement = true;
            break;
        }
        // hack to prevent infinite loop
        --maxNumToSearch;
        if(0 == maxNumToSearch)
            break;
    }
    if(madeReplacement)
        cout << "Made replacment\n";
    else
        cout << "No replacment made\n";

    return 0;
}


If the file is opened using the ios::app flag the program output is:

For which client do you want to change the address?andywestken
Searching...
-
-
-
-
-
-
-
-
-
-
No replacement made

The name of the person in the record being checked should be displayed after the hyphen; so it appears all the operator>> calls are failing to retrieve a value.

Andy
Last edited on
Thank you. Really cleared up things for me.
Topic archived. No new replies allowed.