program ends after for loop in c++











up vote
1
down vote

favorite












why does this program run only till the first for loop and then stops?



It doesn't run the second for loop and also skips system("pause"). Can anyone explain what is wrong in my code?
I want to make two arrays of strings: strgs1 and strgs2 of length a and b, and then take the input from the user for each element of the arrays.
This is my code:



#include <cstdio>
#include <iostream>
#include <string>
using namespace std;

int main(){
int a,b;
cin>>a>>b;
string strgs1[a-1], strgs2[b-1];

for(int i = 0;i < a;i++){
cin>>strgs1[i];
}
for(int j = 0;j < b;j++){
cin>>strgs2[j];
}
system("pause");
return 0;
}









share|improve this question




















  • 2




    string strgs1[a-1] Not to mention, that VLAs are non-standard C++, the last index your loops try to access is a-1, which is out of bounds of the array.
    – Algirdas Preidžius
    Nov 11 at 15:10






  • 2




    Your program exhibits undefined behavior, by way of accessing an index out of bounds. strgs1[a-1] has a-1 elements numbered 0 through a-2. The loop attempts to access an element at index a-1 - there's no such element.
    – Igor Tandetnik
    Nov 11 at 15:11












  • @AlgirdasPreidžius why is strgs[a-1] out of bounds?? the for loops condition is i < a so it will iterate till i = a-1.
    – firangi
    Nov 11 at 15:14








  • 1




    @firangi "my minGW compiler g++ gives no error for this program" - Compilers are not required to emit warnings or errors for undefined behaviour (or many other forms of broken programs). You cannot rely on "it compiles" as a test for "valid program".
    – Jesper Juhl
    Nov 11 at 15:27








  • 1




    @firangi As some of us (including the answers) already stated: You are iterating out-of-bounds of your array. That is undefined behavior.
    – Algirdas Preidžius
    Nov 11 at 15:30

















up vote
1
down vote

favorite












why does this program run only till the first for loop and then stops?



It doesn't run the second for loop and also skips system("pause"). Can anyone explain what is wrong in my code?
I want to make two arrays of strings: strgs1 and strgs2 of length a and b, and then take the input from the user for each element of the arrays.
This is my code:



#include <cstdio>
#include <iostream>
#include <string>
using namespace std;

int main(){
int a,b;
cin>>a>>b;
string strgs1[a-1], strgs2[b-1];

for(int i = 0;i < a;i++){
cin>>strgs1[i];
}
for(int j = 0;j < b;j++){
cin>>strgs2[j];
}
system("pause");
return 0;
}









share|improve this question




















  • 2




    string strgs1[a-1] Not to mention, that VLAs are non-standard C++, the last index your loops try to access is a-1, which is out of bounds of the array.
    – Algirdas Preidžius
    Nov 11 at 15:10






  • 2




    Your program exhibits undefined behavior, by way of accessing an index out of bounds. strgs1[a-1] has a-1 elements numbered 0 through a-2. The loop attempts to access an element at index a-1 - there's no such element.
    – Igor Tandetnik
    Nov 11 at 15:11












  • @AlgirdasPreidžius why is strgs[a-1] out of bounds?? the for loops condition is i < a so it will iterate till i = a-1.
    – firangi
    Nov 11 at 15:14








  • 1




    @firangi "my minGW compiler g++ gives no error for this program" - Compilers are not required to emit warnings or errors for undefined behaviour (or many other forms of broken programs). You cannot rely on "it compiles" as a test for "valid program".
    – Jesper Juhl
    Nov 11 at 15:27








  • 1




    @firangi As some of us (including the answers) already stated: You are iterating out-of-bounds of your array. That is undefined behavior.
    – Algirdas Preidžius
    Nov 11 at 15:30















up vote
1
down vote

favorite









up vote
1
down vote

favorite











why does this program run only till the first for loop and then stops?



It doesn't run the second for loop and also skips system("pause"). Can anyone explain what is wrong in my code?
I want to make two arrays of strings: strgs1 and strgs2 of length a and b, and then take the input from the user for each element of the arrays.
This is my code:



#include <cstdio>
#include <iostream>
#include <string>
using namespace std;

