小编Sag*_*tic的帖子

如何在C++中使用参考参数?

我试图了解如何使用参考参数.我的文本中有几个例子,但是它们太复杂了我无法理解为什么以及如何使用它们.

你想如何以及为什么要使用参考?如果您没有将参数作为参考,而是将其&关闭,会发生什么?

例如,这些功能之间的区别是什么:

int doSomething(int& a, int& b);
int doSomething(int a, int b);
Run Code Online (Sandbox Code Playgroud)

我知道引用变量用于更改形式 - >引用,然后允许参数的双向交换.然而,这是我的知识范围,更具体的例子会有很大帮助.

c++ reference-parameters

51
推荐指数
1
解决办法
8万
查看次数

我刚刚学习了C++函数; 我可以在函数返回值上使用if语句吗?

我感到困惑的是关于isNumPalindrome()函数.它返回一个布尔值true或false.我怎么想使用它,所以我可以显示它是否是回文.对于前者if (isNumPalindrome == true) cout << "Your number is a palindrome"; else cout << "your number is not a palindrome.";

#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{
 return 0;
}

#include <iostream>
#include <cmath>

using namespace std;

int askNumber();
bool isNumPalindrome();

int num, pwr;

int main()
{
 askNumber();

 return 0;
}

bool isNumPalindrome()
{
 int pwr = 0;

 if (num < 10)
  return true;
 else
 {
  while (num / static_cast<int>(pow(10.0, pwr)) >=10)
   pwr++;
  while (num >=10)
  {
   int …
Run Code Online (Sandbox Code Playgroud)

c++ function

7
推荐指数
2
解决办法
2万
查看次数

如何在C++中使用枚举类型?

我不明白如何使用枚举类型.我明白它们是什么,但我不太明白它们的目的.

我制作了一个输入三角形三边的程序,输出它们是等腰,斜角或等边.我想在某个地方加入枚举类型,但是不知道在哪里以及如何使用它们.任何帮助,将不胜感激.

#include <iostream>

using namespace std;

enum triangleType {scalene, isosceles, equilateral, noTriangle};

triangleType triangleShape(double x, double y, double z);
void printTriangleShape(triangleType shape);

int main()
{
    double x, y, z;
    triangleType scalene, isosceles, equilateral, noTriangle;

    cout << "Please enter the three sides of a triangle:" << endl;
    cout << "Enter side 1: ";
    cin >> x;
    cout << endl;
    cout << "Enter side 2: ";
    cin >> y;
    cout << endl;
    cout << "Enter side 3: ";
    cin >> z; …
Run Code Online (Sandbox Code Playgroud)

c++ enums

2
推荐指数
1
解决办法
3760
查看次数

C++完美数字.需要一些帮助修改

我需要一些帮助修改这个.它只保持显示0作为临时值.谢谢.

// A program to determine whether the input number is a perfect number
// A perfect number is defined by the sum of all its positive divisors excluding itself
// 28: 1+2+3+7+14 = 28. 

int perfect, limit, divisor;

cout << "Please enter a positive integer in order to define whether it is a perfect integer or not: " ;
 cin >> perfect;
 cout << endl;

 int temp = 0;
 int prevtemp = 0;
  limit = 1;
  divisor = 1; …
Run Code Online (Sandbox Code Playgroud)

c++ numbers perfect-numbers

1
推荐指数
1
解决办法
3420
查看次数

C++关于pow功能的问题

我试图让这个表达起作用,我很确定它不是括号因为我计算了所有这些.也许我有一些涉及参数pow(x,y)的错误.

double calculatePeriodicPayment()
{
 periodicPaymentcalc = (loan * ((interestRate / yearlyPayment)))  / (1-((pow ((1+(interestRate / yearlyPayment)))),(-(yearlyPayment * numOfYearLoan))));

 return periodicPaymentcalc;
}
Run Code Online (Sandbox Code Playgroud)

c++ exponent

1
推荐指数
1
解决办法
1656
查看次数