0

Um, a situation?

#include <iostream> #include <string> using namespace std; int main() { string zohal [4]; string Gh [4]; for (int i = 0 ; i < 4 ; i++) cin >> Gh; for (int i = 0 ; i < 4 ; i++) for (int j =0 ; j < 4 ; j++) { zohal [i] [j] += Gh [i] [j] ; cout << zohal [i] [j] ; } system ("pause") ; } This is kinda a toy problem I wrote this just because I want to know why its wrong It gives this Error after I input aaaa bbbb cccc dddd String subscript out of range note that the output is supposed to be the same as input plz help

14th Dec 2019, 9:34 AM
Zohal
Zohal - avatar
7 Answers
+ 5
Here you declared one dimension Array of Gh and using as two dimensional Gh [i][j] and same for zohal. That Maybe the Problem. And also there is no loop for j Why? You are taking input 4 times and same time you are doing operation. This is also a mistake.
14th Dec 2019, 9:37 AM
A͢J
A͢J - avatar
+ 4
1) You have declared an 1D array but trying to use a style used for 2D values. 2) Taking user input for Gh is wrong. 3) You have never declared and initialized the variable "j" but you are using it. 4) You are missing curly braces. Edit: Kindly learn the basics well so that you don't make such mistakes again.
14th Dec 2019, 9:45 AM
Avinesh
Avinesh - avatar
+ 2
Could you please show how your output should look like for a given input.
14th Dec 2019, 12:32 PM
Avinesh
Avinesh - avatar
+ 2
Zohal So you just want to copy everything from Gh to zohal?
14th Dec 2019, 5:47 PM
Avinesh
Avinesh - avatar
0
Avinesh I meant to use j instead of i, I made a mistake here but I was using j in the main code I corrected it here too now Im using curly braces just fine U dont need to use them when u only have 1 command About user input for Gh sorry really I didnt do this in the main code but I wrote this in a hurry so... corrected that as well I want to know about arrays Ive been told in University that if u use a 1D string like a 2D, it will put the string's characters there, and Ive tried it before it worked just fine try this if u're still doubting it string zohal; cin >> zohal; cout zohal [0]; It will give u the first letter, It also works for 1D to 2D; So why an error?! Also this Error still happens if I use a normal string as a 1D
14th Dec 2019, 10:15 AM
Zohal
Zohal - avatar
0
Avinesh Input : aaaa bbbb cccc dddd Out put : aaaa bbbb cccc dddd
14th Dec 2019, 5:41 PM
Zohal
Zohal - avatar
0
Avinesh sorry for the late respose but yes, I wanted to know why this doesnt work, I think I found the answer When I put Gh [0] [0] into zohal [0] [0], it will take zohal as a 1 character string, so it wont accept any more characters
14th Dec 2019, 9:37 PM
Zohal
Zohal - avatar