小编use*_*141的帖子

C++程序计算最大公约数

我已经启动了这个程序来计算最大的公约数.这是我到目前为止:

#include <iostream>
#include <math.h>
using namespace std;
int getGCD(int a, int b)
{
    a = a % b;
    if (a == 0)
    {
        return b;
        b = b % a;
    }
    if (b == 0)
    {
        return a;
    }
}
int main()

{
    int x, y;
    cout << "Please enter two integers x and y, for GCD calculation" << endl;
    cin >> x >> y;
    cout << "The GCD of " << x << "and " << y << …
Run Code Online (Sandbox Code Playgroud)

c++ function greatest-common-divisor

6
推荐指数
1
解决办法
4万
查看次数

为什么我的函数没有执行i ++?

我正在一个面向对象程序的类中编写一个get函数,但是i++我的代码中没有执行某些原因.

这是我的.cpp文件中使用的内容:

char MyString::Get(int i)
{
  if( i = '\0')
  {
    exit(1);
  }
  else
  {
    return String[i];
  }
}
Run Code Online (Sandbox Code Playgroud)

这是main.cpp文件中调用的内容:

for(int i=0; i < String1.Length()+1; i++) 
{
  cout<< String1.Get(i)<<" ";
}

cout << endl;
Run Code Online (Sandbox Code Playgroud)

这是.cpp文件中的length方法,供参考:

int MyString::Length()
{
  int counter(0);

  while(String[counter] != '\0')
  {
    counter ++;
  }

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

另外:String1 = Jello World

输出:

JJJJJJJJJJJJJ

c++ string for-loop object

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