这只是我作业的第一部分,我修复了所有其他编译错误,但我一直收到这个错误,有五个。
1>\takehome\main.cpp(39) : error C2065: 'j' : undeclared identifier
1>\takehome\main.cpp(44) : error C2065: 'j' : undeclared identifier
\takehome\main.cpp(45) : error C2065: 'j' : undeclared identifier
\takehome\main.cpp(76) : error C2065: 'j' : undeclared identifier
\takehome\main.cpp(80) : error C2065: 'j' : undeclared identifier
我已尝试用它做任何事情,但我可能做错了什么......显然我是。如果你不介意我可以使用一些帮助:)。顺便说一句,万一有人想知道,做简单的。
#include <iostream>
using namespace std;
int main()
{
int memory[100]; //Making it 100, since simpletron contains a 100 word mem.
int operation; //taking the rest of these variables straight out of the book seeing as how they were italisized.
int operand;
int accum = 0; // the special register is starting at 0
int position = 0; //making the starting position to be 0.
for ( int j = 0; j < 100; j++ ) //Simply stating that for int j is = to 0, j must be less than 100 because that is the memory limit, and for every pass-through, increment j.
memory[j] = 0;
// This is for part a, it will take in positive variables in a sent-controlled loop and compute + print their sum. These are random variables.
memory [0] = 2942;
memory [1] = 2342;
memory [2] = 3523;
memory [3] = 2031;
memory [4] = 5000;
memory [5] = 8080;
memory [6] = 3425;
j = 0; //Makes the variable j start at 0.
while ( true )
{
memory[ j ]%100 = operand; // Finds the op codes from the limit on the memory (100)
memory[ j ]%100 = operation;
//using a switch loop to set up the loops for the cases
switch ( operation ){
case 1: //reads a variable into a word from loc.
cout <<"\n Input a positive variable: ";
cin >> memory[ operand ]; break;
case 2: // takes a word from location
cout << "\n\nThe content at location " << operand << "is " << memory[operand]; break;
case 3:// loads
accum = memory[ operand ]; break;
case 4: //stores
memory[ operand ] = accum; break;
case 5: //adds
accum = accum + memory[ operand ]; break;
case 6: // subtracts
accum = accum - memory[ operand ]; break;
case 7: //divides
accum = accum / (memory[ operand ]); break;
case 8: // multiplies
accum = accum*memory [ operand ]; break;
case 9: // Branches to location
j = -1; break;
case 10: //branches if acc. is < 0
if (accum < 0)
j = 5; break;
case 11: //branches if acc = 0
if (accum == 0); break;
case 12: // Program ends
exit(0); break;
}
j++;
}
return 0;
}
答案 0 :(得分:18)
在“for”循环之外声明“j”。当你在里面声明循环标题时,它在循环块的本地,在它之外是不可见的。
答案 1 :(得分:11)
当您在for
语句中声明变量时,该变量仅在for
循环体的范围内,例如。
for ( int j = 0; j < 100; j++ )
{
// j is defined in here
}
// But j is undefined out here
答案 2 :(得分:3)
您设置的j = 0
未声明为int j = 0
。
你是在for
循环中完成的,但它的局部范围仅适用于循环体..
答案 3 :(得分:2)
当你有类似
的东西时for ( int i = 0; i < k; ++i )
{
// stuff
}
i
仅在for
循环的范围内可用,因此,如果您执行以下操作:
for ( int i = 0; i < k; ++i )
{
// stuff
}
cout << i;
您在cout << i;
收到编译错误,因为i
循环结束后for
不再存在。
答案 4 :(得分:2)
在C ++中,for循环中声明的变量的范围是循环。所以当你说:
for ( int j = 0; j < 100; j++ ) {
// j only exists here (and in the for statement itself)
}
变量j仅存在于循环体中。
答案 5 :(得分:1)
变量j是for循环的本地变量。您需要通过执行以下操作来扩大其范围:
int j;
for(j = 0; j < 100; ++j)
或稍后重新宣布:
for(int j=0; j<100; ++j)
...
...
int j = 0;
while(true)
...
答案 6 :(得分:1)
j
仅存在于for循环中,即在指令
for ( int j = 0; j < 100; j++ ) //Simply stating that for int j is = to 0, j must be less than 100 because that is the memory limit, and for every pass-through, increment j.
memory[j] = 0;
你应该写
int j;
for(j=0;j<100;j++)
...