为什么我得到错误的输出 2686924 而不是 281?

Why I am getting wrong output 2686924 instead of 281?

我是 C++ 的新手,正在学习它。

这是我为问题 4 编写的代码,但它没有给出输出。

我有两个问题:

  1. #include<iostream>
    #include<stdio.h>
    
    int main(){
    int a,b,c,d,e,f;
    int a2,b2,c2,d2,e2,f2;
    
    int answer;
    
    a=1;
    b=2;
    c=3;
    d=4;
    e=5;
    f=6;
    
    a2=11;
    b2=22;
    c2=33;
    d2=44;
    e2=55;
    f2=66;
    
    7+8+14;
    a+a2;
    b+b2;
    c+c2;
    d+d2;
    e+e2;
    f+f2;
    
    answer=answer;
    cout<<"Answer is"<<answer;
    }
    

它告诉我错误 'cout' 没有在范围内声明,但我只使用 C++。

  1. 但是当我更改此代码时:

    #include<iostream>
    #include<stdio.h>
    
    int main(){
    int a,b,c,d,e,f;
    int a2,b2,c2,d2,e2,f2;
    
    int answer;
    
    a=1;
    b=2;
    c=3;
    d=4;
    e=5;
    f=6;
    
    a2=11;
    b2=22;
    c2=33;
    d2=44;
    e2=55;
    f2=66;
    
    7+8+14;
    a+a2;
    b+b2;
    c+c2;
    d+d2;
    e+e2;
    f+f2;
    
    answer=answer;
    printf("Answer is:");
    printf("%d",answer);
    }
    

这给出了输出 2686924。输出是错误的,它应该打印 281。我检查了每一行但没有错误显示请告诉为什么输出没有显示。

你的代码中有很多语句实际上什么都不做,

7+8+14;
a+a2;
b+b2;
c+c2;
d+d2;
e+e2;
f+f2;

answer=answer;

你看,answer 未初始化。你永远不会在代码中设置它的值,所以你需要像

这样的东西
// Initialize `answer' here
answer = 7 + 8 + 14;
answer = answer + a + a2;
answer = answer + b + b2;
answer = answer + c + c2;
answer = answer + d + d2;
answer = answer + e + e2;
answer = answer + f + f2;

还有,你看懂这行文字了吗?你不能没有空格吧!代码也是如此。