2023-04-15T23:00:00Z
Hi all, thought I’d share my completed Bleep project:
Best
Gareth
Blockquote
Things I learnt that helped me !! I’m new at this but this stuff below maybe useful to people learning basics
- use getline(std::can, string_name), not std::can << string_name to get all the input in, if you want a function where you can add via a function.
- When you use getline() make sure you clear out the std::can before or it will jump the input request
- You can use a function to block letter being passed through int to stop errors. EG. You press ‘g’ instead of 8 by mistake.
*a pass-by reference is good with a void function so you can change the variable in your hpp file. Use a function template with &before . eg…
void function(int &a, int &b, int c ) {
c = (a * b)
}
bleep.cpp
#include <iostream>
#include <vector>
#include "functions.hpp"
#include <string>
#include <stdio.h>
#include <string.h>
#include <cctype>
#include <unistd.h>
#include <stdlib.h>
using namespace std;
int main()
{
std::cout << "\n\n Welcome to my text editor. The goal of this programme is to allow the user to enter some text\n and the replace it with a new word.\n";
std::cout << " It will also give you some stats and locations of the changes in the text body. Enjoy! Gareth \n\n";
std::cout << " Are you ready to supress freedom of speech? \n";
y_n(answer);
std::cout << "\n\n";
while (answer ==1)
{
"\n\n";
bleep(word, text, replacement);
std::cout << "\n\n";
boarder(text);
y_n(answer);
}
std::cout << " goodbye";
}
functions.hpp
#include <iostream>
#include <vector>
#include <stdio.h>
#include <string>
#include <string.h>
//==========================function declarations ==============================
void y_n(int &g); //yes or no answers
void ignore_line(); //stops characters instead of digits in above
void bleep(std::string &a, std::string &b, std::string &c); //function for changing the content
void boarder(std::string &t); //adds a boarder to varble
//==========================used in the function loop===========================
std::vector <int> word_to_go;
int loc;
int vec_loc;
int word_length;
//===============variable declarations to be used as inputs in the function==============
std::string text;
std::string replacement;
std::string word;
int answer; // used for the if loop when asking user if they wish to do something with bleep
This is the function.cpp
#include <iostream>
#include <vector>
//#include "functions.hpp"
#include <string>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
using namespace std;
//==============stops characters coming in to cin for ints======================
void ignore_line()// stop alphabet characters being passd in to an int
{
std::cin.clear();
std::cin.ignore();
}
//==============================================================================
//============yes or no option for user - takes an int answer===================
void y_n(int &g)
{
std::cout << " press 1 to redact / edit text, 2 to exit: ";
int new_answer;
std::cin >> g;
while((g !=1) && (g !=2) && (std::cin.fail())) // invalid input repsonse
{
ignore_line();
std::cout << " you must pick 1 or 2: ";
std::cin >> g;
}
}
//================ bleep() function that edits text=============================
//a - word and b text c is the replacement word
void bleep(std::string &a,std::string &b, std::string &c)
{
cin.ignore();
std::cout << " Insert text body / paragraph:\n";
std::cout << " "; std::getline(std::cin, b); "\n";
std::cout << " You have entered " << b << "\n\n";
std::cout << " Insert single word to be replaced: \n";
std::cout << " ";std::cin >> a; "\n";
std::cout << " You have entered " << a << "\n\n";
std::cout << " Choose replacement word: \n";
std::cout << " ";std::cin >> c; "\n";
std::cout << " You have entered " << c; "\n\n";
std::vector<int> word_to_go; // logging the location of the text hits.
//if there are no matches to change, do this========================
if (b.find(a) == std::string::npos)
{
sleep(.5);
std::cout << "\n\n No changes needed\n\n";
}
//if there are matches, do this=====================================
else
{
for (int i = 0; i < b.length();) //stored location of words that need replace in vector
{
int loc;
loc = b.find(a, i);
word_to_go.push_back(loc);
i = (loc + a.length() );
}
//prints some data so user can see input has been processed=========
sleep(1);
std::cout << "\n\n";
std::cout << " stats:\n\n ";
sleep(2);
std::cout << " text length = " << b.length() << std::endl; sleep(.5); //length of text string
//std::cout << " First hit location = " << b.find(a) << std::endl;
std::cout << " word size = " << a.size() << std::endl; sleep(.5); //size of word to be replaced
std::cout << " words to replace = " << word_to_go.size(); sleep(.5); //size of vector is number of hits
std::cout << " \n\n";
std::cout << " " << b << std::endl;
//this part implements the replacement iteration loop ==============
for (int j= 0; j < word_to_go.size(); j++) //uses vector as locations in text string for where replace word string with replacement string
{
sleep(.5);
std::cout << " Match number " << j+1 << " at point " << word_to_go[j] << " in the document and has been replaced with " << c; sleep(.2); std::cout << "\n";
b.replace(word_to_go[j], a.length(),c); //replaces the element in text with new word
} // end of replacement word loop
} // end of 'if' there are matches overall loop
sleep(1);
//std::cout << " Redacted / modified text now reads as: \n\n";
}
//======creates a boarder for the end result================================
void boarder(std::string &t)
{
std::cout << "\n\n";
sleep(.5);
std::cout << "Edited text \n\n";
const int pad = 1;
//desired rows/columns
const int rows = pad * 2 + 3;
const string::size_type cols = t.size() + pad * 2 + 2;
// seperate output from input
cout << std::endl;
// invariants
int r = 0;
while (r != rows) {
string::size_type c = 0;
while(c != cols) {
if (r == 0 || r == rows -1 || c == 0 || c == cols -1) { // if in bordering column or row
cout << "*"; //output *
}
else {
if (r == pad + 1 && c == pad + 1) { //if on row for greeting
cout << t; // write greeting
c += (t.size()-1); // adjust invariant
}
else {
cout << " ";
}
}
++c;
}
++r;
cout << std::endl;
}
}
//===========end of boarder function=========================================
//std::cout << " '" << b << "'"; // prints modified text
// end of bleep() function