why vs code can't read my cin

Hello, so I was trying to figure out whether I could use a vector to store a dialogue but when compiling, vs code keeps telling me this: ld returned 1 exit status. I do not really understand that error but what I see is that vs code recognizes "cin", but not the ">>" that follows. Does anyone have an idea how I can solve this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <vector>
using namespace std;

int main()
{

  vector<string> dialogue;

  cout << "Hello";
  cin >> dialogue;

  cout << "How are you?";
  cin >> dialogue;

} 
you are treating dialogue like a string.
it is not a string.

you should say
string tmp;
cin >> tmp;
dialogue.push_back(tmp);
cout >> dialogue[0];

for starters. a vector is a container of things, but not the thing itself. Why do you want/need a container at all? It would flat out work as-is if you change dialogue to a string.
Last edited on
Maybe something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>

int main() {
	const std::vector prompts { "Hello", "How are you" };

	std::vector<std::string> dialogue;

	for (const auto& p : prompts) {
		std::string d;

		std::cout << p << ": ";
		std::getline(std::cin, d);
		dialogue.push_back(std::move(d));
	}

	std::cout << "\nThe dialogue was:\n";

	for (size_t num_dial {}, minDial { std::min(prompts.size(), dialogue.size()) }; num_dial < minDial; ++num_dial)
		std::cout << prompts[num_dial] << '\n' << dialogue[num_dial] << '\n';
}



Hello: hello to you
How are you: I'm well thanks

The dialogue was:
Hello
hello to you
How are you
I'm well thanks

Well I've just started programming but I was trying to create an interaction between the program and the user so that for example when the program says "hello", the user would have to click again to see the rest instead of having all the output at the same time

But my real issue here is that even when compiling the most simple code, vs code displays "ld returned 1 exit status" and even after searching on the internet I can't seem to understand what it means and how to solve it
Can you show us the code that gives you this exit status? Also, for console programming, you wont be able to click in the console to make it do anything, you'll have to use enter to make it continue if you give it input using cin. Not sure if thats what you meant or not but I just thought i would make that distinction.
If you using Windows, use VS rather than VS code for c++.
https://visualstudio.microsoft.com/vs/community/

Note that you have to configure the installer to install C++. it's not included by default.
As for your program is this what you wanted it to do?

Every time you press enter it should display the contents of the vector one at a time.

Also I second seeplus's recommendation of using regular Visual Studio as well.

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
#include <iostream>
#include <string>
#include <vector>

using std::cout;
using std::cin;
using std::vector;
using std::string;

int main()
{

	vector<string> dialogue;

	cout << "Hello, Enter some text and when you're done type in 'stop'.\n";

	string input{ };

	while (input != "stop")
	{
		cout << '>';
		getline(cin, input);
		dialogue.push_back(input);
	}

	cout << '\n';

	for (const auto& i : dialogue)
	{
		cout << i;
		cin.get();
	}

	return 0;
}
Last edited on
ld returned exit 1 is a cryptic message for sure. You will want to start searching the web for things like this, but in short this message tells you that building your program failed due to an error -- in your original code it was because the code is wrong: vector does not have a stream overload (>> and << for files, cout, cin, etc are stream operators).
Let me break it down for you.

when we try to run the code in the first post you made, we get a spew of stuff like this:

main.cpp:11:7: error: invalid operands to binary expression ('istream' (aka 'basic_istream<char>') and 'vector<string>' (aka 'vector<basic_string<char, char_traits<char>, allocator<char>>>'))
  cin >> dialogue;
  ~~~ ^  ~~~~~~~~
/root/emsdk/upstream/emscripten/cache/sysroot/include/c++/v1/cstddef:153:3: note: candidate function template not viable: no known conversion from 'istream' (aka 'basic_istream<char>') to 'byte' for 1st argument
  operator>> (byte  __lhs, _Integer __shift) noexcept
  ^
