English 中文(简体)
没有语法错误, 但没有打印结果
原标题:No syntax error but not printing result
  • 时间:2012-05-28 03:12:19
  •  标签:
  • c++

I can t get the sum to print out I did not get any warnings or such.

Trying to do this question here:http://projecteuler.net/problem=1 the program runs and then just stalls forever.

// Adding multiples of this and following equations
#include "std_lib_cmpt125.h"

void SumMultiple(int Max,int Base)
{
    int i=0;
    int sum=0;
    for(i;i<Max;i+Base)
        {
            sum=i+sum;
        };
    cout<<"The sum"<<sum;
}
int main()
{
    int base=0;
    int max=0;
    int sum=0;
    cout<<"Please enter the sum s multiple: (ex. 3: 3,6,9,12...)
";
    cin>>base;
    cout<<"Please enter the sum s maximum: (ex. 10000)
";
    cin>>max;
    SumMultiple(max,base);
    return 0;
}
}
最佳回答

你错过了加注 i ,

for(i;i<Max;i=i+Base)
{
..
}

for(i;i<Max;i+=Base)
 {
  ..
 }
问题回答

更改 :

int i=0;
int sum=0;
for(i;i<Max;i+Base)
{
   sum=i+sum;
};

致:

int sum = 0;
for (int i = 0; i < Max; i += Base)
{
  sum += i;
}

请注意,原 < code> for 环中的第一个语句没有效果, < i 的值必须增加(最简单的是使用 运算符来完成),关闭括号后的分号是不必要的。

C++中的习惯做法是在循环中宣布循环变数,限制其范围并强调其作为反面的作用。

你有一个额外的Brace}在最后,你没有加薪i

更改

for(i;i<Max;i+Base)
        {
            sum=i+sum;
        };
    cout<<"The sum"<<sum;

for(i;i<Max;i=i+Base)
        {
            sum=i+sum;
        };
    cout<<"The sum"<<sum;

for(i;i<Max;i+=Base)
        {
            sum=i+sum;
        };
    cout<<"The sum"<<sum;

上述代码将加加计数i并显示必要结果

另外,您也不会用此函数获得答案。问题1是寻找 [1. 999] 中所有3或5倍数的和 [1. 999]

试这个

for ( int i = 1; i < Max ; i++ ) 
    sum += ((i % 3 && i % 5) ? 0 : i ; 
return sum




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?