0

I'm pretty new to c++ and tried this project and got it to work. Basically it takes a word and a sentence then changes the word to asterisks whenever said word is found in the sentence.

The problem I am facing is I tried taking it a step further by asking the user to input a word and input a sentence and then using those as a stored variable to run the code exactly the same way as before, but this doesn't work and only outputs the first word of the sentence. I can't figure it out why it's doing that. Everything is the same except for the cin. Am I just not understanding how cin works with strings?

Main code:

#include <iostream>
#include <string>

#include "functions.hpp"

using namespace std;

int main() {

  string word = "brocolli";
  string sentence = "Roll up that brocolli. I love brocolli!";

  bleep(word, sentence);

  for (int i = 0; i < sentence.size(); i++) {

    cout << sentence[i];

  }

  cout << "\n";

  return 0;

}

Header file:

#include <string>

void asterisk(std::string word, std::string &text, int i);

void bleep(std::string word, std::string &text);

Functions file:

#include <string>

#include "functions.hpp"

using namespace std;

void asterisk(string word, string &text, int i) {
  
  for (int k = 0; k < word.size(); ++k) {

    text[i+k] = '*';

  }

}

void bleep(string word, string &text) {

  for (int i = 0; i < text.size(); ++i) {

    int match = 0;

    for (int j = 0; j < word.size(); ++j) {

      if (text[i+j] == word[j]) {

        ++match;

      }

    }

    if (match == word.size()) {

      asterisk(word, text, i);

    }
    
  }

}

Adjusted main code to include cin:

#include <iostream>
#include <string>

#include "functions.hpp"

using namespace std;

int main() {

  string word;
  string sentence;

  cout << "Word: ";
  cin >> word;

  cout << "Sentence: ";
  cin >> sentence;

  bleep(word, sentence);

  for (int i = 0; i < sentence.size(); i++) {

    cout << sentence[i];

  }

  cout << "\n";

  return 0;

}
  • *"Everything is the same except for the cin."* -- did you verify that part in isolation? That is, right after reading from `cin`, did you send that data to `cout` so that you can be confident that the variables have the values you think they do? *For example, you could do something like: `cout << "Sentence: "; cin >> sentence; cout << "The sentence is: '" << sentence << "'\n";`* – JaMiT Apr 26 '22 at 23:17
  • 1
    Did you try using a debugger to analyze what your code is doing step by step and what values your variables have? – CherryDT Apr 26 '22 at 23:18
  • I didn't. I'm using Codecademy and so I'm just using the terminal on their website if that makes sense. I'm pretty new so I'm not even sure how to use a debugger but I will look into it! – Bruno Oliveira Apr 26 '22 at 23:20
  • Sorry, but C++ is just too complicated, it is the most complicated and a hardest to learn general purpose programming language in use today. Any clown can upload a video to Youtube, or Codecademy, or write a rambling blog on their web site, even I can do that. But the only way to learn C++ is with a guided textbook-based study course, and a stable environment, with a C++ compiler and a debugger that you can work with. A simple "terminal" and a "website" won't be enough. – Sam Varshavchik Apr 26 '22 at 23:22
  • I thought Codecademy was a trusted resource but maybe I was wrong. I'll look into a book and a debugger. Thanks – Bruno Oliveira Apr 26 '22 at 23:29
  • @BrunoOliveira *"I'm just using the terminal on their website"* -- that does not prevent you from using diagnostic output to debug. – JaMiT Apr 26 '22 at 23:32
  • Side note: About [`using namespace std`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)... (Especially: Never ever do that in a *header*!) – Aconcagua Apr 26 '22 at 23:32
  • @JaMiT I will try isolating that part of the code as you suggested, thanks! – Bruno Oliveira Apr 26 '22 at 23:40
  • @Aconcagua dang, I even had this whole code written out without using namespace std and then I decided to include because I thought it would look simpler. I now see that it can be bad practice to use it, so thanks for the input. – Bruno Oliveira Apr 26 '22 at 23:43
  • I know nothing about CodeAcademy and it's educational quality, but I have yet to see an online IDE that is as useful as a full toolchain installed on a computer. I'm sure it's coming, and probably no that far off, but it's just not there yet. – user4581301 Apr 26 '22 at 23:55
  • @user4581301 I've used visual studio code for other projects with javascript and html. Would that be good to use with C++ as well? – Bruno Oliveira Apr 27 '22 at 00:22
  • Some folk swear by it. I found it hard to configure, and the number of questions on the topic here back me up. Expect to do a bit of reading on juts how to set up the JSON configuration files, and my understanding is it works well with CMake driving the build process if you have larger projects. – user4581301 Apr 27 '22 at 00:29

1 Answers1

2
  cout << "Sentence: ";
  cin >> sentence;

The operator>> function you are using here stops reading at the first space character. It is not suitable for reading in a line of text.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278