Command to get a line from the user

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
int get_command_line (char * sa) {
  char * s;
  char * l = fgets(s, 300*5, stdin);
  int i = 0;
  int j;
  int n;
  while (i<5){
    j = 0;
    while (j < 300) {
      sa[i][j] = s[n];
      if (s[n] == '\"') {
        while (s[n]!= '\"' && j < 300) {
          sa[i][j] = s[n];
          if (s[n] != '\"'){
            j+=1;
            n+=1;
          }
        }
      } else if (c == '\n') {
        return i;
      } else if (c == ' ') {
        i += 1;
        break;
      } else {
        sa[i][j] = c;
        j += 1;
        while(s[n] != ' ') n += 1;
      }
    }
  }
  return 5;
}


The aim is to have the function return the number of arguments made after assigning each of the arguments to a c string stored in an array of five pointers. This is how I declare this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 
  int main(void) 
  int n;
  char s0[300];
  char s1[300];
  char s2[300];
  char s3[300];
  char s4[300];
  // There are now 5 strings that can accept user input that is less than 300            chars.
  char * sarray[5];
  sarray[0] = s0;
  sarray[1] = s1;
  sarray[2] = s2;
  sarray[3] = s3;
  sarray[4] = s4;
  // These strings are now stored in an array so that multiple inputs may be taken.

  n = get_command_line(sarray);
  print (sarray, n);

  return;

}



Any help given would be greatly appreciated.
1
2
std::string line;
std::getline(std::cin, line);
Topic archived. No new replies allowed.