int getint(string x){
std::stringstream ss(x);
int result;
while (!ss>>result) {
if (ss.eof()) throw std::runtime_error("reached end of input");
std::cin.clear();
std::cout << "Please Enter a number: ";
std::getline(std::cin, x);
ss.str(x);
}
return result;
}
@MohamedAhmedNabil getline has the side effect that when it reaches the delimiter '\n', it leaves that in the buffer. cin >> i only reads until the next whitespace, which is immedately, so the cin >> i after a getline will fail, unless you use cin.ignore(2, '\n') between.
@MohamedAhmedNabil the string represents a series of characters. Internally it keeps them floating in memory in a completely random location, and just keeps track of where they are.
@MohamedAhmedNabil sort of like a person keeping track of where your house is.
@MohamedAhmedNabil a std::string name represent the series of characters, just like a c-string. However, a c-string can also be treated as if it were a pointer to the first element. a std::string cannot.
@MooingDuck actually the book is alot simplier in its excercises, I like to expand and develop, I learned alot that way. The book usees c-strings but i said her STD::STRING, Also the back feature was my idea
@MooingDuck I know about pointers yea, learned about them and used them alot, but not dynamic memory
actually, here's a better explanation: a std::string is actually three variables, and a bunch of cool functions. The first variable is the number of characters. The second variable is unimportant for now. The third variable is a pointer to a null terminated c-string.
@MohamedAhmedNabil I spoke unclearly. The letters are in a random place in memory, but they are all in a row/array/c-string together. somewhere not inside the string.
@MooingDuck Im using random acccess by using fixed size records while storing them in the binary file, knowing where the record is , is a matter of multyplying the recordsize by the record number
@MooingDuck So i thought i could use std::string as a way to keep the input to a very large extent and yet keep the record fashion of the program
Basically, if i read and write in the same program with std::string like this `obj.write(reinterpret_cast<char*>(&name), sizeof(name)) its no problem, since the pointers keep pointing to the same thing, So writing an std::string that way and reading it again, shouldnt be a problem
Ok ill just leave this here, you said, three variables, and a bunch of cool functions. The first variable is the number of characters. The second variable is unimportant for now. The third variable is a pointer to a null terminated c-string.
@MohamedAhmedNabil but if you change name, the string might move the cstring to a completely new place in memory, so when you read in the old three variables, the cstring pointer is invalid and will crash the program.
if you change the word and it doesn't move it, then it will just erase the old string and put the new one where the old used to be. No crash, but the words are wrong.
@MohamedAhmedNabil if you change it to be a longer word/name.