combining 2 arrays

This is a discussion on combining 2 arrays within the C++ Programming forums, part of the General Programming Boards category; can anyone tell me why I am crashing when i run this ? I am loading and then trying to ...

  1. #1
    Registered User mouse163's Avatar
    Join Date
    Dec 2002
    Posts
    49

    combining 2 arrays

    can anyone tell me why I am crashing when i run this ?
    I am loading and then trying to combine 2 arrays to I can sort the final one:
    thanks!

    Code:
    #include <iostream>
    using std::cout;
    using std::cin;
    using std::cerr;
    using std::endl;
    using std::istream;
    using std::ostream;
    using std::ios;
    #include <fstream>
    using std::ifstream;
    using std::ofstream;
    #include <iomanip>
    using std::left;
    using std::right;
    using std::setw;
    
    void load (istream& infile, istream& infile2, ostream& outfile);
    
    
    void main ()
    	{
    	
    	
    	ifstream infile("C:\\McD\\Cee_\\PROJECTS\\MyProjects_sem2\\HW_cpp\\HW7_4\\file1.txt");
    	if(!infile)
    		{ 
            cerr << "Cannot open input file" << endl;
    		exit (1); 
    		}
    	
    	ifstream infile2("C:\\McD\\Cee_\\PROJECTS\\MyProjects_sem2\\HW_cpp\\HW7_4\\file2.txt");
    	if(!infile2)
    		{ 
            cerr << "Cannot open input file" << endl;
    		exit (1); 
    		}
    	
    	
    	ofstream outfile("7_4out.dat"); 
        if(!outfile)
    		{ 
            cerr << "Cannot open output file" << endl; 
            exit(1); 
    		}
    	
    	load(infile, infile2, outfile);
    	
    	
    	} 
    
    
    void load (istream& infile, istream& infile2, ostream& outfile)
    	{
    	
    	int num[10];
    	int num2[10];
    	
    
    	
    	while (( infile ) && (infile2))  
    		{
    		
    		for (int i = 0; i <10; i++)
    			{
    			for (int j = 0; j < 10; j++)
    				{	
    				
    				infile >> num[i];
    				if(num[i] ==-1) return;
    				
    				infile2 >> num2[j];
    				if(num2[j] ==-1) return;
    				
                                                                     //this is where i combine them..i think it's the problem?
    				((num[i+10])=(num2[j]));
    							
    				cout << num[i] << endl;
    				cout << num2[j] << endl;
    			
    					
    				}//if
    
    			
    			}//if
    				
    		}//wh
    	
    	}//fx

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    num can only hold 10 elements but you are trying to place 110 elements in it. element i + 10 doesn't exist. Your nested loop in load isn't necessary.

  3. #3
    Registered User HaLCy0n's Avatar
    Join Date
    Mar 2003
    Posts
    77

    Re: combining 2 arrays

    Originally posted by mouse163
    Code:
    #include <iostream>
    using std::cout;
    using std::cin;
    using std::cerr;
    using std::endl;
    using std::istream;
    using std::ostream;
    using std::ios;
    #include <fstream>
    using std::ifstream;
    using std::ofstream;
    #include <iomanip>
    using std::left;
    using std::right;
    using std::setw;
    Seems a lot of people do this for some reason. I would hate to have to type out all of those "using"s. All you need to put is "using namespace std;" and it will include everything in the std namespace. For more on namespaces in C++, http://faq.cprogramming.com/cgi-bin/...&id=1043284351

  4. #4
    Registered User mouse163's Avatar
    Join Date
    Dec 2002
    Posts
    49
    actually for some things using namespace std does not work.
    I would very much prefer to type one simple statement but....
    no dice @ times...

    I'm sure you'll run into a time when this is appropriate

  5. #5
    Registered User HaLCy0n's Avatar
    Join Date
    Mar 2003
    Posts
    77
    I have never run into a situation where that is the case, and it would work for your situation. Could someone else please tell me when this might be the case? As I understand it, if it's in the std namespace, it's included using "using namespace std;". Am I wrong?

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109

    Re: Re: combining 2 arrays

    Originally posted by HaLCy0n
    Seems a lot of people do this for some reason. I would hate to have to type out all of those "using"s. All you need to put is "using namespace std;" and it will include everything in the std namespace. For more on namespaces in C++, http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    you could. i prefer not to though because of global pollution. but that's my preference.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 03:24 PM
  2. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  3. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 09:47 PM
  4. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 08:24 PM
  5. Combining two char arrays into a third one
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 08-02-2002, 09:15 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21