Is this a correct way of declaring an array [closed]
I have to make an array of whole numbers K(L) (L<=30) in c++,and i need some help declarating it correctly ,because im certain my way is wrong.
#include <iostream>
using namespace std;
const int L = 30;
void main ()
int K[L]
int br =0;
int i;
for (i = 0; i < L; i++)
cout <<endl << "Enter a number" ;
cin >> K[i];
c++ arrays
closed as off-topic by Neil Butterworth, πάντα ῥεῖ, paddy, juanchopanza, llllllllll Nov 14 '18 at 23:22
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – paddy, juanchopanza
|
show 6 more comments
I have to make an array of whole numbers K(L) (L<=30) in c++,and i need some help declarating it correctly ,because im certain my way is wrong.
#include <iostream>
using namespace std;
const int L = 30;
void main ()
int K[L]
int br =0;
int i;
for (i = 0; i < L; i++)
cout <<endl << "Enter a number" ;
cin >> K[i];
c++ arrays
closed as off-topic by Neil Butterworth, πάντα ῥεῖ, paddy, juanchopanza, llllllllll Nov 14 '18 at 23:22
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – paddy, juanchopanza
3
Why are you certain that your way is wrong?
– eerorika
Nov 14 '18 at 20:16
1
Using an array is normally not what you want to do,void main()
is wrong, but your array definition is perfectly legal C++.
– Neil Butterworth
Nov 14 '18 at 20:17
2
You array declaration is fine. However, thecin >> K[i];
statement will be run only once, withi=L
, which will go past the end of the array and cause undefined behavior. Perhaps you meant to wrap thecout
/cin
statements in bracesto include them in the body of the
for
-loop?
– alter igel
Nov 14 '18 at 20:17
1
@bashtati "Why is void main () wrong ?" Because the c++ standard demandsmain()
to return anint
.
– πάντα ῥεῖ
Nov 14 '18 at 20:23
1
@Jim It's blatantly wrong. That the MSVC compiler still supports that is completely irrelevant.
– πάντα ῥεῖ
Nov 14 '18 at 20:25
|
show 6 more comments
I have to make an array of whole numbers K(L) (L<=30) in c++,and i need some help declarating it correctly ,because im certain my way is wrong.
#include <iostream>
using namespace std;
const int L = 30;
void main ()
int K[L]
int br =0;
int i;
for (i = 0; i < L; i++)
cout <<endl << "Enter a number" ;
cin >> K[i];
c++ arrays
I have to make an array of whole numbers K(L) (L<=30) in c++,and i need some help declarating it correctly ,because im certain my way is wrong.
#include <iostream>
using namespace std;
const int L = 30;
void main ()
int K[L]
int br =0;
int i;
for (i = 0; i < L; i++)
cout <<endl << "Enter a number" ;
cin >> K[i];
c++ arrays
c++ arrays
asked Nov 14 '18 at 20:13
bashtatibashtati
1
1
closed as off-topic by Neil Butterworth, πάντα ῥεῖ, paddy, juanchopanza, llllllllll Nov 14 '18 at 23:22
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – paddy, juanchopanza
closed as off-topic by Neil Butterworth, πάντα ῥεῖ, paddy, juanchopanza, llllllllll Nov 14 '18 at 23:22
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – paddy, juanchopanza
3
Why are you certain that your way is wrong?
– eerorika
Nov 14 '18 at 20:16
1
Using an array is normally not what you want to do,void main()
is wrong, but your array definition is perfectly legal C++.
– Neil Butterworth
Nov 14 '18 at 20:17
2
You array declaration is fine. However, thecin >> K[i];
statement will be run only once, withi=L
, which will go past the end of the array and cause undefined behavior. Perhaps you meant to wrap thecout
/cin
statements in bracesto include them in the body of the
for
-loop?
– alter igel
Nov 14 '18 at 20:17
1
@bashtati "Why is void main () wrong ?" Because the c++ standard demandsmain()
to return anint
.
– πάντα ῥεῖ
Nov 14 '18 at 20:23
1
@Jim It's blatantly wrong. That the MSVC compiler still supports that is completely irrelevant.
– πάντα ῥεῖ
Nov 14 '18 at 20:25
|
show 6 more comments
3
Why are you certain that your way is wrong?
– eerorika
Nov 14 '18 at 20:16
1
Using an array is normally not what you want to do,void main()
is wrong, but your array definition is perfectly legal C++.
– Neil Butterworth
Nov 14 '18 at 20:17
2
You array declaration is fine. However, thecin >> K[i];
statement will be run only once, withi=L
, which will go past the end of the array and cause undefined behavior. Perhaps you meant to wrap thecout
/cin
statements in bracesto include them in the body of the
for
-loop?
– alter igel
Nov 14 '18 at 20:17
1
@bashtati "Why is void main () wrong ?" Because the c++ standard demandsmain()
to return anint
.
– πάντα ῥεῖ
Nov 14 '18 at 20:23
1
@Jim It's blatantly wrong. That the MSVC compiler still supports that is completely irrelevant.
– πάντα ῥεῖ
Nov 14 '18 at 20:25
3
3
Why are you certain that your way is wrong?
– eerorika
Nov 14 '18 at 20:16
Why are you certain that your way is wrong?
– eerorika
Nov 14 '18 at 20:16
1
1
Using an array is normally not what you want to do,
void main()
is wrong, but your array definition is perfectly legal C++.– Neil Butterworth
Nov 14 '18 at 20:17
Using an array is normally not what you want to do,
void main()
is wrong, but your array definition is perfectly legal C++.– Neil Butterworth
Nov 14 '18 at 20:17
2
2
You array declaration is fine. However, the
cin >> K[i];
statement will be run only once, with i=L
, which will go past the end of the array and cause undefined behavior. Perhaps you meant to wrap the cout
/cin
statements in braces
to include them in the body of the for
-loop?– alter igel
Nov 14 '18 at 20:17
You array declaration is fine. However, the
cin >> K[i];
statement will be run only once, with i=L
, which will go past the end of the array and cause undefined behavior. Perhaps you meant to wrap the cout
/cin
statements in braces
to include them in the body of the for
-loop?– alter igel
Nov 14 '18 at 20:17
1
1
@bashtati "Why is void main () wrong ?" Because the c++ standard demands
main()
to return an int
.– πάντα ῥεῖ
Nov 14 '18 at 20:23
@bashtati "Why is void main () wrong ?" Because the c++ standard demands
main()
to return an int
.– πάντα ῥεῖ
Nov 14 '18 at 20:23
1
1
@Jim It's blatantly wrong. That the MSVC compiler still supports that is completely irrelevant.
– πάντα ῥεῖ
Nov 14 '18 at 20:25
@Jim It's blatantly wrong. That the MSVC compiler still supports that is completely irrelevant.
– πάντα ῥεῖ
Nov 14 '18 at 20:25
|
show 6 more comments
2 Answers
2
active
oldest
votes
The first error the compiler gives is pretty self-explanatory:
main.cpp:4:16: error: ‘::main’ must return ‘int’
void main ()
^
The next two are trickier. They are caused by the forgotten ;
on the int K[L]
line.
main.cpp: In function ‘int main()’:
main.cpp:7:8: error: expected initializer before ‘int’
int br =0;
^~~
main.cpp:11:15: error: ‘K’ was not declared in this scope
cin >> K[i];
The forgotten ;
makes the compiler read the line as int K[L] int br =0;
so the declaration of both K
and br
fails, and it's the failed declaration of K
that causes the third error.
I corrected it and removed an oddly placed std::endl.
#include <iostream>
const int L = 30;
int main()
int K[L];
// int br =0; // unused
int i;
for (i = 0; i < L; i++)
std::cout << "Enter a number: ";
std::cin >> K[i];
add a comment |
Your array declaration is simply missing a semicolon.
Use int K[L];
instead of int K[L]
.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
The first error the compiler gives is pretty self-explanatory:
main.cpp:4:16: error: ‘::main’ must return ‘int’
void main ()
^
The next two are trickier. They are caused by the forgotten ;
on the int K[L]
line.
main.cpp: In function ‘int main()’:
main.cpp:7:8: error: expected initializer before ‘int’
int br =0;
^~~
main.cpp:11:15: error: ‘K’ was not declared in this scope
cin >> K[i];
The forgotten ;
makes the compiler read the line as int K[L] int br =0;
so the declaration of both K
and br
fails, and it's the failed declaration of K
that causes the third error.
I corrected it and removed an oddly placed std::endl.
#include <iostream>
const int L = 30;
int main()
int K[L];
// int br =0; // unused
int i;
for (i = 0; i < L; i++)
std::cout << "Enter a number: ";
std::cin >> K[i];
add a comment |
The first error the compiler gives is pretty self-explanatory:
main.cpp:4:16: error: ‘::main’ must return ‘int’
void main ()
^
The next two are trickier. They are caused by the forgotten ;
on the int K[L]
line.
main.cpp: In function ‘int main()’:
main.cpp:7:8: error: expected initializer before ‘int’
int br =0;
^~~
main.cpp:11:15: error: ‘K’ was not declared in this scope
cin >> K[i];
The forgotten ;
makes the compiler read the line as int K[L] int br =0;
so the declaration of both K
and br
fails, and it's the failed declaration of K
that causes the third error.
I corrected it and removed an oddly placed std::endl.
#include <iostream>
const int L = 30;
int main()
int K[L];
// int br =0; // unused
int i;
for (i = 0; i < L; i++)
std::cout << "Enter a number: ";
std::cin >> K[i];
add a comment |
The first error the compiler gives is pretty self-explanatory:
main.cpp:4:16: error: ‘::main’ must return ‘int’
void main ()
^
The next two are trickier. They are caused by the forgotten ;
on the int K[L]
line.
main.cpp: In function ‘int main()’:
main.cpp:7:8: error: expected initializer before ‘int’
int br =0;
^~~
main.cpp:11:15: error: ‘K’ was not declared in this scope
cin >> K[i];
The forgotten ;
makes the compiler read the line as int K[L] int br =0;
so the declaration of both K
and br
fails, and it's the failed declaration of K
that causes the third error.
I corrected it and removed an oddly placed std::endl.
#include <iostream>
const int L = 30;
int main()
int K[L];
// int br =0; // unused
int i;
for (i = 0; i < L; i++)
std::cout << "Enter a number: ";
std::cin >> K[i];
The first error the compiler gives is pretty self-explanatory:
main.cpp:4:16: error: ‘::main’ must return ‘int’
void main ()
^
The next two are trickier. They are caused by the forgotten ;
on the int K[L]
line.
main.cpp: In function ‘int main()’:
main.cpp:7:8: error: expected initializer before ‘int’
int br =0;
^~~
main.cpp:11:15: error: ‘K’ was not declared in this scope
cin >> K[i];
The forgotten ;
makes the compiler read the line as int K[L] int br =0;
so the declaration of both K
and br
fails, and it's the failed declaration of K
that causes the third error.
I corrected it and removed an oddly placed std::endl.
#include <iostream>
const int L = 30;
int main()
int K[L];
// int br =0; // unused
int i;
for (i = 0; i < L; i++)
std::cout << "Enter a number: ";
std::cin >> K[i];
edited Nov 14 '18 at 21:05
answered Nov 14 '18 at 20:24
Ted LyngmoTed Lyngmo
3,0082518
3,0082518
add a comment |
add a comment |
Your array declaration is simply missing a semicolon.
Use int K[L];
instead of int K[L]
.
add a comment |
Your array declaration is simply missing a semicolon.
Use int K[L];
instead of int K[L]
.
add a comment |
Your array declaration is simply missing a semicolon.
Use int K[L];
instead of int K[L]
.
Your array declaration is simply missing a semicolon.
Use int K[L];
instead of int K[L]
.
answered Nov 14 '18 at 20:29
PageFaultPageFault
111
111
add a comment |
add a comment |
3
Why are you certain that your way is wrong?
– eerorika
Nov 14 '18 at 20:16
1
Using an array is normally not what you want to do,
void main()
is wrong, but your array definition is perfectly legal C++.– Neil Butterworth
Nov 14 '18 at 20:17
2
You array declaration is fine. However, the
cin >> K[i];
statement will be run only once, withi=L
, which will go past the end of the array and cause undefined behavior. Perhaps you meant to wrap thecout
/cin
statements in bracesto include them in the body of the
for
-loop?– alter igel
Nov 14 '18 at 20:17
1
@bashtati "Why is void main () wrong ?" Because the c++ standard demands
main()
to return anint
.– πάντα ῥεῖ
Nov 14 '18 at 20:23
1
@Jim It's blatantly wrong. That the MSVC compiler still supports that is completely irrelevant.
– πάντα ῥεῖ
Nov 14 '18 at 20:25