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;
}
c++ arrays string for-loop
|
show 16 more comments
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;
}
c++ arrays string for-loop
2
string strgs1[a-1]Not to mention, that VLAs are non-standard C++, the last index your loops try to access isa-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]hasa-1elements numbered0througha-2. The loop attempts to access an element at indexa-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
|
show 16 more comments
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;
}
c++ arrays string for-loop
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
c++ arrays string for-loop
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 isa-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]hasa-1elements numbered0througha-2. The loop attempts to access an element at indexa-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
|
show 16 more comments
2
string strgs1[a-1]Not to mention, that VLAs are non-standard C++, the last index your loops try to access isa-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]hasa-1elements numbered0througha-2. The loop attempts to access an element at indexa-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
|
show 16 more comments
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).
"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
add a comment |
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.
add a comment |
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).
"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
add a comment |
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).
"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
add a comment |
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).
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).
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
add a comment |
"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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 11 at 15:21
Fabio
823318
823318
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
2
string strgs1[a-1]Not to mention, that VLAs are non-standard C++, the last index your loops try to access isa-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]hasa-1elements numbered0througha-2. The loop attempts to access an element at indexa-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