/root/emsdk/upstream/emscripten/cache/sysroot/include/c++/v1/istream:670:1: note: candidate function template not viable: no known conversion from 'vector<string>' (aka 'vector<basic_string<char, char_traits<char>, allocator<char>>>') to 'unsigned char &' for 2nd argument
operator>>(basic_istream<char, _Traits>& __is, unsigned char& __c)
^
/root/emsdk/upstream/emscripten/cache/sysroot/include/c++/v1/istream:678:1: note: candidate function template not viable: no known conversion from 'vector<string>' (aka 'vector<basic_string<char, char_traits<char>, allocator<char>>>') to 'signed char &' for 2nd argument
operator>>(basic_istream<char, _Traits>& __is, signed char& __c)
^
/root/emsdk/upstream/emscripten/cache/sysroot/include/c++/v1/istream:635:1: note: candidate template ignored: deduced conflicting types for parameter '_CharT' ('char' vs. 'vector<string>' (aka 'vector<basic_string<char, char_traits<char>, allocator<char>>>'))
operator>>(basic_istream<_CharT, _Traits>& __is, _CharT& __c)
^
/root/emsdk/upstream/emscripten/cache/sysroot/include/c++/v1/__random/uniform_int_distribution.h:269:1: note: candidate template ignored: could not match 'uniform_int_distribution' against 'vector'
operator>>(basic_istream<_CharT, _Traits>& __is,
^
/root/emsdk/upstream/emscripten/cache/sysroot/include/c++/v1/istream:578:1: note: candidate template ignored: could not match '_CharT[_Np]' against 'vector<string>' (aka 'vector<basic_string<char, char_traits<char>, allocator<char>>>')
operator>>(basic_istream<_CharT, _Traits>& __is, _CharT (&__buf)[_Np])
^


but when dealing with syntax errors, one error often triggers many, many messages!
What is best, 99.9 % of the time, is to fix only the topmost error and try again. If you are working on a giant project that takes a very long time to compile, that rule can be changed to deal with the issue, but for now, focus on the topmost error.

So, we see this at the top:
main.cpp:11:7: error: invalid operands to binary expression ('istream' (aka 'basic_istream<char>') and 'vector<string>' (aka 'vector<basic_string<char, char_traits<char>, allocator<char>>>'))
cin >> dialogue;

that tells me (it takes a while to be able to read this junk, you will learn it in time) that your file main.cpp has a problem on line 11, column 7. The problem is that the operands are invalid. Ok, there is only one operation here: the >> operator (its an operator same a + and -, in c++ terms) is not getting the parameter it wants. Which cycles back to what I already told you: that vectors do not directly work with cin and cout. NONE of the C++ containers work directly with I/O streams. Its just something you should probably learn, but if you wanted to figure it out, you can look up vector in a reference and see what it supports: note that operator << and >> are not listed.

Thusly armed (the error is the stream operator and that isn't supported by vector) you now know that you need to do something else on that line to accomplish what you want to do.

The ld exit status is NOT very interesting, note. It tells you that your program did not build, and nothing more. exit of 0 means success in c++ and operating systems generally (hence the return 0 in main if things went well).

finally, looking at vector here:
https://cplusplus.com/reference/vector/vector/
note that it lists 'operator =' but not 'operator <<' or 'operator >>'
now look at string:
https://cplusplus.com/reference/string/string/
down in the 'non member function overloads' section, you see this:
operator>> Extract string from stream (function)
operator<< Insert string into stream (function)

so you can use >> and << with a string variable, but not a vector variable.
Last edited on
the program says "hello", the user would have to click again to see the rest instead of having all the output at the same time


Well for something like this, possibly:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
include <iostream>
#include <sstream>
#include <string>

int main() {
	const std::string text { "We hold these truths to be self-evident, "
		"that all men are created equal, that they are endowed by their Creator with certain unalienable Rights, "
		"that among these are Life, Liberty and the pursuit of Happiness." };

	std::istringstream iss(text);

	std::cout << "After each word has displayed, press <CR> for the next:\n";

	for (std::string w; iss >> w; std::cin.ignore(1000, '\n'))
		std::cout << w;
}

The build process has basically two steps:
1. Compiler generates object files from source code
2. Linker generates executable from object files

The OP code fails to compile into object file. If that happens, then the linker should not run at all.

If VS-code did run linker despite earlier failure(s), then it behaves differently from the build systems that I have seen.
Topic archived. No new replies allowed.