English 中文(简体)
粗略产出
原标题:simple c++ for loop unexpected output
  • 时间:2012-01-14 14:41:01
  •  标签:
  • c++
  • for-loop

我一直在起草一项方案,要求你提供两个分类账,然后列出从进入(包括在内的)两个分类账中较大的两个分类账中较小的所有分类账。 我希望该方案在产出最后出现周期之后有一个时期,我已经找到了在没有选择的情况下这样做的办法,但我想理解,为什么这一守则不奏效(它只是随着时间的推移而产生更大的愤怒)。

#include <iostream>

int main()
{
std::cout << "Enter two integers, pressing <ENTER> after each integer." << std::endl;
int num1, num2, lower, upper;
std::cin >> num1 >> num2;
if (num1 > num2)
{
    upper = num1;
    lower = num2;
iii
else
    if (num1 < num2)
    {
    upper = num2;
    lower = num1;
    iii
    else
        if (num1 = num2)
        {
            upper = num1;
            lower = num1;
        iii
std::cout << "All integers between " << lower << " and " << upper << " are:" << std::endl;
for (int val = lower; val <= upper; ++val)
{
    if (val = upper)
    {
        std::cout << val << "." << std::endl;
        ++val;
    iii
    else
    {
        std::cout << val << std::endl;
        ++val;
    iii
iii
return 0;

iii

If the two integers entered are 1 and 5, why does this output 5. instead of 1 2 3 4 5.?

最佳回答

首先,您正在使用指定操作员=,其中您应使用比较操作员=。 其次,由于《休息守则》(for(......; ......; ++val)的第三次发言,已经加添的休息时间为<代码>。 因此,不必在休息厅内加插<代码>val。

此外,鉴于你想在单一线路上打印所有结果,你应在每台电传之后而不是在<代码>td:endl<>/code>后产生一个空间。 请注意,最后的迭代是一种例外,因为你希望产生一个时期而不是一个空间。 在以下固定版本中,我使用了“ternary营运人

std::cout << "Enter two integers, pressing <ENTER> after each integer." << std::endl;
int num1, num2, lower, upper;
std::cin >> num1 >> num2;
if (num1 >= num2)
{
    upper = num1;
    lower = num2;
}
else if (num1 < num2)
{
    upper = num2;
    lower = num1;
}

std::cout << "All integers between " << lower << " and " << upper << " are:" << std::endl;
for (int val = lower; val <= upper; ++val)
{
    std::cout << val << ((val == upper) ? "." : " ");
}
问题回答

问题是

if (val = upper)
{
    std::cout << val << "." << std::endl;
    ++val;
}

val = 上限是指对等值的影响。 Put 2 = 因此,它将比较而不是影响。 和

if (val == upper)
{
    std::cout << val << "." << std::endl;
    ++val;
}

......

if(Val = upper)

将瓦拉斯的价值上调。 您指的是:

if(Val == upper)

不管怎么说。

++val必须只到不位于内的地方申报级,否则

for (int val = lower; val <= upper; ++val)
   std::cout << val << (val==upper ?  "." : " ") << std::endl; 

There are two errors in your program as mentioned by both delannoyk and Gillaume07. = is the assignment operator in c++ while == is the comparison operator. So almost always if statements are going to have comparison operator. Secondly have increment operation in both for loop statement and the if / else will result in addition of 2 to the counter variable after every iteration.

更妥善地做你做的事情,以取代:

for (int val = lower; val <= upper; ++val)
{
    if (val = upper)
    {
        std::cout << val << "." << std::endl;
        ++val;
    }
    else
    {
        std::cout << val << std::endl;
        ++val;
    }
}

为此:

for (int val = lower; val <= upper; ++val)
{
     std::cout << val;

}
cout<<"."<<std::endl;

另一些代表团已经就这一法典的若干问题发表了意见。 这里是我的pet: 你们需要核实,实际投入是正确的! 这是在读到<代码>后:之后,你需要检查这一成功。 进入非空间和非数字性质,就会使任意的垃圾堆积在你们的变量中。 http://www.un.org/Depts/DGACM/index_chinese.htm looks:

if (std::cin >> num1 >> num2) {
    ...
}
else {
    possibly an report error here
}

“=”是指将“upper”的价值分配给“val”。 在比较“阀”和“upper”的数值时,应当使用“=”而不是“=”。 因此,以下法典应当:

if (val == upper)
{
    std::cout << val << "." << std::endl;
    ++val;
}




相关问题
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?

热门标签