0

The main task of this program is to find words using char array with 2 user chosen letters.This completes the task but I also need to be able to repeat the program and when i do it skips array entry, I've tried using fflush(NULL) also I've tried fflush(stdin) after changing getline() to scanf() but none of them worked, if IDE changes anything I am using visual studio 2019.

#include <iostream>
#include <cmath>
#include <stdio.h>
#include <conio.h>

using namespace std;


int arrLength(char mas[]);

void upperCaseArr(char mas[]);

void returnWords(char mas[], char x);


int main()
{
    int rep = 1;

    while (rep == 1) {        
        system("cls");
        char mas[256], x, b;   
        int i = 0;

        cout << "write a sentence!\n\n";

        fflush(NULL);
        cin.getline(mas, 256);     
        upperCaseArr(mas);

        cout << "\n\n\n";

        cout << "enter a letter! if two of these letters will be in a word then it'll be written out.\n";
        cin >> x;
        x -= 32;                          
        cout << "\n\n\n";

        cout << "words that have 2 your letters:\n";
        returnWords( mas , x);    

        cout << "\n\n\nDo you want to repeat? (Y/N)\n";

        int x1 = 0;
        while (x1 == 0) {           

            cin >> b;

            if (b == 'Y' || b == 'y') {
                rep = 1;
                x1 = 1;
            }
            else if (b == 'N' || b == 'n') {
                rep = 0;
                x1 = 1;
            }
            else {
                cout << "Invalid operator!" << endl;
            }
        }

    }
}

int arrLength(char mas[]) {                  
    int x = 0;                             
    for (int i = 0; i <= 256; i++) {
        if (mas[i] == '\0') { break; }
        else { x++; }
    }
    return x;
}

void upperCaseArr(char mas[]) {             
    int length = arrLength(mas);
    for (int i = 0; i < length; i++) {
        int c = mas[i];
        if (islower(c)) {
            mas[i] = toupper(c);
        }
    }
};

void returnWords(char mas[], char x) {
    int length = arrLength(mas), ssiz = 0, sp = 0; 
    int space[100];

    for (int i = 0; i <= length; i++) {  
        char m = mas[i];
        if (m == ' ') { space[sp] = i + 1; sp++; }
    };

    for (int i = sp; i < 100; i++) { space[i] = -1; }  /
    for (int i = 0; i < 100; i++) { if (space[i] != -1) { ssiz++; } }
    if (ssiz == 0) { ssiz = 1; space[sp] = length; }                 
    if (mas[length] != ' ') { ssiz += 1; space[sp] = length + 1;}      
    for (int i = 0; i < ssiz; i++) {           
        int sk = 0;
        int i1 = 0, i2 = 0;

        if (i > 0) { i1 = space[i] - (space[i] - space[i - 1]); }  

        for (i1; i1 < space[i]; i1++) {     
            char m = mas[i1];
            if (x == m) { sk++; };
        }

        if (i > 0) { i2 = space[i] - (space[i] - space[i - 1]); } 

        if (sk == 2) {
            for (i2; i2 < space[i]; i2++) {        
                cout << mas[i2];
            }
        }
    }
};
kaylum
  • 13,833
  • 2
  • 22
  • 31
Elgars
  • 23
  • 3
  • I'm not so good with C++'s `cin`, but my guess is that after reading the answer to the repetition question (Y/N) the rest of the line is still to be read and the next call to `getline` will read the rest of the empty line. It's best not to mix line-wise and character-wise input. Use `getline` throughout and scan the read lines for the desired input. That also matches the interactive nature of your input: Program asks, user answers _and presses Enter_. – M Oehm Mar 08 '20 at 09:47
  • Fixed it by adding cin.ignore(256, '\n') right after the Y/N program repeat loop. – Elgars Mar 08 '20 at 10:15

0 Answers0