【发布时间】:2018-01-21 12:09:29
【问题描述】:
我是 C++ 新手,正在学习它。
这是我为问题 4 编写的代码,但它没有给出输出。
我有两个问题:
#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++。
-
但是当我更改此代码时:
#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。我检查了每一行但没有显示错误请说明为什么没有显示输出。
【问题讨论】:
-
你认为
answer=answer;会做什么? -
7+8+14;也不做任何事情,也不做它下面的任何行。我猜你打算写a += a2? -
以及你必须使用 std::cout
-
answer 打印所有数字的总和
-
知道了,但只需使用 using namespace std;或 std::cout 'cout' 未在范围内声明,但我仅使用 c++。