int main(){
int a,b;
cin>>a>>b;
string strgs1[a-1], strgs2[b-1];

for(int i = 0;i < a;i++){
cin>>strgs1[i];
}
for(int j = 0;j < b;j++){
cin>>strgs2[j];
}
system("pause");
return 0;
}









share|improve this question















why does this program run only till the first for loop and then stops?



It doesn't run the second for loop and also skips system("pause"). Can anyone explain what is wrong in my code?
I want to make two arrays of strings: strgs1 and strgs2 of length a and b, and then take the input from the user for each element of the arrays.
This is my code:



#include <cstdio>
#include <iostream>
#include <string>
using namespace std;

int main(){
int a,b;
cin>>a>>b;
string strgs1[a-1], strgs2[b-1];

for(int i = 0;i < a;i++){
cin>>strgs1[i];
}
for(int j = 0;j < b;j++){
cin>>strgs2[j];
}
system("pause");
return 0;
}






c++ arrays string for-loop






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 15:10

























asked Nov 11 at 15:07









firangi

144




144








  • 2




    string strgs1[a-1] Not to mention, that VLAs are non-standard C++, the last index your loops try to access is a-1, which is out of bounds of the array.
    – Algirdas Preidžius
    Nov 11 at 15:10






  • 2




    Your program exhibits undefined behavior, by way of accessing an index out of bounds. strgs1[a-1] has a-1 elements numbered 0 through a-2. The loop attempts to access an element at index a-1 - there's no such element.
    – Igor Tandetnik
    Nov 11 at 15:11












  • @AlgirdasPreidžius why is strgs[a-1] out of bounds?? the for loops condition is i < a so it will iterate till i = a-1.
    – firangi
    Nov 11 at 15:14








  • 1




    @firangi "my minGW compiler g++ gives no error for this program" - Compilers are not required to emit warnings or errors for undefined behaviour (or many other forms of broken programs). You cannot rely on "it compiles" as a test for "valid program".
    – Jesper Juhl
    Nov 11 at 15:27








  • 1




    @firangi As some of us (including the answers) already stated: You are iterating out-of-bounds of your array. That is undefined behavior.
    – Algirdas Preidžius
    Nov 11 at 15:30
















  • 2




    string strgs1[a-1] Not to mention, that VLAs are non-standard C++, the last index your loops try to access is a-1, which is out of bounds of the array.
    – Algirdas Preidžius
    Nov 11 at 15:10






  • 2




    Your program exhibits undefined behavior, by way of accessing an index out of bounds. strgs1[a-1] has a-1 elements numbered 0 through a-2. The loop attempts to access an element at index a-1 - there's no such element.
    – Igor Tandetnik
    Nov 11 at 15:11












  • @AlgirdasPreidžius why is strgs[a-1] out of bounds?? the for loops condition is i < a so it will iterate till i = a-1.
    – firangi
    Nov 11 at 15:14








  • 1




    @firangi "my minGW compiler g++ gives no error for this program" - Compilers are not required to emit warnings or errors for undefined behaviour (or many other forms of broken programs). You cannot rely on "it compiles" as a test for "valid program".
    – Jesper Juhl
    Nov 11 at 15:27








  • 1




    @firangi As some of us (including the answers) already stated: You are iterating out-of-bounds of your array. That is undefined behavior.
    – Algirdas Preidžius
    Nov 11 at 15:30










2




2




string strgs1[a-1] Not to mention, that VLAs are non-standard C++, the last index your loops try to access is a-1, which is out of bounds of the array.
– Algirdas Preidžius
Nov 11 at 15:10




string strgs1[a-1] Not to mention, that VLAs are non-standard C++, the last index your loops try to access is a-1, which is out of bounds of the array.
– Algirdas Preidžius
Nov 11 at 15:10




2




2




Your program exhibits undefined behavior, by way of accessing an index out of bounds. strgs1[a-1] has a-1 elements numbered 0 through a-2. The loop attempts to access an element at index a-1 - there's no such element.
– Igor Tandetnik
Nov 11 at 15:11






