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

C++ C++;调用函数未成功调用函数,c++,C++,我正在开发一个程序,允许用户写入、读取和附加文本文件。我已经决定使用这个函数,而不是在一个主函数中使用许多if-else语句(我喜欢结构)。但是,我似乎无法获得函数调用来调用函数。我不确定哪里出了问题 当使用cin>>choice时,当输入选择时,程序终止 当使用scanf\u s(“\n%c”,&choice)时,程序会在终止前声明不正确的输入 我正在使用VSE 2013 #include "stdafx.h" #include <iostream> //C++ header fi

我正在开发一个程序,允许用户写入、读取和附加文本文件。我已经决定使用这个函数,而不是在一个主函数中使用许多if-else语句(我喜欢结构)。但是,我似乎无法获得函数调用来调用函数。我不确定哪里出了问题

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

当使用
scanf\u 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
#包括“stdafx.h”
#包含用于输入/输出的//C++头文件
#包含用于输入/输出的//C++头文件
#包含//包含暂停函数的库
#包括//的读写功能
#包括
使用名称空间std;
//--------------------写函数-----------------------------
int write()//主函数
库特
这些语句不调用函数。事实上,他们什么都不做

要调用
write
函数,需要执行
write()和其他类似的代码


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

ofstream write;
//declare writer as ofstream
ifstream read;
//declaire read as ifstream
ofstream append;
//declare ofstream as append;
您有与函数同名的变量。。。这将是一个混乱的局面,可能会妨碍您首先正确调用函数


清理你的命名,这样它们就不会重叠。然后用适当的语法调用函数。

您发布了一篇文章,我们可能会“容忍您”。目前还没有。我建议您切换回
cin
,并使用调试器查看该语句之后会发生什么。您确定可以编译代码吗?它看起来不像C++代码。“我很喜欢<代码> IoSturi,我把它包括了两遍。”查看你的包含文件,并去掉那个<代码> STDAFX头文件!所有的
void\u退出(
stuff?s/may/will)。局部变量会影响函数。谢谢,我不久前意识到缺少括号的问题,我还将函数名更改为writeToFile()、appendToFile()和readFromFile()因此,它们不再冲突。我也采纳了Thomas Matthew的建议,回到了cin。我的write函数现在似乎工作得很好,尽管其他两个没有做任何事情。
ofstream write;
//declare writer as ofstream
ifstream read;
//declaire read as ifstream
ofstream append;
//declare ofstream as append;