CS Problem 321_HW4_1 output looks OK! |
std::string s;
std::vector<std::string> v;
while (1) {
/* Read the next word from std::cin here.
Stop reading when you hit white space.
Break once there's nothing more to read. */
???
v.push_back(s);
}
for (unsigned int i=0;i<v.size();i++)
std::cout<<"The "<<i<<"th string read is '"<<v[i]<<"'\n";
return v.size();
int foo(void) {
std::string s;
std::vector<std::string> v;
while (1) {
/* Read the next word from std::cin here.
Stop reading when you hit at semicolon or period,
and do NOT include the semicolon or period in s.
Break once there's nothing more to read. */
???
v.push_back(s);
}
for (unsigned int i=0;i<v.size();i++)
std::cout<<"The "<<i<<"th string read is '"<<v[i]<<"'\n";
return v.size();
}
int foo(void) {
std::vector<int> v;
while (1) {
int i;
/* Read big-endian binary ints from the standard input here.
Stop reading when no more ints are available. */
???
v.push_back(i);
}
for (unsigned int i=0;i<v.size();i++)
std::cout<<"The "<<i<<"th int read is '"<<v[i]<<"'\n";
return v.size();
}
struct dude {
public:
std::string name;
std::string address;
double karma;
std::vector<std::string> blogs;
};
/* Make up a random string for testing */
std::string makeString(int length) {
std::string s="";
for (int i=0;i<length;i++) {
if (length<20) s+='A'+(rand()%24);
else if ((rand()&0xff)<5) s+='\n';
else s+=32+(rand()%(127-32));
}
return s;
}
/* Make up some random dude objects for testing */
std::vector<dude> makeDudes(void) {
std::vector<dude> dudes;
int i,n=2+(rand()&3);
for (i=0;i<n;i++) {
dude f;
f.name=makeString(6);
f.address=makeString(150);
f.karma=rand()&511;
for (int j=0;j<(rand()&3);j++)
f.blogs.push_back(makeString(18));
dudes.push_back(f);
}
return dudes;
}
void printDude(dude &f) {
std::cout<<"Dude:\n";
std::cout<<" name='"<<f.name<<"'\n";
std::cout<<" address='"<<f.address<<"'\n";
std::cout<<" kharma="<<f.karma<<"\n";
for (unsigned int i=0;i<f.blogs.size();i++)
std::cout<<" blog='"<<f.blogs[i]<<"'\n";
}
void printDudes(std::vector<dude> &dudes) {
for (unsigned int i=0;i<dudes.size();i++) printDude(dudes[i]);
}
/* Write and read all these dude objects to/from this disk file.
You MUST write both these routines, and CANNOT change any other routine,
(in particular, you must not change foo!)
*/
void writeDudes(const char *toFile,std::vector<dude> dudes) {}
void readDudes(const char *fromFile,std::vector<dude> &dudes) {}
int foo(void) {
srand(read_input());
/* Make up some dudes, and write them to disk */
writeDudes("dudes.bin",makeDudes());
/* Read the dudes we just wrote */
std::vector<dude> dudes;
readDudes("dudes.bin",dudes);
if (dudes.size()==0) std::cout<<"Woa--I didn't read any dudes?\n";
printDudes(dudes);
return 0;
}