2015-05-29 112 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