my code is wrong:(

i have tried a lot to solve this problem but still my code is wrong could any one help me!!,this the problem:
FirstName LastName
Ali Abdallah
Maged Sayyed
Soha Gad
Ahmed Baher
Salma Gamal
Store these values of first & last name for these persons then display it in western system (LastName then FirstName).
Hint: use 2D Array

and this is my code:
# include <iostream>
#include <string.h>
using namespace std;
void main()
{char arr [5][2];
cout<<"plz enter 1st name last name"<<endl;
for (int i=0;i<5;i++)
{for (int j=0;j<2;j++)
cin>>arr[i][j];
}
cout<<"Names in western system are"<<endl;
for (int i=0;i<5;i++)
{
for (int j=0;j<2;j++)
cout<<arr[j][i]<<" ";
}
}
thnx anyway..
Try using this logic,which as per you uses 2d array.
Here is code snippet :-
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
         //a character array for holding max 5 FULL NAME, i.e. First name and Last name 
         char szArray[10][100];

        //this loop will take input as hold actually 5 FULL NAME
	for(int i=0; i<10; i++)
	{
		cin>>szArray[i];
	}

	cout<<"\n printing now\n";
	
	for(int i=0; i<10; )
	{
		cout<<szArray[i+1]<<" "<<szArray[i]<<"\n";
		i+=2;
	} 


I hope this would help you.
If you like this, you can also visit my website.
I think one problem is, that you use a character 2-dimensional Array. But should be string.
I suppose the hint want to say, store last and first name in two differeny fields of the 2d Array.

So first try to change

 
char arr [5][2];


into (do not forget to change cstring into string header)

 
string arr[5][2];


Second problem is, that i would think about, if changing indexes i and j really does solve your switching thing.

Edit: think about how you store the names. If you type "Hulk TheGreat" at the first line, how do you access "Hulk" and "TheGreat" ? (hint: arr[line][1] and arr[line][0])

Greatings, Maikel
Last edited on
Maike, i have taken 2d array of character and i structured it in a way that it can hold 20 string value of size max 100.
And as cin>> operator breaks string on "space" or "newline". So when you enter first name it will come into arr[0] and when you enter second name it will come into arr[1]...and after which you would enter new line which shift your array to arr[2], where you again enter First Name and so on..
So, ultimately you would have 10 strings value which actually denotes 5 FULL NAME.

So as per your doubt if you type "Hulk TheGreat" at the first line.
Then it arr[0] = "Hulk"
and arr[1] = "TheGreat".

Regards
-Tajendra
I'm sorry Tajendra, i was referring not to you but to the one who had the question (mandolin). In my oppinion his intend of two dimensional array was an other than yours.

Greatings,
Maikel
Using the string to collect five different name ( First name and the last name ) and then finding the white space to separate array for the first name and the last name then reversing (Last name and then first name) will solve the problem .
you have to use substring for that . .. to separate the last name .
Did you even look at his code? He just need to fix very few things and HIS code works. I do not think its gonna help if we give him even more different ideas. There are really more than 1000 ways of solving this excercise. -_-

But since hes not answering anymore, I suppose the problem is already solved ~~
Last edited on
Topic archived. No new replies allowed.