如果无法打开文件,我在C++ void函数中使用exit.这样做是好的做法吗?
void ReadData()
{
ofstream myfile ("example.txt");
if (! myfile.is_open())
{
cout << "Unable to open file";
exit(0);
}
else {
myfile << "This is a line.\n";
myfile << "This is another line.\n";
myfile.close();
}
}
Run Code Online (Sandbox Code Playgroud) 我只是想将pow的值赋给变量,我已经用过了
#include<stdio.h>
#include<math.h>
int main( void )
{
int x;
int y;
int z;
x=10;
y=2;
z = pow(x,y);
printf ("%d" , &z);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但在输出中我得到-1076813284,对不起,但我刚开始学习C并且在每个教程中每个人都只打印pow的值,就像
printf("Value 3.05 ^ 1.98 = %lf", pow(3.05, 1.98));
Run Code Online (Sandbox Code Playgroud)
我不知道如何将它分配给变量
假设我有一个用于fopen的RAII类和一个名为ManagedFile的fclose,由于某种原因它失败并且构造函数抛出.
ManagedFile mf("e.txt") //fails and throws
mf.F( //What would happen here?9
Run Code Online (Sandbox Code Playgroud)
永远不会被创造出来吗?
如果它不会,如果我以后使用mf会发生什么,假设在mf中访问文件*F?
如果RAII类的构造函数失败,究竟会发生什么?
我试图call_function使用一个字符串参数,并调用与该映射中的字符串相对应的函数,但出现错误。我该如何解决?为什么不起作用?
#include <iostream>
#include <unordered_map>
using namespace std;
class xyz {
public:
unordered_map< std::string, void(xyz::*)()> arr{
{ "user", &xyz::func_user},
{ "pwd", &xyz::func_pwd},
{ "dir", &xyz::func_dir}
};
void call_function(std::string x) {
arr.at( x)();// Error: term does not evaluate a function taking 0 arguments
}
void func_user(){
cout << "func_user" << endl;
}
void func_pwd(){
cout << "func_pwd" << endl;
}
void func_dir(){
cout << "func_dir" << endl;
}
};
int main(){
xyz a;
a.call_function( "dir");
}
Run Code Online (Sandbox Code Playgroud) 我刚开始学习c ++,我试图让自己接受简单的加法和减法.但是我似乎无法运行此功能.任何输入都将得到很好的赞赏.我也接受建设性的批评:)
#include "stdafx.h"
#include <iostream>
int main()
{
std::cout << " I have a qustion for you Devante. Here it is . . . if you add the word two to the number 2, what do you get ?";
int x = 4;
std::cin >> x;
std::cout << "Correct, the correct answer is " << x << std::endl;
return 0;
}
int main()
{
std::cout << " Since you got the answer right this time, lets see if you …Run Code Online (Sandbox Code Playgroud)