[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 304
  • Last Modified:

run time failure check#2: 2d array and struct

Hi, I am trying to input words form a text file into 2 different 2 dimensional arrays.
We have to use array of char and struct class.

The first problem is that a.french[0] is empty, and it should contain the word "livre".
and then there is a run-time failure check.

I don't understand where the problem is.
Any help would be greatly appreciated.



#include<iostream>
#include<fstream>
#include<string>

using namespace std;

struct Words{
	char english[6][5];
	char french[6][7];
};


int main(){

	Words a;
//initialize arrays
	ifstream f;
	f.open("words.txt");
	string string1,string2;
	
	f>>string1>>string2;
	for(int i =0;i<7;++i)
	{f>>a.english[i]>>a.french[i];
	}
	f.close();

	for(int i=0;i<7;++i)
	{cout<<a.english[i]<<'\t'<<a.french[i]<<endl;}

	cout<<a.french[0]<<endl;//comes out empty.why?????


return 0;
}

Open in new window

words.txt
0
pgmerLA
Asked:
pgmerLA
  • 4
  • 2
1 Solution
 
phoffricCommented:
couple of problems
1) your text file header has 3 words in the first line (but you only read in two strings) so you are immediately out of sync.
2) your text file has 6 pairs of english/french words, but you have:
    for(int i=0;i<7;++i)
resulting in some clobbering of words
3) your char array doesn't give enough space for all of the words, so you end up clobbering words

I think you should be using string arrays, rather than char arrays in your struct
0
 
pgmerLAAuthor Commented:
Our teacher wants us to use array of character.
I fixed 1) and for 3) when I put char english[7][5]; instead of char english[6][5], it works, but I don't understand why since I only have 6 words and not 7. why do I need to create an array of 7 then?
and 2) what would you suggest I put for the boundaries?
0
 
phoffricCommented:
>>  for 3) when I put char english[7][5]; instead of char english[6][5], it works,
If it works, it is purely by accident, I would bet.
struct Words{  
        char english[6][5];  
        char french[6][7];  
}; 

Open in new window


The first index, 6, means that you can have 6 words. Read it as english is an array of 6 elements, where each element is a c-style string (requiring a terminating null byte).

For english, the 2nd index 5. Read it as char [5] meaning that each word can have an allocated memory region of 5 char. So, in this case, the length of the english word is 4 (since the extra char is the terminating null byte). But one of your english words is PAPER, which requires 6 chars.

>> and 2) what would you suggest I put for the boundaries?
If you have an array of N elements, then the index is from 0 to N-1.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
pgmerLAAuthor Commented:
GOT IT!
Thanks a lot.
0
 
phoffricCommented:
Great! Glad to have helped.
0
 
GkCommented:
Its very simple .Your allocation of  french array comes immediate after array of english.
So when you fill up to 6th. its fine. When you execute 7th of english it overlaps the pointer to the space allocated for french, because its the immediate location. So the empty is allocated to english[7] which is eaqual to french[0], and french[0] gets empty!!!
If you couldnt get it, debug the memory pointers.

f>>a.english[7];    causing a.french[0] to empty.




words.txt
abcd 1234
efgh 5678
ijklmn  91011121314
opq 151617

=========================

struct Words{
      char english[100][100];
      char french[100][100];
};

int main()
{

      Words a;
      ifstream f;
      f.open("words.txt");

      int iCount=0;
      while(!f.eof())
      {
            f>>a.english[iCount];
            f>>a.french[iCount];
            iCount++;
      }
      f.close();

      for(int i1=0;i1<iCount;++i1)
      {
            cout<<a.english[i1]<<"\t=\t"<<a.french[i1]<<endl;
      }

      return 0;
}
0
 
phoffricCommented:
A more advanced solution would be to pre-scan the file to know how many entries there are, and then dynamically allocate enough pointers to handle all the entries. And while you are pre-scanning you could find the max length of the english and french words, and use that to dynamically allocate space for the 2nd dimension.

By doing this, you do not have to worry about whether your program will be clobbering words since you will dynamically allocate just what you need for a given input file.

And there are other variations of this that do not require a pre-scan. If you are interested in pursuing this dynamic allocation, then I'll be back tomorrow night, and will look for a new question from you.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now