我当前的一个项目要求我将一个文件的实际名称发送到一个类中,但我无法弄清楚如何将该字符串转换为变量.换句话说,我需要打开Andrew到变量name的Person类.
main.c中
#include <iostream>
#include <string>
#include "Person.h"
using namespace std;
int main ()
{
Person p1("Andrew");
}
Run Code Online (Sandbox Code Playgroud)
Person.h
#include <iostream>
using namespace std;
class Person
{
public:
Person (string name);
void setName(string name) {string name = name;}
void printName(string name) {cout << name;}
private:
string name;
};
Run Code Online (Sandbox Code Playgroud) 我只是从c转到C++,我正在尝试构建一个计算器.Int'结果'不会通过数学运算初始化.逻辑是,根据操作'',将有一个不同的值分配给'结果'.这似乎不起作用.
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
int main ()
{
int n1, n2;
char s,r;
int result = 0;
cout<< "Enter a calculation? (y/n)"<<endl;
cin>>r;
while(r=='y')
{
cout <<"Enter the first number"<<endl;
cin>>n1;
cout<<"Enter the operator"<<endl;
cin>>s;
cout<<"Enter the second number"<<endl;
cin>>n2;
if ('s' == '*')
{
result = n1*n2;
}
if ('s' =='+')
{
result = n1+n2;
}
if ('s' =='-')
{
result = n1-n2;
}
if ('s' =='/')
{
result = n1/n2;
}
cout << result<<endl;
cout<< …Run Code Online (Sandbox Code Playgroud) 此代码用于打印1到30之间的非素数.它是如何工作的以及错误在哪里.
BEGIN
<<outer>>
FOR i in 1..30
<<inner>>
for k in 2..i-1 loop
if (mod(i, k) = 0) THEN
DBMS_OUTPUT.PUT_LINE(i);
exit inner when (mod(i, k)= 0);
end if;
end loop inner
end loop outer
end;
Run Code Online (Sandbox Code Playgroud)