Strings/search for chars/separator

1
2
3
4
5
6
7
8
9
Write a program that allows a user to type in tabular data similar to a CSV file, but instead of
using commas a separator, you should try to detect valid separators. First let the user type in
the lines of tabular data. Then detect possible separator characters by looking through the input
for non-number, non-letter, non-space characters. Find all of these characters that appear on
every single line, and display the user these characters to ask which one to use. For example, if
you see input like this:
Alex Allain, webmaster@cprogramming.com
John Smith, john@nowhere.com
You should prompt the user to choose between comma, at sign, and period for the separator. 


Hello guys, I am having a hard time interpreting the exercise, Should I for example only use normal "cin>>" and not getline, for the inputs? Cause my job here seems to be to "find" the separator?

Also to find a delimiter, should I have a very long string of
string letter = "AaBbCcDdFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz123456789 ";
and then iterate through every input, & if a char is not of any char in "letter string", it will count as a possible separator? It sounds like a very bad idea I guess.

I am also unsure how the program should even work, should it cout out all strings together, without the delimiter? Psuedocode or code is very welcome.

Thanks for reading!
Since the space can be part of the input you best use getline to read the input.
You can read the input in a loop and process each line.
I would do it like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main () 
{
  std::string input, delimeters;
  char delim = 0;

  while (std::getline (std::cin, input))
  {
    /* 
      process the input - i.e. add delimeters you find 
      to the delimeter string
    */
  }
  std::cout << "These are the possible delimeters: " << delimeters << '\n';
  std::cout << "Choose one: ";
  std::cin.get (delim);
  system ("pause");
  return 0;
}
Thank you, I've been away, but I took inspiration from your post while creating it now. I guess this would do for an answer? Or was it poorly made? I am not very experienced so I chose the tools I knew, but it does work if I understood the exercise correctly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <string>

using namespace std;

int main ()
{
  char deli;
  string input, delimiters, tmp = "";
  int k = 0, counter = 0, choice = 0, b = 0;
  char delimiter = ' ';

  while (b < 5){
        cout << "Enter input #" << b+1 << ": ";
        getline(cin,input);
        tmp += input;

  for (int i = 0; i<input.size(); ++i)
  {
      if ((!(input[i] >= '0' && input[i] <= '9')) && (!isalpha(input[i]) && (input[i] != ' '))){

        for (int o = 0; o<delimiters.size(); ++o){
        if (delimiters[o] == input[i])
        ++counter;
        }
            if (counter == 0)
            delimiters += input[i];
        }
    }
  counter = 0;
  //cout << tmp << "\n";

  while (delimiters.size() > 0 && b == 4){
        cout << "Found delimiters: [" << delimiters << "] choose one from 0-" << delimiters.size()-1 << ":\n";
        cin >> choice;
        if(choice >= 0 && choice < delimiters.size()){
  delimiter = delimiters[choice];
  cout << "You chose: " << delimiter << "\n";
    break;
    }
  }
++b;
}

for (string::iterator itr = tmp.begin(), End = tmp.end(); itr != End; ++itr)
{
    if (*itr == delimiter)
    {
        cout << "\n";
    }
    else
    cout << *itr;
}
}

Console;
https://i.gyazo.com/cc83c8ed31e666be9fa63d3f7b439a19.png
Last edited on
Topic archived. No new replies allowed.