Sorting? Or Searching

Pages: 1234567
Yes.

Aceix.
Last edited on
detail.Age = atoi(word.c_str());
I don't understand how this works.
I know its changing string to int but how and using what?

also what is 'iss'?

should it be is?
Last edited on
faieq92 wrote:
and using what?

Using the atoi(); function.

It is taking a const char* form of the string variable, word with the help of the c_str(); member function.

HTH,
Aceix.
getline(iss,]]whats that doing there

I don't use get line much so I don't understand it fully
Let's look at the code I posted last time.
4
5
6
7
8
9
10
11
12
13
14
    string line;
    string word;
    char delim = '\t';
    getline(is, line);

    istringstream iss(line);

    getline(iss, word, delim);
    detail.FirstName = word;
   
   ... etc.

Line 7 reads a entire line (up to the newline character '\n') into the string line.

Line 9 creates a stringstream called iss and loads the string line into it. A stringstream allows us to do input or output using a string, as if it was a file.

Then we use getline again, using the stringstream which we just created. This is the really useful part. We use the delimiter '\t' (or other value if you prefer) which allows the reading of an item which contains spaces.

http://www.cplusplus.com/reference/sstream/
Last edited on
so getline(iss, word, delim);


this is the way I see it.

iss holds the line that we are reading. word? is all the string up to the delim??

word is all of the string up to the first delimiter, yes.

Then we repeat that getline, once for each item. Each time it reads the next portion of the string, starting just after the previous delimiter, and up to the next one.
Last edited on
also one more question what is causing it to read the next portion of the string . Why hasn't it gone back to the start.? I know its doing what I want it to do but ?
It's the stringstream, it mimics the behaviour of a normal file.

If you use getline() on an ordinary file, it reads the first line on the first call, and if you use getline again, it reads the second line and so on. It uses an internal pointer to keep track of the position it has reached in the file. Similarly the stringstream keeps track of the position within the stream.
Last edited on
I get a load of these errors.?


1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\string(448) : see declaration of 'std::getline'
1>c:\users\faieq\documents\visual studio 2010\projects\attempt 2 challange 1\attempt 2 challange 1\attemp2.cpp(134): error C2784: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &&,std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &&' from 'int'
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\string(395) : see declaration of 'std::getline'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
etc
Also says

error no instance of overloaded function "get line " matches argument list.
Did you put #include <sstream> as well as #include <string>

Those messages indicate on which line the problem occurred, attemp2.cpp(134).
Around line 134 is one of the problems.
that has fixed it. However when I input text into cin it is not accepting spaces?
That's a good point which I forgot to mention.

Instead of
 
    cin >> member.RoadName;

if you want to permit spaces, use
 
    getline(cin, member.RoadName);

and so on...

(the numeric values should be ok without change, as there won't be any spaces).
Last edited on
Thanks but for some reason on test.txt file it is only keeping records of one person whoever was stored latest. I can't seem to pinpoint the reason for this
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
void DataEntry(Details &member){
	ofstream DataSave("test.txt");
	DataSave <<"\n";
	cout << "First Name: ";
    getline(cin, member.FirstName);
	DataSave << member.FirstName<<"\t";

    cout << "Last Name: ";
    getline(cin, member.LastName);
	DataSave << member.LastName<<"\t";

	cout << "Age(Please Enter an Integer(Number)): ";
    cin >> member.Age;
	DataSave << member.Age<<"\t";

	cout << "Email: ";
	getline(cin, member.Email);
	DataSave << member.Email<<"\t";

    cout << "Door Number(Please Enter an Integer(Number)): ";
    cin >> member.DoorNumber;
	DataSave << member.Age<<"\t";

    cout << "Road Name: ";
    getline(cin, member.RoadName);
	DataSave << member.RoadName<<"\t";

    cout << "Post Code(NOTE:Please use Lower Case): ";
	getline(cin, member.PostCode);
	DataSave << member.PostCode<<"\t";
	DataSave <<"\n";
	
}


also it seems not to work. it seems to be putting them all together and allowing me to input only one thing??
Last edited on
I think you need to share your code, at least for the part where you output to the file.
I have if you look above or is it not showing up?
it was done with normal c in before like how it is for Age. And even then it was only keeping one record in the file
I think this is the code you mean:
http://www.cplusplus.com/forum/beginner/89067/2/#msg478536

Here, the file is opened at line 2 of function DataEntry() and it will close at the end of the function. The file will be created fresh each time, thus only the most recent version will be kept.

There are several possible solutions. One is to open the file with a mode of ios::app, in order to append the new data at the end of the existing file.
 
    ofstream DataSave("test.txt", ios::out | ios::app);


Another possibility would be to open the file before calling the function DataEntry(), and close it after all the user-input has been done.

Yet another alternative would be to store all the user input in an array. Then have a function which opens the file, then loops through each array element and output from the array to the file.
Pages: 1234567