Is this a correct way of declaring an array [closed]










-4















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];










share|improve this question













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
If this question can be reworded to fit the rules in the help center, please edit the question.











  • 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, 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





    @bashtati "Why is void main () wrong ?" Because the c++ standard demands main() to return an int.

    – πάντα ῥεῖ
    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















-4















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];










share|improve this question













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
If this question can be reworded to fit the rules in the help center, please edit the question.











  • 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, 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





    @bashtati "Why is void main () wrong ?" Because the c++ standard demands main() to return an int.

    – πάντα ῥεῖ
    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













-4












-4








-4








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];










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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
If this question can be reworded to fit the rules in the help center, please edit the question.







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
If this question can be reworded to fit the rules in the help center, please edit the question.







  • 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, 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





    @bashtati "Why is void main () wrong ?" Because the c++ standard demands main() to return an int.

    – πάντα ῥεῖ
    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





    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, 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





    @bashtati "Why is void main () wrong ?" Because the c++ standard demands main() to return an int.

    – πάντα ῥεῖ
    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












2 Answers
2






active

oldest

votes


















7














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];







share|improve this answer
































    1














    Your array declaration is simply missing a semicolon.



    Use int K[L]; instead of int K[L].






    share|improve this answer





























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      7














      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];







      share|improve this answer





























        7














        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];







        share|improve this answer



























          7












          7








          7







          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];







          share|improve this answer















          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];








          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 14 '18 at 21:05

























          answered Nov 14 '18 at 20:24









          Ted LyngmoTed Lyngmo

          3,0082518




          3,0082518























              1














              Your array declaration is simply missing a semicolon.



              Use int K[L]; instead of int K[L].






              share|improve this answer



























                1














                Your array declaration is simply missing a semicolon.



                Use int K[L]; instead of int K[L].






                share|improve this answer

























                  1












                  1








                  1







                  Your array declaration is simply missing a semicolon.



                  Use int K[L]; instead of int K[L].






                  share|improve this answer













                  Your array declaration is simply missing a semicolon.



                  Use int K[L]; instead of int K[L].







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 14 '18 at 20:29









                  PageFaultPageFault

                  111




                  111













                      這個網誌中的熱門文章

                      Barbados

                      How to read a connectionString WITH PROVIDER in .NET Core?

                      Node.js Script on GitHub Pages or Amazon S3