Can't read in multiple lines

I'm trying to read in a number which is how many lines of input I have (each line is three strings), but when I press enter to input the second line (for example:
3
a b c
d e f
)

it ends before I can type d e f.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using namespace std;

int main(){
int inputs;
    cin>> inputs;
 vector <string> names;
    vector <string> status;
    vector <string> travel;
    char *travels[100];
     for (int i=0;i< inputs;i++){
        //name, status, itinenary
        string a, b, c;
        cin>>a>>b>>c;
names.push_back(a);
        status.push_back(b);
        travel.push_back(c);
        //get rid of the dashes and put it in an array
     travels[i] = strtok(travels[i], "-");
    cout<<"got here"<<endl; //this line isn't printed
}
cout<< "Program finished"; //this line is also not output
return 0;
}
Last edited on
The same program with only the relevant bits:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

int main(){
  int inputs;
  std::cin>> inputs;
  char* travels[100]; // 100 uninitialized pointers

  for ( int i=0; i< inputs; i++ ) {
    travels[i] = strtok(travels[i], "-"); // dereferences uninitialized pointer
  }
}

(The three vectors had nothing to do with the 'travels'.)
I tried adding travels[i] = NULL (or travels[i] = nullptr) before the strtok line, but that's not working... why?
What do you mean by "not working"?

What do you expect to achieve by effectively writing:
strtok( nullptr, "-" )
Hello GonlyG,

Here is the answer to your problem
1
2
3
4
5
6
7
8
9
10
 _                                   
                                   
                                   
                                   
                                   
                                   
                                   
                                   
                                   
                                   

I wish I could make the underscore/cursor blink like on the screen, but like your program it is waiting for input, but you have no idea what to type in.

Sometimes mixing C and C++ code does not work well. I would like to tell you that you are missing the <string.h> header file, but you did not include your header files, so I do not know.

In the short time I did spend with the program I did come up with this:
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 <iomanip>
#include <string>
#include <vector>

#include <algorithm>

int main()
{
	int inputs;
	std::vector <std::string> names;
	std::vector <std::string> status;
	std::vector <std::string> travel;

	//std::string travels; // <--- Not needed.

	std::cout << "\n Enter number of lines to input: ";
	std::cin >> inputs;

	std::cout << "\n Enter Name, Status, Itinerary";
	std::cout << "\n Example (Andy, sick abc-def-ghi-jkl ...): " << std::endl;

	for (int i = 0; i < inputs; i++)
	{
		//name, status, itinerary
		std::string name, condition, itinerary;

		std::cout << "\n Enter person " << i + 1 << ". : ";
		std::cin >> name >> condition >> itinerary;
		names.push_back(name);
		status.push_back(condition);
		travel.push_back(itinerary);

		//get rid of the dashes and put it in an array
		//travels[i] = strtok(travels[i], "-");

		replace(travel[i].begin(), travel[i].end(), '-', ' '); // <--- include <algorithm> header file,

		//std::cout << "got here" << std::endl; //this line isn't printed
	}

        // <--- Added for testing.
	for (int lc = 0; lc < inputs; lc++)
	{
		std::cout << "\n " << names[lc]
			<< ' ' << status[lc]
			<< ' ' << travel[lc] << std::endl;

	}

	std::cout << "\n Program finished";

	return 0;
}

This is one possible solution.

I am think of creating a struct and only needing one vector of structs to hold all the information. I will play with that shortly.

Tip: use better names for your variables not "a", "b", "c". See above code.

Hope that helps,

Andy
Topic archived. No new replies allowed.