0 Members and 2 Guests are viewing this topic.
#include<fstream.h>#include<iostream.h>void main(){ char string[25], header[50] = "Name \0Address \0City \0State \0ZIP Code "; ifstream file_ptr; int offset = 0; file_ptr.open("ADDRFILE.DAT",ios::in); if (file_ptr) { do { file_ptr.get(string, 25); file_ptr.ignore(80, '\n'); if (!file_ptr.eof()) cout << header + 10 * offset << ':' << string << '\n'; offset = (offset + 1) % 5; } while (!file_ptr.eof()); file_ptr.close(); } else cout << "An Error occured!\n";}
#include<fstream.h>#include<iostream.h>void main(){ char name[25], address[25], city[25], state[25], zip[25]; ifstream file_ptr; file_ptr.open("ADDRFILE.DAT",ios::in); if (file_ptr) { do { file_ptr.get(name, 25); file_ptr.ignore(80, '\n'); file_ptr.get(address, 25); file_ptr.ignore(80, '\n'); file_ptr.get(city, 25); file_ptr.ignore(80, '\n'); file_ptr.get(state, 25); file_ptr.ignore(80, '\n'); file_ptr.get(zip, 25); file_ptr.ignore(80, '\n'); if (!file_ptr.eof()) { cout << "Name: " << name << '\n'; cout << "Address: " << address << '\n'; cout << "City: " << city << '\n'; cout << "State: " << state << '\n'; cout << "ZIP Code: " << zip << '\n'; } } while (!file_ptr.eof()); file_ptr.close(); } else cout << "An Error occured!\n";}
Sorry you had to downgrade your project.
Sigh... If the teacher can't understand something you can, he ought to learn how your code works...
cout << header + 10 * offset << ':' << string << '\n';offset = (offset + 1) % 5;
That code isn't hard to follow at all, and its not very complex. But I have a feeling what your teacher is complaining about is that you made it very "C style" instead of "C++ style". They want to train you to stick with the C++ commands and not do those efficient workarounds becasue eventually you're going to be dealing with abstract objects and your code will no longer be portable when instead of strings you have classes and instead of characters you have child objects.