如何将值输入到使用.txt文件名称的对话中

时间:2014-11-19 05:44:45

标签: c++ xcode file

很抱歉,如果之前有人询问,但我找不到任何其他内容。这是课堂作业。 好吧,所以我在Xcode中打开了一个.txt文件,我能够将它的内容打印到屏幕上。然而,我的目标是在使用switch语句的随机对话中使用该内容(“Paul”)并将其写回输出文件,同时还将对话显示在屏幕上。我现在的问题是,即使我放了一个cin,我也无法输入任何东西;该计划刚刚结束。我该如何解决?我是C ++的初学者。感谢任何帮助。

/*
ELEN 1301-02 Programming Assignment #12.

Name : Neal Patel   
Student ID : L20306621
Due Date : November 20, 2014

Objective of this assignment :
Read a name from the input file. Make a small talk with a user. Record entire conversation to the output file.

Step 1.
Open an input and an output file.

Step 2.
Read a name from the input file.

Step 3.
Make a small talk with a user. The name you stored in the input file will be the name of the character. Ask a user's name and incorporate it in the conversation.

Step 4.
Repeat Step 3 at least 5 times with randomized different replies.

Step 5.
Record the entire session to the output file.

Step 6.
Close both input and output files.

* /

#include<iostream>
#include<fstream>
#include<cstdlib>
using namespace std;

int main()
{
    int response;
    string ifilename, ofilename, line, r1, r2, r3, r4, r5, r6, r7;
    ifstream inFile, checkOutFile;
    ofstream outFile;

    // Step 1
    cout << "Please enter the name of the file you wish to read : ";
    cin >> ifilename;

    inFile.open(ifilename.c_str());

    if(inFile.fail())
    {
        cout << "The file " << ifilename << " was not successfully opened." << endl;
        cout << "Please check the path and name of the file. " << endl;
        exit(1);
    }
    else
    {
        cout << "The file is successfully opened.";
    }

    cout << "\nPlease enter the name of the file you wish to write : ";
    cin >> ofilename;

    checkOutFile.open(ofilename.c_str());

    if(!checkOutFile.fail())
    {
        cout << "A file " << ofilename << " exists.\nDo you want to continue and overwrite           it? (y/n) : ";
        cin >> response;
        if(tolower(response) == 'n')
        {
        cout << "The existing file will not be overwritten. " << endl;
        exit(1);
        }
    }

    outFile.open(ofilename.c_str());

    if(outFile.fail())
    {
        cout << "The file " << ofilename << " was not successfully opened." << endl;
        cout << "Please check the path and name of the file. " << endl;
        exit(1);
    }

    if (inFile.is_open())
    {
        while (!inFile.eof() )
        {
            // Step 2
            getline (inFile,line);
            cout << line << endl;
        }
        cout << "Hello, my name is " << line << ". What is your name : ";
        cin >> r1;
    }
}

0 个答案:

没有答案