Your program exhibits undefined behavior, by way of accessing an index out of bounds. strgs1[a-1] has a-1 elements numbered 0 through a-2. The loop attempts to access an element at index a-1 - there's no such element.
– Igor Tandetnik
Nov 11 at 15:11














@AlgirdasPreidžius why is strgs[a-1] out of bounds?? the for loops condition is i < a so it will iterate till i = a-1.
– firangi
Nov 11 at 15:14






@AlgirdasPreidžius why is strgs[a-1] out of bounds?? the for loops condition is i < a so it will iterate till i = a-1.
– firangi
Nov 11 at 15:14






1




1




@firangi "my minGW compiler g++ gives no error for this program" - Compilers are not required to emit warnings or errors for undefined behaviour (or many other forms of broken programs). You cannot rely on "it compiles" as a test for "valid program".
– Jesper Juhl
Nov 11 at 15:27






@firangi "my minGW compiler g++ gives no error for this program" - Compilers are not required to emit warnings or errors for undefined behaviour (or many other forms of broken programs). You cannot rely on "it compiles" as a test for "valid program".
– Jesper Juhl
Nov 11 at 15:27






1




1




@firangi As some of us (including the answers) already stated: You are iterating out-of-bounds of your array. That is undefined behavior.
– Algirdas Preidžius
Nov 11 at 15:30






@firangi As some of us (including the answers) already stated: You are iterating out-of-bounds of your array. That is undefined behavior.
– Algirdas Preidžius
Nov 11 at 15:30














2 Answers
2






active

oldest

votes

















up vote
2
down vote



accepted










First point, you are using static arrays string strgs1[a-1], strgs2[b-1]; with sizes non-constant at compile time. That's a bad idea. I would advice to use std::vector instead.



Second point, the sizes that you are using for your arrays are not good. For example, the size of your first array is a-1 and you try to insert a strings inside it (from 0 to a-1).






share|improve this answer





















  • "That's a bad idea" - not just a bad idea. Such arrays are not valid C++ at all. Some compilers implement VLAs as an extension, but it's not valid according to the standard and will break on other compilers.
    – Jesper Juhl
    Nov 11 at 15:22










  • I thought array size also starts from 0 that's why I wrote strgs1[a-1]. Now the code is working thanks! I don't know std::vector I will learn it. now the question seems very stupid..lol
    – firangi
    Nov 11 at 16:45




















up vote
0
down vote













Suppose you enter a=3. The size of the array is a-1=2. The loop iterates i=0, i=1, i=2. But this is 3 elements, while your vector has only size 2!



