Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 161703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T11:15:36+00:00 2026-05-11T11:15:36+00:00

Every time I do anything, and my while(1) gets called in my main function,

  • 0

Every time I do anything, and my while(1) gets called in my main function, my file gets cleared. It’s driving me crazy. I’ve tried EVERYTHING. I try to put my ofstream out(‘data.dat’); outside the while(1) statement so it isn’t called everytime but then nothing is written to the file like the ofstream isn’t even working!

I’ve tried to make the ofstream static, so it isn’t called over and over again like:

static ofstream open('data.dat'); 

That doesn’t work.

And like I said, when I put the ofstream OUTSIDE the while statement, nothing is written to the file. Like:

ofstream out('data.dat');      while (1)     {           string line = '';         cout << 'There are currently ' << structList.size() << ' items in memory.';         cout << endl << endl;         cout << 'Commands: ' << endl;         cout << '1: Add a new record ' << endl;         cout << '2: Display a record ' << endl;         cout << '3: Edit a current record ' << endl;         cout << '4: Delete a record ' << endl;         cout << '5: Save current information ' << endl;         cout << '6: Exit the program ' << endl;         cout << endl;         cout << 'Enter a command 1-6: ';          getline(cin , line);          int rValue = atoi(line.c_str());          system('cls');          switch (rValue)         {             case 1:                 structList = addItem(structList);                 break;             case 2:                 displayRecord(structList);                 break;             case 3:                 structList = editRecord(structList);                 break;             case 4:                 deleteRecord(structList);                 break;             case 5:                 if (!structList.size()) { cout << 'There are no items to save! Enter one first!' << endl << endl; system('pause'); system('cls'); break; }                 writeVector(out , structList);                 break;             case 6:                 return 0;             default:                 cout << 'Command invalid. You can only enter a command number 1 - 6. Try again. ' << endl;         }          out.close();     } 

And can someone tell me why my check to prevent reading of a empty file won’t work?

My Check:

bool checkFileEmpty() {     ifstream in('data.dat');      if (in.peek() == in.eofbit)     {         return true;     }      return false; } 

I am so sick and tired of my program crashing on startup over and over again because my vector is getting set to a size of 200 million. I’ve tried a BUNCH of stuff for this… none of it works… Please GOD someone help me with both of these! I’ve been up for 18 hours working on this ( all night yes ) and i’m ALMOST done. I’m begging you….

My Code:

