2015-05-29 60 views
-1

我正在开发一个程序,允许用户写入,读取和追加文本文件。我决定在这个函数中使用函数,而不是在一个主函数中使用许多if else语句(我喜欢结构)。但是,我似乎无法获得函数调用来调用函数。我不确定我出错的地方。C++调用函数没有成功调用函数

当使用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 
+3

您发布了[MCVE](http://stackoverflow.com/help/mcve),我们可能会“与你同住”_。还没有到目前为止。 –

+1

我建议你切换回'cin'并使用调试器来查看该语句后会发生什么。 –

+1

你确定你可以编译你的代码吗?它看起来不像C++代码。 – Tim3880

回答

1
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();,其他操作也相似。


但是采取进一步的回头看看你的代码中也有

ofstream write; 
//declare writer as ofstream 
ifstream read; 
//declaire read as ifstream 
ofstream append; 
//declare ofstream as append; 

你必须使用相同的名称作为函数的变量...这只是将是一个烂摊子,可能会阻止你从能够首先正确地调用功能。

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

+0

s/may/will。局部变量会影响函数 –

+0

谢谢,我刚才发现丢失括号的问题,我还将函数的名称改为writeToFile(),appendToFile()和readFromFile()所以他们不再发生冲突,我也采取了托马斯马修的建议,并恢复为cin。l我的写函数现在似乎工作正常,但其他两个都没有做任何事情。 – Anniee