Start New Discussion within our Software Development Community

I need to create a program where it asks the user where is a text file. The file will open and it will be a list of names. The names should be stored in a vector after being read by the program.The program then will call a function named print that prints to the monitor the list of names, numbered starting with 1. It will ask the user if they want to delete a name, insert a name, or write the names to a file and end the program. The problem here is, i can get the file to open up and display the names, but i can't get it to prompt the user what to do next. It basically just displays the file, but doesnt allow for any editing.

# include <iostream>
# include <fstream>
# include <cstdlib>
# include <iomanip>
# include <string>
# include <vector>

using namespace std;

int choice=0;

int main()
{
    string filePath = "C:\\17\\test\\";
    string fileName = "";
    string name = "";
    char character=' ';

    ofstream myfile("C:\\17\\test\\");
    ifstream inputFile;
    
    vector<string>names(4," ");
    
    cout << "Please enter the name of the input file: ";
    getline(cin, fileName);
    fileName = filePath + fileName;

    inputFile.open(fileName.c_str());
    
    while(inputFile.get(character)) 
    {
        cout << character;
        cout<<endl;
    }
    
    cout<<"Do you want to:";
    cout<<endl;
    cout<<"1. Delete a name";
    cout<<endl;
    cout<<"2. Insert a name";
    cout<<endl;
    cout<<"3. Write the names to a file and end the program";
    cout<<endl;
    cin>>choice;

    
    cin.get();
    cin.get();
    return 0;
}

Yes, i am a beginner at C++ :lol:

Add a switch statement after line 44 that will execute the desired action. then put lines 30 to 44 (and code you add in the switch statement) into a do or while loop so that it can be executed more than once.

This article has been dead for over six months. Start a new discussion instead.