output problem

hi,
I'm trying to write a code to read an input string (InputReaded) and divide it into 2 different string;
the first one (Operazione) is maden up of the characters befor '('; the second one (Parentesi) is maden up of the characters between the '(' and ')'.
I can read the input using scanf() and i can copy che characters before '(' in the string called 'Operazione', but when i try to copy the characters between '(' and ')' in the string called 'Parentesi' i fuound difficults.
The problem is I can't copy the character, in fact when i try to print the string 'Parentesi' it's empty.
the code i use is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <cstdlib>
#include <iostream>
#include <string.h>
#include <stdio.h>

using namespace std;

int main(int argc, char *argv[])
{
char InputReaded[100];
char Operazione[100];
char Parentesi[100];

 
scanf("%s", InputReaded);
 

char * OpenBraket = strchr(InputReaded,'(');
char * ClosBraket = strchr(InputReaded,')');


strncpy(Operazione,InputReaded,int(OpenBraket-InputReaded));
Operazione[int(OpenBraket-InputReaded)]='\0';


for(int i=int((OpenBraket-InputReaded)+1);i<int(ClosBraket-InputReaded);i++)
    Parentesi[i]=InputReaded[i];
Parentesi[int(ClosBraket-InputReaded-1)-int(OpenBraket-InputReaded+1)]='\0';

 
    system("PAUSE");
    return EXIT_SUCCESS;
}


thank for help.
Why not use C++ instead of C?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <string>
using namespace std;

int main()
{
	string InputReaded, Operazione, Parentesi;

	// Read into InputReaded
	getline(cin,InputReaded);

	// Find the locations of the ()
	int OpenBraket = InputReaded.find_first_of('(');
	int ClosBraket = InputReaded.find_last_of (')');

	// Copy the string
	Operazione.assign(InputReaded, 0           , OpenBraket);
	Parentesi.assign (InputReaded, OpenBraket+1, ClosBraket-OpenBraket-1);

	// Output the results
	cout << Operazione << endl;
	cout << Parentesi  << endl;

	return 0;
}
Hello I'm Joe (I'm a plumber)
Hello I'm Joe
I'm a plumber
Press any key to continue . . .


References:
http://cplusplus.com/reference/string/getline/
http://cplusplus.com/reference/string/string/find_first_of/
http://cplusplus.com/reference/string/string/find_last_of/
http://cplusplus.com/reference/string/string/assign/

Edit: Made naming convention the same as the OP and fixed an index thing as per
http://cplusplus.com/forum/general/64168/#msg347092
Last edited on
So many castings.
¿Are you using spaces in the input string?

1
2
for(int i=iOpenBraket-InputReaded)+1;i<ClosBraket-InputReaded;i++)
    Parentesi[i]=InputReaded[i];
Do a desk test. Look at the value of 'i'.
¿Why you didn't use strncpy() as before?

I'll suggest you to check out std::string.

system("PAUSE"); look at the sticky in the beginners forum. 'Console Closing Down'.
thanks for answer;
I just thought with C i have more control on my owner code than C++;
i tryed your code but it saves in Parentesi also the character ')'. is it right to write:

Parentesi.assign(Input,Opening+1,Ending-Opening-1);

instead

Parentesi.assign(Input,Opening+1,Ending-Opening);

i just added -1 at the end....or there is another method?
You're right, I just fixed that in the code above.
¿Why you didn't use strncpy() as before?


becouse strncpy() copy the first n characters of a string in another string.
But it can't copy the last n characters...or am I wrong?

strncpy() can ignore the first m characters of a string and copy the following n characters into another string?
I need to delete the first m characters from my string 'InputReaded' and then use strncpy(), right?
You don't understand pointers/iterators.
1
2
strncpy(Parentesi, OpenBracket+1, ClosBracket-OpenBracket-1);
Parentesi[ ClosBracket-OpenBracket-1 ] = '\0';
Topic archived. No new replies allowed.