1

I am trying to implement a simple c++ program which takes an input string with punctuation characters and return an output string removing those punctuations. The program is

#include<iostream> #include<cctype> using namespace std; int main() { int index=0; string sequence1,sequence2; cout<<"enter the sequence"<<endl; getline(cin,sequence1); for(index=0;index<20;++index) if(!ispunct(sequence1[index])) sequence2[index]=sequence1[index]; cout<<sequence2<<endl; return 0; } 

It gives me an error saying ld returned 1 exit status.The total error is

progprac: In function `_start': (.text+0x0): multiple definition of `_start' /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o:(.text+0x0): first defined here progprac: In function `_fini': (.fini+0x0): multiple definition of `_fini' /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crti.o:(.fini+0x0): first defined here progprac:(.rodata+0x0): multiple definition of `_IO_stdin_used' /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o:(.rodata.cst4+0x0): first defined here progprac: In function `__data_start': (.data+0x0): multiple definition of `__data_start' /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o:(.data+0x0): first defined here progprac: In function `__data_start': (.data+0x8): multiple definition of `__dso_handle' /usr/lib/gcc/x86_64-linux-gnu/4.6/crtbegin.o:(.data+0x0): first defined here progprac: In function `_init': (.init+0x0): multiple definition of `_init' /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crti.o:(.init+0x0): first defined here /tmp/ccpOfw2A.o: In function `main': progprac.cc:(.text+0x0): multiple definition of `main' progprac:(.text+0xe4): first defined here /usr/lib/gcc/x86_64-linux-gnu/4.6/crtend.o:(.dtors+0x0): multiple definition of `__DTOR_END__' progprac:(.dtors+0x8): first defined here /usr/bin/ld: error in progprac(.eh_frame); no .eh_frame_hdr table will be created. collect2: ld returned 1 exit status 

I checked online that it is a linker error where you declare a function and not define it. But I haven't made such an error. What is the error?

10
  • 1
    Your code compiles fine: link. Commented Jun 10, 2013 at 13:14
  • 3
    Please include the complete linker error message Commented Jun 10, 2013 at 13:15
  • 2
    U used g++ for compiling? Commented Jun 10, 2013 at 13:45
  • 1
    @talasila It's not compiler problem. It's problem of the build system. Can you specify how you are trying to build your source file? Commented Jun 10, 2013 at 13:50
  • 2
    @talasila g++ -o progprac progprac.cc? or alternatively g++ progprac.cc to create an executable a.out Commented Jun 10, 2013 at 13:59

1 Answer 1

1

You missed the -o in compile line,

g++ -o progprac progprac.cc 

After resolving 1st problem,

You have created a blank string sequence2 and in your for loop you make a if check to assign value to sequence2 and you use index as a subscript for it. In some cases, (where there are punctuation marks you don't assign anything to that character position of sequence2). That can be a source of trouble as the string is still "" .

If you use sequence2.at(index) it says out of range, which means the string does not exist[no characters] at those locations.

If you use + operator, you eliminate those issues, because you concatenate the characters to your existing string (starting with "")

 sequence2 += sequence1[index]; 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.