Help

Can someone tell me why after the line: cout<<"Enter title for search: "; the console is waiting to enter a symbol but I can't With or without cin.get() is the same.

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
  void update()
{
    fstream f;
    fstream t;
    int itemnumber,availability,choose;
    string author,title,searching;
    f.open("file.txt",ios::in);
    t.open("temp.txt",ios::out);
    cout<<"Enter title for search: ";
    cin.get();
    getline(cin,searching,',');
    while(!f.eof())
    {
        f>>itemnumber;
        f.get();
        getline(f,title,',');
        f.get();
        getline(f,author,',');
        f>>availability;
        f>>itemnumber;
        f.get();
        getline(f,title,',');
        f.get();
        getline(f,author,',');
        f>>availability;
        if(searching==title)
         {cout<<"Enter 0 for renting and 1 for returning ";
          cin>>choose;
          if(choose==0)
            availability --;
          if(choose==1)
            availability ++;
          else
            cout<<"Invalid choice "; break;
         }
         else
            cout<<"There isnt a book with this name "; break;
        t<<itemnumber<<" "<<title<<", "<<author<<", "<<availability<<endl;
    }
    t.close();
    f.close();
    t.open("temp.txt",ios::in);
    f.open("file.txt",ios::out);
    while(!t.eof());
    {
        t>>itemnumber;
        getline(t,title,',');
        getline(t,author,',');
        t>>availability;
        f<<itemnumber<<" "<<title<<", "<<author<<", "<<availability<<endl;
    }
    f.close();
    t.close();
}
Last edited on
Hello Delcho,

In line 11 remove the third parameter. The "getline" is waiting to find a "," in your input and there is none there.

In your while conditions do not use "eof" it does not work the way you are thinking. You reach "eof" inside the while loop and process the record before the while condition can determine that "eof" has been reached. This usually leads to outputting the last read twice and the question why.

What is most often done is:

1
2
3
4
5
6
7
8
	while (f >> itemnumber)
	{
		//f.get();  // <--- Not needed.
		std::getline(f, title, ',');
		//f.get();  // <--- Not needed.
		std::getline(f, author, ',');
		f >> availability;
                // Rest of code. 


This way when the stream fails the while loop fails.

Line 11 looks like you are inputting a title to search for, but you never use this variable for any kind of search.

For initial testing you should use two different output files until the program work the way you want. Then if you want to overwrite the input file or append to it fo that later.

A hint: a few blank lines would make the code easier to read instead of being bunched all together.

Hope that helps,

Andy
Thank you but basically, the problem is somewhere after the first IF after
cout<<"Enter 0 for renting and 1 for returning ";
cin>>choose;
I cant enter nothing in the console
Hello Delcho,

That is not the code I was first working on. Not a good idea to change the original post. It throughs people off when you change or delete the original post. Best to add new changes than to edit the original.

I will load it up and see what I get.

Andy

Yes Andy and I apologize for that. But l haven't noticed that these lines were not here
Hello Delcho,

You are using "file.txt" for your input file. I need to see what that file looks like. Post the whole file if it is short otherwise 5 lines should do.

I have manages to get the file to read, but I do not know if my guess is correct or not.

Refer to my first response regarding line 11. It is still a problem.

Andy
These are all lines in the file

5343 Harry Potter, Rowling, 56
5001 Hobbit, Tolkien, 9

the first number and last numbers are not special so you can input any number there
Last edited on
Hello Delcho,

Thank you for the info on the input file.

I hope it will be easier if to just show you what I have done and read the comments in the 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
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
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <chrono>
#include <thread>

void update()
{
	std::fstream f;  // <--- Better names for these variables would be helpful.
	std::fstream t;

	int itemnumber{}, availability{}, choose{};
	std::string author, title;
	// Switch comments for testing. Remove quoted text for run.
	std::string searching{ "Harry Potter" };
	//std::string searching{ "Hobbit" };

	f.open("file.txt", std::ios::in);

	if (!f.is_open())
	{
		std::cout << "\n File did not open.";
		std::this_thread::sleep_for(std::chrono::seconds(2));  // Requires header files "chrono" and "thread"
		exit(1);
	}

	t.open("temp.txt", std::ios::app);

	if (!t.is_open())
	{
		std::cout << "\n File did not open.";
		std::this_thread::sleep_for(std::chrono::seconds(2));  // Requires header files "chrono" and "thread"
		exit(1);
	}

	//  Uncomment next three lines for normal use.
	//std::cout << "Enter title for search: ";
	////std::cin.get();  // <--- Not needed here. Will cause someone to enter a title twice.
	//std::getline(std::cin, searching);

	while (f >> itemnumber)
	{
		f.get();  // <--- Used here to get the space after the number.
		std::getline(f, title, ',');
		f.get();  // <--- Used here to get the space after the title.
		std::getline(f, author, ',');
		f >> availability;

		//  Used for testing. Comment out when not needed.
		//std::cout << itemnumber << " " << title << ", " << author << ", " << availability << std::endl;

		// This section not needed.
		//f >> itemnumber;
		////f.get();
		//std::getline(f, title, ',');
		////f.get();
		//std::getline(f, author, ',');
		//f >> availability;

		if (searching == title)
		{
			std::cout << "Enter 0 for renting and 1 for returning ";
			std::cin >> choose;

			if (choose == 0)
				availability--;
			else if (choose == 1)
				availability++;
			else
			{
				std::cout << "Invalid choice ";
				//break;  // <-- Should not leave the while loop. Leaves the while loop to early. Skips writing to the file.
			}
		}
		else
		{
			std::cout << "There isnt a book with this name ";
			//break;  // <--- Leaves the while loop to early. Skips the next statement.
		}

		//  This line writes every input to the "temp.txt" . Is that what you want?
		t << itemnumber << " " << title << ", " << author << ", " << availability << std::endl;

		//  Used for testing. Comment out when not needed.
		std::cout << itemnumber << " " << title << ", " << author << ", " << availability << std::endl;
	}

	t.close();
	f.close();
	return;  // <--- Leaves the function while working on the first half.

	//  I find it hard to understand the purpose of this section.
	t.open("temp.txt", std::ios::in);
	f.open("file2.txt", std::ios::out || std::ios::app);

	while (!t.eof());  // <---Incorrect see above.
	{
		//  This should look more like what is at the beginning of the while loop.
		t >> itemnumber;
		getline(t, title, ',');
		getline(t, author, ',');
		t >> availability;
		f << itemnumber << " " << title << ", " << author << ", " << availability << std::endl;
	}

	f.close();
	t.close();
}

The "break" statements should not be where they are. They cause you to exit the while loop to early.

Both the else statement contain two statements, so they need to be inside a set of {} to work properly. Since you do not need the "break" statements the {} are not needed for just one statement. I added them before I realized the problem with the "break" statements.

I added the code to check if the files are open. Mostly needed for the input file as the output will create a file if it does not exist. Just good coding.

I will have to admit that I have been more focused on making the code work than what the function should be doing.

In the end what you might want to do is when you create the "temp.txt" file correctly you might consider deleting the "file.txt" and renaming "temp.text".

The "update" function works, but there are better ways to use this function than having to read and write files each time you call the function. If you are interested or can do something different.

Hope that helps,

Andy
Yeah, it helped me thank you so much!
Topic archived. No new replies allowed.