I'm currently writing a code using functions that will take in a users phrase, remove any extra spaces and then allow the user to re-assort the phrases as needed. This is one of my first time using functions and I'm stuck when I try to remove the spaces.
The code that I have written takes in the statement from the user input device. I then created a for loop that was supposed to iterate and find any spaces that were more than one. The problem is, when I pass the function check_phrase, all of my content is deleted and it keeps me enters an infinite loop, I believe.
I'm really stuck on this and I can't use strings to fix anything. It all has to be done using arrays of characters. Solutions with array advice would be the most appreciated. Thank you!
// The purpose of this program is to create a set of user defined phrases.
// These phrases are going to be predefined by the user.
// The purpose of the phrases will be to serve as a predefined statement
// that will serve to respond to emails or messages that are commonly sent.
#include <iostream>
#include <cstring>
usingnamespace std;
constint MAX = 130;
void phrase_1 (char sent_1 [] ); // reads in the
void phrase_2 (char sent_2 [] );
void phrase_3 (char sent_3 [] );
void phrase_4 (char sent_4 [] );
void check_phrase (char statement []); // adjusts the number of spaces pass by reference
void date (int& );
void greeting ();
int main ()
{
char phr_1 [MAX];
char phr_2 [MAX];
char phr_3 [MAX];
char phr_4 [MAX];
greeting();
phrase_1 (phr_1); // function call for first phrase
// cout << "These are the contents of function call 1: " << endl;
// cout << "entering call two" << endl;
// check_phrase (phr_1); //check phrase
phrase_1 (phr_2); // second phrase call
phrase_1 (phr_3); // third phrase call
phrase_1 (phr_4); // fourth phrase call
return 0;
}
void greeting ()
{
cout << "Hello!\n"; // greet user and explain the program
cout << " I bet you're sick of sending the same emails every day!"
<< " Good news for you, we're going to pre-define some phrases that will"
<< " ultimately serve as your fast lane ticket to having free time again!"
<< "instead of fussing with those pesky emails!";
cout << "Let's get started!" << endl;
}
void phrase_1 (char sent_1 [])
{
cout << "Enter your statement below followed by enter when finished (130 characters max) \n";
cin.get (sent_1, MAX, '\n');
while (cin.peek() != '\n')
{
cout << "I'm sorry, you entered too many characters, please reenter your statements below. '\n'" << endl;
cin.get (sent_1, MAX, '\n');
}
cin.ignore(100, '\n');
cout << "Your statement is: " << sent_1 << endl;
int len = strlen(sent_1);
sent_1[0] = toupper(sent_1[0]);
for (int i = 0; i < len; ++i)
{
if (sent_1[i] = ' ')
while (sent_1[i+1] = ' ')
sent_1[i] = '8';
}
cout << "Your statement after correction is:\n " << sent_1 << endl;
}
#include <cctype>
#include <cstring>
#include <iostream>
usingnamespace std;
// Modifies s in place.
char* no_extraneous_ws( char* s )
{
// r is our result -- pointer to the original s
char* r = s;
// p is a pointer to further along in the string than s.
// We'll use it to copy characters from p to s (which overwrites s!)
char* p = s;
// Find the first not-space character
while (isspace( *p )) ++p;
// While we haven't hit the end of the string
while (*p)
{
// Copy characters back, including any space.
*s = *p++;
// If we just copied a space, skip any immediately following spaces
if (isspace( *s ))
while (isspace( *p )) ++p;
++s;
}
// If the last character is a space, remove it too
if (isspace( s[-1] )) --s;
// Terminate the string
*s = '\0';
// And return it to caller
return r;
}
int main()
{
char s[] = " Hello world! ";
cout << "\"" << no_extraneous_ws( s ) << "\"\n";
}