0

I have a problem that I am having trouble implementing in c++. I have summarized it below:

The problem: Implementing a linear search typing program to build a full string and return it.

  1. Iterate through an array containing each Alpha character,' ', '.', '!'
  2. Ask user if the current array element is the one they have in mind
  3. if user input is yes, concatenate that element to 'result' above, restart loop
  4. If user input is '!' output resulting string

My program is not producing any output, so I know I am doing something wrong, but I am very fresh to c++ and I am having trouble identifying potential bugs. The code produces no errors or warnings as is. I wasn't entirely sure of what sections of the code would be most important so I included it all.

Here is my code thus far:

#include <iostream>
#include <string>
using namespace std;
 
char linearSearch(char arr[], int n, char x)
{
    int i;
    for (i = 0; i < n; i++)
        if (x == 'y')
            return i;
        if (x == '!')
            return -1;
        if (i > n)
            i = 0;
      return 0;
}
 
// Driver code
int main()
{
    char arr[] = {
     ' ', '.', '!', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
   'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
   };
    char x;
    int n = sizeof(arr) / sizeof(arr[0]);
    int i;
    i = 0;
    cin >> x;
    string result_str;
    result_str = " ";
    while (i < n)
    {
      i++;
      cout << "Are you thinking of the letter:" << arr[i] << '?';
      result_str += arr[i];
    }
    // Function call
    char result = linearSearch(arr, n, x);
        cout << "You typed:" << result_str;
    return 0;
}

Does anyone have any comments or thoughts? I'd really appreciate it! It is important for me to understand where I am going wrong here. I have been trying to debug this for a day or two and I am feeling defeated, but determined to figure this out today!

Samosa
  • 1
  • 1
  • 1
    I am not sure about the return value. – drescherjm Oct 29 '20 at 18:17
  • ***My program is not producing any output, so I know I am doing something wrong,*** When this happens as a programmer your first thought should be to use a debugger to figure out where your algorithm is deviating from your design. – drescherjm Oct 29 '20 at 18:18
  • Thank you! I think I am still iffy on those so I will look into that. I appreciate the advice – Samosa Oct 29 '20 at 18:19
  • You also want to look at the scope of your for loop. `if (x == '!')` is outside the for loop. Indentation means nothing to the compiler in `c++` – drescherjm Oct 29 '20 at 18:19
  • I see. This is outside the scope by the ' ; ' I take it? Thanks for all this. It's very helpful. – Samosa Oct 29 '20 at 18:22
  • After you fix the scope I have no idea why you are doing this: `if (i > n) i = 0;`. `i` will never be greater than `n` – drescherjm Oct 29 '20 at 18:27
  • Well that's good to know. Here, my intention was to restart at the beginning if after reaching the end of the array (sizeOfArray = n) there wasn't any user input (either 'y' or '!'). Assuming that is probably not correct, though, and also out of the scope of the loop. I thought I needed a way to restart if out of letters. – Samosa Oct 29 '20 at 18:34

0 Answers0