-2

I am trying to create a text file that the user inputs the name for. It should just create the text file and then close it, and then give feedback through the terminal if it was created successfully or not. I was able to figure out how to create the file if I hardcoded the file name but now that I am trying it with user input I can't get the file to be created. Right now it doesn't even let me type anything in, it just ends when i call the function. Thank you for your help.

EDIT: I am not sure if it's because my getline call is just getting a blank line then ending or why it won't let me cin anything.

EEDIT: This is my full code,I guess the part I thought wasn't working is, so hopefully somebody can tell me why my code just ends after i select 1 for my switch case. Obviously my code isn't complete, but this is everything I have.

#include <iostream>
#include <fstream>
#include <stdio.h>

using namespace std;

void createDB() {
    ofstream db;
    string filename;
    cout << "Enter the name of the database you want to create: \n";
    getline (cin, filename);

    db.open(filename.c_str());
    if(!db) { // checking if the file could be opened
        cout << "\nCould not create database\n";
    }// add more checks to make sure file doesn't have same name
    else {
        cout << "\nYour database " << filename << " was created successfully\n";
    }

    db.close();

}

void openDB() { // just opens hard coded file for now
    // need to add check to see if one is already open
    cout << "Enter the name of the database you want to open: ";
    string line;

    //ifstream file (
}

void closeDB() {

}

void display() {

}

void update() {

}

void create() {

}

void add() {

}

void del() {

}

int menu() {
    cout << "Enter the number of the operation you wish to perform (1-9)\n"
    << "1. Create new database\n"
    << "2. Open database\n"
    << "3. Close database\n"
    << "4. Display record\n"
    << "5. Update record\n"
    << "6. Create report\n"
    << "7. Add a record\n"
    << "8. Delete a record\n"
    << "9. Quit\n";

    int sel = 0;
    cin >> sel;

    switch (sel) {
        case 1: createDB();
            //menu(); // after creating file go back to list of options
            break;

        case 2: openDB();
            break;

        case 3: closeDB();
            break;

        case 4: display();
            break;

        case 5: update();
            break;

        case 6: create();
            break;

        case 7: add();
            break;

        case 8: del();
            break;

        case 9: return 0;
            break;

        default: cout << "Please try again and enter a valid number\n\n";
            menu();
            break;
    }
    return true; // to avoid error saying control may reach end of non-void function
}


int main() {
    menu();

    return 0;
}
justinbg10
  • 51
  • 1
  • 7

1 Answers1

0

The piece of code you posted works actually, but it seems getline skips input because of your previous code. So it would be really helpful if you posted the rest of your code.

Therefore because you are using cin before getline you should add std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); after cin >> sel;.

So your menu function would be

int menu() {
cout << "Enter the number of the operation you wish to perform (1-9)\n"
    << "1. Create new database\n"
    << "2. Open database\n"
    << "3. Close database\n"
    << "4. Display record\n"
    << "5. Update record\n"
    << "6. Create report\n"
    << "7. Add a record\n"
    << "8. Delete a record\n"
    << "9. Quit\n";

int sel = 0;
cin >> sel;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

switch (sel) {
case 1: createDB();
    //menu(); // after creating file go back to list of options
    break;

case 2: openDB();
    break;

case 3: closeDB();
    break;

case 4: display();
    break;

case 5: update();
    break;

case 6: create();
    break;

case 7: add();
    break;

case 8: del();
    break;

case 9: return 0;
    break;

default: cout << "Please try again and enter a valid number\n\n";
    menu();
    break;
}
return true; // to avoid error saying control may reach end of non-void function
}
Community
  • 1
  • 1
BnBDim
  • 136
  • 5