我正在開發一個程序,允許用戶寫入,讀取和追加文本文件。我決定在這個函數中使用函數,而不是在一個主函數中使用許多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
您發佈了[MCVE](http://stackoverflow.com/help/mcve),我們可能會「與你同住」_。還沒有到目前爲止。 –
我建議你切換回'cin'並使用調試器來查看該語句後會發生什麼。 –
你確定你可以編譯你的代碼嗎?它看起來不像C++代碼。 – Tim3880