Thread: can't find ronin, so can you guys help :D

  1. #1
    Registered User
    Join Date
    Apr 2002

    can't find ronin, so can you guys help :D

    remember ronin showed me this code last week ... basically inserting string 2 into string 1, user decides where to make the string insertion.

    #include <iostream.h>
    int main(void)
    	char user1[80];
    	char user2[80];
    	char temp[256];
    	temp[0] = '\0';
    	int loc, i, j;
    	cout << "Enter string1: ";
    	cin.getline(user1, 80); 
    	cout << "Enter string2: ";
    	cin.getline(user2, 80);
    	cout << "Enter loc where to place string 2 into string 1: ";
    	cin >> loc;
    	for(i=0; i < loc-1; i++)
    		temp[j++] = user1[i];
    	for(i=0; user2[i] != '\0'; i++)
    		temp[j++] = user2[i];
    	for(i=loc-1; user1[i] != '\0'; i++)
    		temp[j++] = user1[i];
    	temp[j] = '\0';
    	cout << endl << temp << endl; 
    	return 0;

    this of course works perfectly fine
    but when it's my turn to incorporate stuff into it, obviously the whole thing crashed !

    I'm just trying to use that to work with 2 dimensional arrays, like so....

    int main()
    	const int MAX=3;
    	char array1[MAX][81];
    	char array2[MAX][81];
    	char temp[MAX][81];
    	int location,j; 
    	int i=0;
    	cout<<"enter string 1"<<endl;
    	cout<<"enter string 2"<<endl;
    	cin.getline(array2[i], 80);
    	cout<<"location in string1 to insert string2 ?: ";
    	for(i=0; i<location-1; i++)
    // element [0] because I'm trying to copy line 1 of array1 into
    // line 1 of temp.									
    	//for(i=0; array2[i]!='\0'; i++) 
    	//	temp[0][i++]=array2[0][i];
    	//for(i=location-1; array1[i]!='\0'; i++)
    	//	temp[0][j++] = array1[0][i];
    // the last 2 for loops are commented out, coz they crashed my 
    // whole comp. !
    	return 0;

  2. #2
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    i'm just wondering why are you making your life difficult, by working with 2 dimensional arrays???

    What you have as your previous (one dimensional array) works, so why not stick with it?

    Anyway, your version is "logically" incorrect. Well, if you are doing it for the sole purpose of learning, I recommend that you take out a piece of paper and trace your execution step by step...

    good luck....


  3. #3
    Registered User
    Join Date
    Apr 2002
    believe me , I wouldnt want to make life anymore difficult than "a
    life with c++" already is

    <--- still in school, part of class work requirement.

    God, you dont know how much on paper planning and scribbling
    I've done, execute step by step etc ! urgghh

  4. #4
    I lurk
    Join Date
    Aug 2002
    Life with C is hard... on the other hand, life with C++ is easy!
    #include <iostream>
    #include <string>
    int main(int argc, char** argv)
    	using namespace std;
    	string input[2];
    	cout << "Enter string 1: ";
    	getline(cin, input[0]);
    	cout << "Enter string 2: ";
    	getline(cin, input[1]);
    	int index;
    	cout << "Enter index to insert at: ";
    	cin >> index;
    	input[0].insert(index, input[1]);
    	cout << input[0] << endl;
    	return 0;

  5. #5
    Registered User
    Join Date
    Apr 2002
    okay looking at Eibro's code and only one word went thru my head ...

    " yikes !"

  6. #6
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    279 that case I will give you few pointers...

    When you are working with 2-dimensional arrays, in reality (in memory) what you are working with is one (twice as long) row of memory locations....

    char a[20]; // continues (one after another)  ....20 characters...
    char b[3][20]; // continues (one after another) .... 60 characters
    at first I would concern myself with "readin in" my character array correctly, therefore I would display each one of them on the screen the moment one is entered...

    HINT: include the following: <conio.h> and put getch() right after the "cout" statement .... it will hault the execution and wait for user to press any key and it will contiue if you have done so, i.e.

    // inside main.... after cout....
    getch();          // pauses the execution
    after you made sure that the arrays were inputed properly you can go onto combining them or whatever else you wanted to do...keeping in mind that pausing an execution or having temporary "cout" statements in your code to display a given value or tell the state or a location in the array is a valuable helper in debugging...

    anyway.............i don't know on what programming level you are currently, but i'm speculating you don't know pointers yet, thus I would recommend a different approach to inputing those arrays of characters.....

    instead of using ...getline() function (which operates with a implicit pointer to the first character in the array --- don't concern yourself much with this now) I would recommend using a nested double loop for entering a character by character and a loop termination when "new line" (\n) is entered, for example:

    char array[3][10];
    for(int i = 0; (i < 3 && array[i][j] != '\n'); i++)
          for(int j = 0; (j < 10 && array[i][j] != '\n'); j++)
               cin >> array[i][j];
    // keep in mind this is just an arbitrary sample code
    // which doesn't necessarily have to be used in your context..

    hope that helps a little bit...

    keep me posted on how things are going...?


    ps. ohhh yeah.........i hope this isn't due tomorrow???
    and please forgive my unhelpfullness in the previous post...

  7. #7
    Registered User
    Join Date
    Apr 2002
    haha it is due tomorrow !

    yea we sortta touch on pointer but that's not what we should
    use for now.

    I'm just writing a 300 lines or so program using 2 dimensional
    array, doing basic array operation like you said.
    Nearly there except for that insertion bit.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. please help!...find and replace
    By amy589 in forum C++ Programming
    Replies: 26
    Last Post: 10-13-2006, 06:42 PM
  3. Can't find DirectX!!!
    By Queatrix in forum C++ Programming
    Replies: 2
    Last Post: 07-24-2006, 07:50 PM
  4. Replies: 3
    Last Post: 06-09-2006, 09:53 AM
  5. Q: Recursion to find all paths of a maze
    By reti in forum C Programming
    Replies: 7
    Last Post: 11-26-2002, 09:28 AM