0

We are taking user input and reversing it in the background and asking the user what is the reverse. Then we compare realreverse and userreverse and say that it's correct or how many mistakes were made. Also we are counting sentences with using dots, and the user should end his input with @.

For example:

hi. how are you doing. @

or

hi bro how are u. 
im good. @

The problem is my program is only working if the input doesn't contain any spaces.

hihowareyoudoing. thanksimfinebro @     

If the input is like that, it's working. it works

hihowareyoudoing.
thanksimfinebro. @

This 2 column also working

code: (program shows correct inverse for testing)

#include <iostream>
#include <string>
using namespace std;
void Comparison(string reverseReal, string reverseUser) {
    int wrongCount = 0;
    if (reverseReal.length() != reverseUser.length()) {
        cout << "These strings are not comparable." << endl;
    }
    else {
        for (int i = 0; i < reverseReal.length(); i++) {
            if (reverseReal.substr(i, 1) != reverseUser.substr(i, 1)) {
                wrongCount++;
            }
        }
        cout << "Number of mistakes: " << wrongCount << endl;
        if (wrongCount == 0){
            cout << "gj correct reverse" << endl;
        }
    }
}
string getReverse(string sentence) {

    string reverseResult = "";
    for (int i = sentence.length() - 1; i >= 0; i--) {
        reverseResult += sentence.substr(i, 1);
    }
    return reverseResult;
}
int findsnumber(string snumber)
{
    unsigned int sentences = 0, index = 0, length;
    string searchPattern = ".";
    length = snumber.length();
    while (index < length)
    {
        index = snumber.find(searchPattern, index);
        if (index != string::npos)
        {
            index += searchPattern.length();
            sentences++;
        }
    }
    return sentences;
}

// Gets the string until it finds a dot in it
string getOneSentence(string sentence) {
    while (sentence.find(".") != string::npos) {
        int dotFinder = 0;
        string sub = "";
        dotFinder = sentence.find(".");
        sub = sentence.substr(0, dotFinder);
        return sub;
    }
    return "";
}

int main() {

    string input = "";
    string result = "";
    string reverseReal = "";
    int i = 1;
    cout << "Welcome to my cancer c++ programme." << endl;
    cout << "Enter the paragraph with @ at the end to end it:" << endl;
    while (cin >> input && input != "@") {
        result += input + " ";
    }
    int sentnumber = findsnumber(result);
    cout << "Number of sentences in the given string " << sentnumber << endl;
    while (result.find(".") != string::npos) {
        string sub = "";
        string subReverse = "";
        string reverse = "";
        string reverseson = "";
        cout << i << "/" << sentnumber << endl;
        sub = getOneSentence(result);
        subReverse = getReverse(sub);
        cout << "reverse of the sub " << subReverse << endl;
        cout << "Sentence " << i << " : " << sub << endl;
        cout << "Enter the reverse of your sentence" << endl;
        while (cin >> reverse && reverse != "@"){
            reverseson += reverse + "";
        }
        Comparison(subReverse, reverseson);
        result = result.substr(result.find(".") + 2, result.length());
        i++;
    }
    cin.ignore();
    cin.get();
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
rektandlove
  • 39
  • 1
  • 6

2 Answers2

0

cin does not support input string with spaces.
So, to input a string containing spaces you need to use getline()

The correct syntax is std::getline(std::cin, input);

Tanuj Yadav
  • 1,259
  • 13
  • 21
  • if i do it with getline the problem is i cant read if user writes it in 1 row i mean hi how u doin bro. im fine how are you. @ cant read this – rektandlove Mar 30 '17 at 18:07
  • That's not true, you just need to change the delimiter http://stackoverflow.com/questions/7302996/changing-the-delimiter-for-cin-c – user975989 Mar 30 '17 at 19:54
0

Edited Your Code!

#include <iostream>
#include <string>
using namespace std;
void Comparison(string reverseReal, string reverseUser) {
    int wrongCount = 0;
    if (reverseReal.length() != reverseUser.length()) {
        cout << "These strings are not comparable." << endl;
    }
    else {
        for (int i = 0; i < reverseReal.length(); i++) {
            if (reverseReal.substr(i, 1) != reverseUser.substr(i, 1)) {
                wrongCount++;
            }
        }
        cout << "Number of mistakes: " << wrongCount << endl;
        if (wrongCount == 0){
            cout << "gj correct reverse" << endl;
        }
    }
}

string getReverse(string sentence) {

    string reverseResult = "";
    for (int i = sentence.length() - 1; i >= 0; i--) {
        reverseResult += sentence.substr(i, 1);
    }
    return reverseResult;
}

int findsnumber(string snumber)
{
    unsigned int sentences = 0, index = 0, length;
    string searchPattern = ".";
    length = snumber.length();
    while (index < length)
    {
        index = snumber.find(searchPattern, index);
        if (index != string::npos)
        {
            index += searchPattern.length();
            sentences++;
        }
    }
    return sentences;
}
// Gets the string until it finds a dot in it
string getOneSentence(string sentence) {

    while (sentence.find(".") != string::npos) {
        int dotFinder = 0;
        string sub = "";
        dotFinder = sentence.find(".");
        sub = sentence.substr(0, dotFinder);
        return sub;
    }
    return "";
}

int main() {

    //char input = '\0';
    string input = "";
    string result = "";
    string reverseReal = "";
    int i = 1;
    cout << "Welcome to my cancer c++ programme." << endl;
    cout << "Enter the paragraph with @ at the end to end it:" << endl;
    getline(cin, input,'@');
    result = input;
    int sentnumber = findsnumber(result);
    cin.clear();
    cout << "Number of sentences in the given string " << sentnumber << endl;

    while (result.find(".") != string::npos) {
        string sub = "";
        string subReverse = "";
        string reverse = "";
            string reverseson = "";
        cout << i << "/" << sentnumber << endl;
        sub = getOneSentence(result);
        subReverse = getReverse(sub);
        cout << "reverse of the sub " << subReverse << endl;
        cout << "Sentence " << i << " : " << sub << endl;
        cout << "Enter the reverse of your sentence" << endl;
        cin.clear();
        cin.ignore();
        getline(cin,reverse,'@');
        reverse.erase(reverse.end() - 1, reverse.end());
        reverseson = reverse;
        Comparison(subReverse, reverseson);
        result = result.substr(result.find(".") + 2, result.length());
        i++;
    }
    system("pause");
}
Mohammad Tayyab
  • 696
  • 4
  • 22