Furthermore, use std::vector. Arrays with non constant size are allowed by some compilers, but it is not portable.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250041%2fprogram-ends-after-for-loop-in-c%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote



    accepted










    First point, you are using static arrays string strgs1[a-1], strgs2[b-1]; with sizes non-constant at compile time. That's a bad idea. I would advice to use std::vector instead.



    Second point, the sizes that you are using for your arrays are not good. For example, the size of your first array is a-1 and you try to insert a strings inside it (from 0 to a-1).






    share|improve this answer





















    • "That's a bad idea" - not just a bad idea. Such arrays are not valid C++ at all. Some compilers implement VLAs as an extension, but it's not valid according to the standard and will break on other compilers.
      – Jesper Juhl
      Nov 11 at 15:22










    • I thought array size also starts from 0 that's why I wrote strgs1[a-1]. Now the code is working thanks! I don't know std::vector I will learn it. now the question seems very stupid..lol
      – firangi
      Nov 11 at 16:45

















    up vote
    2
    down vote



    accepted










    First point, you are using static arrays string strgs1[a-1], strgs2[b-1]; with sizes non-constant at compile time. That's a bad idea. I would advice to use std::vector instead.



    Second point, the sizes that you are using for your arrays are not good. For example, the size of your first array is a-1 and you try to insert a strings inside it (from 0 to a-1).






    share|improve this answer





















    • "That's a bad idea" - not just a bad idea. Such arrays are not valid C++ at all. Some compilers implement VLAs as an extension, but it's not valid according to the standard and will break on other compilers.
      – Jesper Juhl
      Nov 11 at 15:22










    • I thought array size also starts from 0 that's why I wrote strgs1[a-1]. Now the code is working thanks! I don't know std::vector I will learn it. now the question seems very stupid..lol
      – firangi
      Nov 11 at 16:45















    up vote
    2
    down vote



    accepted







    up vote
    2
    down vote



    accepted






    First point, you are using static arrays string strgs1[a-1], strgs2[b-1]; with sizes non-constant at compile time. That's a bad idea. I would advice to use std::vector instead.



    Second point, the sizes that you are using for your arrays are not good. For example, the size of your first array is a-1 and you try to insert a strings inside it (from 0 to a-1).






    share|improve this answer












    First point, you are using static arrays string strgs1[a-1], strgs2[b-1]; with sizes non-constant at compile time. That's a bad idea. I would advice to use std::vector instead.



    Second point, the sizes that you are using for your arrays are not good. For example, the size of your first array is a-1 and you try to insert a strings inside it (from 0 to a-1).







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 11 at 15:17









    Liumatt

    1344




    1344












    • "That's a bad idea" - not just a bad idea. Such arrays are not valid C++ at all. Some compilers implement VLAs as an extension, but it's not valid according to the standard and will break on other compilers.
      – Jesper Juhl
      Nov 11 at 15:22










    • I thought array size also starts from 0 that's why I wrote strgs1[a-1]. Now the code is working thanks! I don't know std::vector I will learn it. now the question seems very stupid..lol
      – firangi
      Nov 11 at 16:45




















    • "That's a bad idea" - not just a bad idea. Such arrays are not valid C++ at all. Some compilers implement VLAs as an extension, but it's not valid according to the standard and will break on other compilers.
      – Jesper Juhl
      Nov 11 at 15:22










    • I thought array size also starts from 0 that's why I wrote strgs1[a-1]. Now the code is working thanks! I don't know std::vector I will learn it. now the question seems very stupid..lol
      – firangi
      Nov 11 at 16:45


















    "That's a bad idea" - not just a bad idea. Such arrays are not valid C++ at all. Some compilers implement VLAs as an extension, but it's not valid according to the standard and will break on other compilers.
    – Jesper Juhl
    Nov 11 at 15:22




    "That's a bad idea" - not just a bad idea. Such arrays are not valid C++ at all. Some compilers implement VLAs as an extension, but it's not valid according to the standard and will break on other compilers.
    – Jesper Juhl
    Nov 11 at 15:22












    I thought array size also starts from 0 that's why I wrote strgs1[a-1]. Now the code is working thanks! I don't know std::vector I will learn it. now the question seems very stupid..lol
    – firangi
    Nov 11 at 16:45






    I thought array size also starts from 0 that's why I wrote strgs1[a-1]. Now the code is working thanks! I don't know std::vector I will learn it. now the question seems very stupid..lol
    – firangi
    Nov 11 at 16:45














    up vote
    0
    down vote













    Suppose you enter a=3. The size of the array is a-1=2. The loop iterates i=0, i=1, i=2. But this is 3 elements, while your vector has only size 2!



    Furthermore, use std::vector. Arrays with non constant size are allowed by some compilers, but it is not portable.






    share|improve this answer

























      up vote
      0
      down vote













      Suppose you enter a=3. The size of the array is a-1=2. The loop iterates i=0, i=1, i=2. But this is 3 elements, while your vector has only size 2!



      Furthermore, use std::vector. Arrays with non constant size are allowed by some compilers, but it is not portable.






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        Suppose you enter a=3. The size of the array is a-1=2. The loop iterates i=0, i=1, i=2. But this is 3 elements, while your vector has only size 2!



        Furthermore, use std::vector. Arrays with non constant size are allowed by some compilers, but it is not portable.






        share|improve this answer












        Suppose you enter a=3. The size of the array is a-1=2. The loop iterates i=0, i=1, i=2. But this is 3 elements, while your vector has only size 2!



        Furthermore, use std::vector. Arrays with non constant size are allowed by some compilers, but it is not portable.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 15:21









        Fabio

        823318




        823318






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250041%2fprogram-ends-after-for-loop-in-c%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            List item for chat from Array inside array React Native

            Thiostrepton

            Caerphilly