#include 'stdafx.h' #include <iostream> #include <fstream> #include <string> #include <vector>  using namespace System; using namespace std; #pragma hdrstop  bool isValidChoice(int size, int choice);  bool checkFileEmpty();  template<typename T> void writeVector(ofstream &out, const vector<T> &vec);  template<typename T> vector<T> readVector(ifstream &in);  template<typename T> vector<T> addItem(vector<T> &vec);  template<typename T> void printItemDescriptions(vector<T> &vec);  template<typename T> int displayRecord(vector<T> &vec);  template<typename T> vector<T> editRecord(vector<T> &vec);  template<typename T> vector<T> deleteRecord(vector<T> &vec);   struct InventoryItem {     string Description;     int Quantity;     int wholesaleCost;     int retailCost;     string dateAdded; } ;   int main(void) {     cout << 'Welcome to the Inventory Manager extreme! [Version 1.0]' << endl;     ifstream in('data.dat');     if (in.is_open()) { cout << 'File \'data.dat\' has been opened successfully.' << endl; } else { cout << 'Error opening data.dat' << endl;}     cout << 'Loading data...' << endl;     vector<InventoryItem> structList = readVector<InventoryItem>( in );     cout <<'Load complete.' << endl << endl;     in.close();      while (1)     {           string line = '';         cout << 'There are currently ' << structList.size() << ' items in memory.';         cout << endl << endl;         cout << 'Commands: ' << endl;         cout << '1: Add a new record ' << endl;         cout << '2: Display a record ' << endl;         cout << '3: Edit a current record ' << endl;         cout << '4: Delete a record ' << endl;         cout << '5: Save current information ' << endl;         cout << '6: Exit the program ' << endl;         cout << endl;         cout << 'Enter a command 1-6: ';          getline(cin , line);          int rValue = atoi(line.c_str());          system('cls');          ofstream out('data.dat');          switch (rValue)         {             case 1:                 structList = addItem(structList);                 break;             case 2:                 displayRecord(structList);                 break;             case 3:                 structList = editRecord(structList);                 break;             case 4:                 deleteRecord(structList);                 break;             case 5:                 if (!structList.size()) { cout << 'There are no items to save! Enter one first!' << endl << endl; system('pause'); system('cls'); break; }                 writeVector(out , structList);                 break;             case 6:                 return 0;             default:                 cout << 'Command invalid. You can only enter a command number 1 - 6. Try again. ' << endl;         }          out.close();     }      system('pause');      return 0; }  template<typename T> void writeVector(ofstream &out, const vector<T> &vec) {     out << vec.size();      for(vector<T>::const_iterator i = vec.begin(); i != vec.end(); i++)     {         out << *i;     }     cout << 'Save completed!' << endl << endl; }  ostream &operator<<(ostream &out, const InventoryItem &i) {     out << i.Description << ' ';     out << i.Quantity << ' ';     out << i.wholesaleCost  << ' ' << i.retailCost  << ' ';     out << i.dateAdded  << ' ';     return out; }   istream &operator>>(istream &in, InventoryItem &i) {     in >> i.Description;     in >> i.Quantity;     in >> i.wholesaleCost >> i.retailCost;     in >> i.dateAdded;     return in; }    template<typename T> vector<T> readVector(ifstream &in) {      size_t size;     if (checkFileEmpty())     {         size = 0;     } else {         in >> size;     }      vector<T> vec;     vec.reserve(size);      for(unsigned int i = 0; i < size; i++)     {         T tmp;         in >> tmp;         vec.push_back(tmp);     }      return vec; }  template<typename T> vector<T> addItem(vector<T> &vec) {     system('cls');      string word;     unsigned int number;      InventoryItem newItem;      cout << '-Add a new item-' << endl << endl;     cout << 'Enter the description for the item: ';     getline (cin , word);     newItem.Description = word;      cout << endl;     cout << 'Enter the quantity on hand for the item: ';     getline (cin , word);     number = atoi(word.c_str());     newItem.Quantity = number;      cout << endl;     cout << 'Enter the Retail Cost for the item: ';     getline (cin , word);     number = atoi(word.c_str());     newItem.retailCost = number;      cout << endl;     cout << 'Enter the Wholesale Cost for the item: ';     getline (cin , word);     number = atoi(word.c_str());     newItem.wholesaleCost = number;      cout << endl;     cout << 'Enter current date: ';     getline (cin , word);     newItem.dateAdded = word;      vec.push_back(newItem);      return vec; }  template<typename T> void printItemDescriptions(vector<T> &vec) {     int size = vec.size();      if (size)     {         cout << '---------------------------------' << endl;         cout << '|      ~ Item Descriptions ~    |' << endl;         cout << '---------------------------------' << endl;         cout << '*********************************' << endl;         for (int i = 0 ; i < size ; i++)         {             cout << '(' << i+1 << ')' << ': ' << vec[i].Description << endl;         }         cout << '*********************************' << endl << endl;     } }  template<typename T> int displayRecord(vector<T> &vec) {     string word = '';     string quit = 'quit';     int choice = 1;     int size = vec.size();      if (size)     {         printItemDescriptions(vec);         cout << endl;          while (1)         {             cout << 'Type \'exit\' to return to the Main Menu.' << endl << endl;             cout << 'Enter \'list\' to re-display the items.' << endl << endl;             cout << endl;             cout << 'Pick the number of the item you would like to display: ';             getline (cin , word);              if (convertToLower(word) == 'exit') { system('cls'); return 0; }             if (convertToLower(word) == 'list') { system('cls'); displayRecord(vec); }              choice = atoi(word.c_str());              choice -= 1;              if (isValidChoice(size, choice))             {                 system('cls');                 cout << endl << '[Item (' << choice << ') details] ' << endl << endl;                 cout << '******************' << endl;                 cout << '*  Description   * ' << vec[choice].Description << endl;                 cout << '******************' << endl << endl;                 cout << '******************' << endl;                 cout << '*Quantity On Hand* ' << vec[choice].Quantity << endl;                 cout << '******************' << endl << endl;                 cout << '******************' << endl;                 cout << '* Wholesale Cost * ' << vec[choice].wholesaleCost << endl;                 cout << '****************** ' << endl << endl;                 cout << '******************' << endl;                 cout << '*  Retail Cost   * ' << vec[choice].retailCost << endl;                 cout << '****************** ' << endl << endl;                 cout << '******************' << endl;                 cout << '*  Data Added    * ' << vec[choice].dateAdded << endl;                 cout << '****************** ' << endl << endl;             } else { system('cls'); cout << 'That item doesn't exist!' << endl; cout << 'Pick another item or enter \'list\' to see available items.' << endl << endl; }         }     } else { cout << 'There are currently no items to display.' << endl << endl; system('pause'); system('cls'); return 0; }      return 1; }  bool isValidChoice(int size, int choice) {     for (int i = 0 ; i <= size ; i++)     {         if (choice == i) { return true; }     }     return false; }  string convertToLower(string word) {     for (unsigned int i = 0 ; i < word.size() ; i++)     {         word[i] = tolower(word[i]);     }      return word; }  bool checkFileEmpty() {     ifstream in('data.dat');      if (in.peek() == in.eofbit)     {         return true;     }      return false; }  template<typename T> vector<T> editRecord(vector<T> &vec) {     string word;     int choice;     printItemDescriptions(vec);      cout << 'Choose item to edit: ';     getline ( cin, word );     choice = atoi(word.c_str());       system('cls');      unsigned int number;      InventoryItem newItem;      cout << '-Edit an item-' << endl << endl;     cout << 'Enter the description for the item: ';     getline (cin , word);     vec[choice-1].Description = word;      cout << endl;     cout << 'Enter the quantity on hand for the item: ';     getline (cin , word);     number = atoi(word.c_str());     vec[choice-1].Quantity = number;      cout << endl;     cout << 'Enter the Retail Cost for the item: ';     getline (cin , word);     number = atoi(word.c_str());     vec[choice-1].retailCost = number;      cout << endl;     cout << 'Enter the Wholesale Cost for the item: ';     getline (cin , word);     number = atoi(word.c_str());     vec[choice-1].wholesaleCost = number;      cout << endl;     cout << 'Enter current date: ';     getline (cin , word);     vec[choice-1].dateAdded = word;      system('cls');      cout << 'Item edited successfully! ' << endl;      return vec; }  template<typename T> vector<T> deleteRecord(vector<T> &vec) {     if (!vec.size()) { cout << 'There are no items to delete!' << endl << endl; return vec; }     printItemDescriptions(vec);      string word;     int choice;      cout << 'Choose item to delete: ';      getline( cin, word);      choice = atoi(word.c_str());      vec.erase (vec.begin()+choice-1);     return vec; } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T11:15:37+00:00Added an answer on May 11, 2026 at 11:15 am

    You’d better move the ofstream openning and closing inside case 5.

    Here you create a new file at each while iteration.

    case 5:         {             ofstream out('data.dat');             writeVector(out , structList);             out.close();         }         break; 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 118k
  • Answers 118k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Defining def __init__(self): self.foo = 5 in Base makes foo… May 11, 2026 at 11:31 pm
  • Editorial Team
    Editorial Team added an answer When you say this to a shell scriptA.py | scriptB.py… May 11, 2026 at 11:31 pm
  • Editorial Team
    Editorial Team added an answer You could use a TypeConverter and return a string or… May 11, 2026 at 11:31 pm

Related Questions

I have a set of transactions occurring at specific points in time: CREATE TABLE
This really, really urks me, so I hope that someone can give me a
Please note - I am not looking for the right way to open/read a
How do I distribute a small amount of data in a random order in

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.