C ++调用函数未成功调用函数

时间:2015-05-29 00:05:50

标签: c++

我正在开发一个程序,允许用户写入,读取和追加文本文件。我决定在一个主函数(我喜欢结构)中使用函数而不是很多if else语句。但是,我似乎无法调用函数调用函数。我不知道我哪里出错了。

使用cin >> choice时,程序在输入选择时终止。

使用scanf_s("\n%c", &choice)时,程序会在终止前声明输入错误。

我正在使用VSE 2013。

#include "stdafx.h"
#include <iostream> //C++ header file for input/output
#include <iostream> //C++ header file for input/output
#include <conio.h> // include library for pause function
#include <fstream> //for both read and write functionality
#include <string>

using namespace std;
//--------------------WRITE FUNCTION-----------------------------

int write()//main function 
    cout << "You have opted to write to the file.";

    string x;
ofstream mynewfile;
//open output file stream
    mynewfile.open("testC++.txt");
    //use ofstream object to open file created within paranethesis
    mynewfile << "I have a hairy butt.\n";
    //push the above char data to the file
    cout << "Please write something: \n";//outputs
    //  std::string x;//store input as x, already declared string
    mynewfile << x;//write input to file
    mynewfile.close();
    //close file on execution for resources purposes
    return 0;
    // '0' is returned as a 'success flag'  
//---------------------APPEND FUNCTION--------------------------------

int append()
{
    string x;
    ofstream mynewfile;
    ifstream read;
    ofstream append;
    string line;
    cout << "You have opted to append the text file.";
    append.open("testC++.text", ios::app);
    cin >> x;
    append << x << "  ";
    append.close();
    read.open("testC++.text");
    if (read.is_open())//if read is open

{
        while (getline(read, line))
{
            cout << line << "\n";//output file to screen
}
        append.close();
}
    else cout << "Unable to append file. \n"; //else 
    return (0);

//------------------------READ FUNCTION-------------------------------------

int read()
{
    ifstream read;
    string line;
read.open("testC++.text");//open text file
if (read.is_open())//if text file is open
    while (getline(read, line))
{
        cout << line << "\n";//output file to screen
}
    read.close();
}
else cout << "Unable to read the file. \n"; // else error message
return (0);
//--------------MAIN FUNCTION----------------------------------------------------
int main()
//declare main function
{
    int choice;
    ofstream write;
    //declare writer as ofstream
    ifstream read;
    //declaire read as ifstream
    ofstream append;
    //declare ofstream as append;
    cout << "1. Write to file.\n";
    //output to screen to prompt user for their choice of task
    cout << "2. Append to file.\n";
    cout << "3. Display the text file.\n";
    cout << "4. Exit the program.\n";
    cout << "Please select the operation you wish to carry out by selcting the corresponsing number\n";
    scanf_s("\n%c", &choice); // trying to get the function calls to work I tried scanf_s(states incorrent input)
    //cin >> choice; --- origional input method (program just terminates)
    //declare choice as user input
if (choice == 1)
write;
//if user input = 1 call write function
else if (choice == 2)
append;
// if user input = 2 call read function
else if (choice == 3)
read;
//if user input = 3 call read function
else if (choice == 4)
//cout << "The program will now quit. \n";
void _exit( //_exir function termites calling process in LIFO order.
void _exit( //_exir function termites calling process in LIFO order.
);
else if (choice != 4 || choice > 4)
cout << "incorrect value entered, please enter a value from 1-4";
//if user input = 4 call exit function
return 0;
system("pause"); // not the best way I am aware
}
//--------------------------------------------------------------------------------------------------

//program ends

1 个答案:

答案 0 :(得分:1)

In [83]: d = datetime.date(2014,1,1)

In [84]: d.strftime("%Y%m%d")
Out[84]: '20140101'

这些语句不调用函数。事实上,他们根本没有做任何事情。

要调用if (choice == 1) write; //if user input = 1 call write function else if (choice == 2) append; // if user input = 2 call read function else if (choice == 3) read; //if user input = 3 call read function 功能,您需要执行write,其他人也需要这样做。

然而,进一步回顾你的代码,你也有

write();

你的变量与你的函数具有相同的名称......这只会是一团糟,可能会阻止你在第一时间正确调用函数。

清理您的命名,使它们不重叠。然后使用适当的语法调用函数。