MCP
MCP

Reputation: 4536

Basic C++: convert from char to string

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.

Upvotes: 1

Views: 6293

Answers (4)

Joachim Isaksson
Joachim Isaksson

Reputation: 181077

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;
}

Upvotes: 0

Vlad
Vlad

Reputation: 18633

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.

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726987

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.

Upvotes: 2

Wander Nauta
Wander Nauta

Reputation: 19695

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!

Upvotes: 1

Related Questions