Thread: I forget c for a long time.

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    4

    I forget c for a long time.

    I pick up c today for a short exercise.
    Tons of error, be ashamed.
    Code:
    // removechar.cpp : main project file.
    
    #include "stdafx.h"
    #include <string>
    #include <iostream>
    using namespace std;
    
    
    String removechar(String s, char c) {
       String r = "";
       for (int i = 0; i < strlen(s); i ++) {
          if (s.at(i) != c) r += s.at(i);
          }
       return r;
    }
    
    
    int main() {
    	String s;
         removechar re = new removechar();
         s=re.removechar("abcdefg","f");
    }
    Error 1 error C2146: syntax error : missing ';' before identifier 'removechar'
    ...
    thanks for help

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Is there such a class as String? I believe it's string (lower case).
    What's more... are you trying to create a new instance of a function??? You can't do that. Functions you call directly:
    Code:
    s = removechar("abcdefg", 'f');
    It's also a little stupid to call strlen when you're using a string class. It should have its own get length function, though I don't use basic_string very much so I don't know its name.
    It seems like you're a little more than rusty. Maybe you should read up on C/C++ basics some more again.
    You also try to pass a char* to removechar instead of char. "f" --> char* 'f' --> char.
    Last edited by Elysia; 10-21-2007 at 07:00 PM.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    4
    Code:
    #include "stdafx.h"
    #include <string>
    #include <iostream>
    using namespace std;
    
    string removechar(char *s, char c) {
       char *r;
       for (int i = 0; i < strlen(s); i ++) {
          if (s.at(i) != c) r += s.at(i);
          }
       return r;
    }
    
    int main() {
    	char s[];
        s=removechar("abcdefg",'f');
    }
    I modified it, still get some errors.
    please give me an advice.
    read up on C/C++ basics is helpful but too slow.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    There are so many wrong things with that code, I don't know where to start...
    You should really read up on some basics, because you can't continue like this. Everything you're doing is wrong.
    I could point out all mistakes, but...
    Okay, let me try to point them out.

    First:
    Code:
       char *r;
    Unitialized pointer. Pointer to unowned memory. Pointer to non-existant buffer.

    Code:
          if (s.at(i) != c)
    s is of type char* - it's NOT a class! You can't call member functions of built-in types. Either use the index operator or declare it as "string s":
    Code:
    string s;
    s.at(i)
    or
    Code:
    s[i]
    Code:
     r += s.at(i);
    For s.at, refer to above. As for "r +=" - all you're doing is incrementing the pointer address (if the compiler even will allow that, I don't know).
    This is a POINTER, so to access the memory, you have to dereference it, like *r. HOWEVER, that's not all. Since this isn't a string class, you need to use string functions such as strcpy or strcat to copy/append strings.

    Code:
    	char s[];
        s=removechar("abcdefg",'f');
    First, why are you declaring s as char[] when you're returning a "string"? Second, there's no such thing as dynamic arrays for built-in types as char. You can't declare a variable of type char[]. It must be a fixed amount, so that's not even possible. If not using a class, you'd return a pointer to the string (char*) where the string is on the heap.

    As I pointed out, you're lacking basics. You can't program with this little knowledge you have.

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    4
    Code:
    // removechar.cpp : main project file.
    
    #include "stdafx.h"
    #include <string>
    #include <iostream>
    using namespace std;
    
    
    char *removechar(char *s, char c) {
       char *r;
       int j;
       for (int i = 0; i < strlen(s); i ++) {
    	   if (s[i] != c) {
    		  j= i;
    			  r[i] = s[j];}
    	   else{
    		   j=i+1;
    		   r[i] = s[j];
    	   }
          }
       return r;
    }
    
    int main() {
    	char *s;
        s=removechar("abcdefg",'f');
    	cout<<s;
    }
    almost there!
    Warning 1 warning C4018: '<' : signed/unsigned mismatch
    Warning 2 warning C4700: uninitialized local variable 'r'

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    ...You're not listening. r is not pointing to a buffer.

    Code:
    char* removechar(char* s, char c)
    {
    	char* r = new char[ strlen(s) + 1 ]; // YOU MUST HAVE A BUFFER OR A POINTER TO A BUFFER IF YOU'RE GOING TO MODIFY THE MEMORY
    	int j = 0; // Keep track of the position in the buffer pointed by r
    	for (int i = 0; i < strlen(s); i++) // Loop until the end of string s
    		if (s[i] != c) r[j++] = s[i]; // If the char at pos i in string s is not equal to c then assign that char to the position j in the buffer r and increment j
    	r[j] = '\0'; // Put a NULL char at the end of the string. All string function expect a C-string to end with a NULL char
    	return r; // Return out newly created buffer
    }
    
    int main()
    {
    	char* s = removechar("abcdefg", 'f'); // Remove 'f' from the string "abcdefg" ("abcdefg" IS NOT A BUFFER) and assign the returned buffer to s
    	cout << s; // Print the contents of s to the console or wherever the default outstream is pointing to.
    	delete [] s; // VERY IMPORTANT: DELETE OUT BUFFER NOW THAT WE'RE FINISHED WITH IT.
    }
    Watch that code carefully and read the comments.

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    If you insist on using strlen() instead of string.size(), you need to include <cstring>.
    So far your 2nd version was the best.
    Forget about char* and char[] types; that's C, use std::string instead.

    Here are some hints:
    - Use the string::find() function to find the char you want to remove.
    - Use the string::erase() function to delete the char from the string.
    - Here's a good function signature to use: void RemoveChar( std::string& str, char c )

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Look into the <algorithm> library. Chances are that you can let it do most of the hard work.

    Code:
    void remove_char(std::string& s, char c)
    {
        //string::erase version which erases a range between two iterators
        //std::remove - removes the occurrences of the value and returns
        //an iterator to the end of the valid range
        s.erase(
            std::remove(s.begin(), s.end(), c), //from the beginning of invalid range
            s.end());                           //up to end of string
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to time how long a program takes to run?
    By advancedk in forum C Programming
    Replies: 2
    Last Post: 08-18-2008, 07:50 PM
  2. Time to seconds program
    By Sure in forum C Programming
    Replies: 1
    Last Post: 06-13-2005, 08:08 PM
  3. Quick, Partiotion, and Insertion Sort
    By silicon in forum C++ Programming
    Replies: 0
    Last Post: 05-18-2005, 08:47 PM
  4. The space time continueimnms mm... (rant)
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 32
    Last Post: 06-27-2004, 01:21 PM
  5. convert unix long time to get hour
    By huskers in forum C Programming
    Replies: 11
    Last Post: 05-09-2002, 07:03 PM