1

I'm a little confused by the class code. Here's what I'm trying to do:

//Program takes "string text" and compares it to "string remove".  Any letters in
//common between the two get deleted and the remaining string gets returned. 
#include <string>
#include "genlib.h"

string CensorString1(string text, string remove);

int main() {
    CensorString1("abcdef", "abc");
    return 0;
}

string CensorString1(string text, string remove) {
    for (int i = 0; text[i]; i++){
        for (int n = 0; remove[n]; n++){
            if (i != n){
                string outputString = ' '; //to store non repeated chars in,
                                           //forming my return string
                outputString += i;
                }
            }
        }
        return outputString;
}
  1. I'm getting an error on the "outputString += 1" saying: "cannot convert from "char" to std::basic_string
  2. I'm also getting an error on the "return outputString" saying: undeclared identifier ???????

I get that I'm putting a "char" on a "string" variable but what if shortly that "char" will soon be a string? Is there a way to pass this?

I'm always forgetting libraries. Can someone recommend a couple of standard/basic libraries I should always think about? Right now I'm thinking , "genlib.h" (from class).

C++ is kicking my ass. I can't get around constant little errors. Tell me it's going to get better.

locriani
  • 4,995
  • 2
  • 23
  • 26
MCP
  • 4,436
  • 12
  • 44
  • 53
  • I hope you didn't really put `using namespace std;` in a header, did you? What are you trying to achieve by `outputString += i`? – Fred Foo Jan 08 '12 at 13:14
  • If this is homework, please tag. – Sergey Kalinichenko Jan 08 '12 at 13:14
  • *C++ is kicking my ass. I can't get around constant little errors. Tell me it's going to get better.* It has, it's called Java or C#, with C++ you will forever be stuck with libraries that use different kind of character strings and unsafe conversions. Of course, having these libraries and access to OS functionality is a major reason to go with C++ in the first place. – Maarten Bodewes Jan 08 '12 at 13:18

4 Answers4

2

There are many errors in your code:

  • Your outputString needs to be in the outer scope (syntax)
  • You compare i to n instead of text[i] to remove[n] (semantic)
  • You are adding i to the output instead of text[i] (semantic)
  • You ignore the return of CensorString1 (semantic)

Here is your modified code:

string CensorString1(string text, string remove) {
    string outputString;
    for (int i = 0; text[i] ; i++){
        for (int n = 0; remove[n] ; n++){
            if (text[i] != remove[n]){
                outputString += text[i];
            }
        }
    }
    return outputString;
}

This has some remaining issues. For example, using text[i] and remove[n] for termination conditions. It is also very inefficient, but it should be a decent start.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

At any rate, strings are always double-quoted in C and C++. Single-quoted constants are char constants. Fix that and you should probably be all right.

Also, look at this SO question: How do you append an int to a string in C++?

Good luck at Stanford!

Community
  • 1
  • 1
Wander Nauta
  • 18,832
  • 1
  • 45
  • 62
  • Thanks WN. That was helpful. The little things... Now I just need to understand why I get the Error "undeclared identifier" for "return outputString". didn't I identify it above? – MCP Jan 08 '12 at 13:20
  • @MCP because you're declaring 'outputString' in the scope of if statement. It gets destroyed when that scope ends. It needs to be at the function scope, so declare it at the start of the function. – jrok Jan 08 '12 at 13:23
0

There are some problems there:

string outputString = ' '; will try to construct a string from a char, which you can't do. You can assign a char to a string though, so this should be valid:

string outputString;
outputString = ' ';

Then, outputString is only visible within your if, so it won't act as an accumulator but rather be created and destroyed.

You're also trying to add character indices to the string, instead of characters, which is not what I think you want to be doing. It seems like you're mixing up C and C++.

For example, if you want to print the characters of a string, you could do something like:

string s("Test");
for (int i=0;i<s.length();i++)
    cout << s[i];

Finally, I'd say that if you want to remove characters in text that also appear in remove, you'd need to make sure that none of the characters in remove match your current character, before you add it to the output string.

Vlad
  • 18,195
  • 4
  • 41
  • 71
0

This is an implementation of what I think you want, your code has multiple problems which just showed up described in multiple other answers.

std::string CensorString1(std::string text, std::string remove) {
    std::string result;
    for (int i = 0; i<text.length(); i++) {
        const char ch = text[i];
        if(remove.find(ch) == -1)
                result.append(1,ch);
    }
    return result;
}
Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
  • That is actually pretty clean. I'm assuing that implementation will be a lot less inefficient than my code? I've got to get used to seeking member functions to complete a task... Thanks JI. – MCP Jan 08 '12 at 14:36
  • When you're getting into it a bit more, you'll like iterators instead of the for loop above. Iterators are a bit harder to grasp, so I left them out of this simple implementation. When you feel more comfortable, look them up though :) – Joachim Isaksson Jan 08 '12 at 14:57