Hello everyone I'm new here! :icon_smile:
I'm also new to C++, and I'm having a little bit of trouble with a code of mine.

// function example
#include <iostream>
#include <string>
using namespace std;

int addition (int a, int b)
{
int r;
r=a+b;
return (r);
}

int main ()
{
string mystring2;
string mystr;
int z;
z = addition (5,3);
cout << "Please enter your name:";
cin >> mystr;
cout << "The result is " << z << mystr << "isn't that neat!";
cout << "I'm amazing, agreed?";
getline (cin, mystring2);
cout << mystring2 << "?!";
return 0;
}

In which the line:

getline (cin, mystring2);

Does not work.

I'm not sure what I'm doing wrong and I'm open to suggestions.

Additionally, when I change the code like this:

// function example
#include <iostream>
#include <string>
using namespace std;

int addition (int a, int b)
{
int r;
r=a+b;
return (r);
}

int main ()
{
string mystring2;
string mystr;
int z;
z = addition (5,3);
cout << "Please enter your name:";
cin >> mystr;
cout << "The result is " << z << " " << mystr << ", isn't that neat!";
cout << "I'm amazing, agreed?";
cin >> mystring2;
cout << mystring2 << "?!";
return 0;
}

It works fine.

I've been using this tutorial http://www.cplusplus.com/doc/tutorial/introduction.html perhaps someone can suggest a better one if this one isn't very good?

Recommended Answers

All 3 Replies

I'm so sorry for not reading the read me :(
Next time I'll use the code tags, I apologize, I was pretty anxious.

Well, you didn't really specify your problem, but this might solve your problem.
:)

// function example
#include <iostream>
#include <string>
using namespace std;

int addition (int a, int b)
{
	int r;
	r=a+b;
	return (r);
}

int main ()
{
	string mystring2;
	string mystr;
	int z;
	z = addition (5,3);
	cout << "Please enter your name:";
	cin >> mystr;
	cout << "The result is " << z << mystr << "isn't that neat!";
	cout << "I'm amazing, agreed?";
	[B]cin.ignore();[/B]
	getline (cin, mystring2);
	cout << mystring2 << "?!";
	[B]cin.ignore();[/B]
	return 0;
}

Worked perfectly :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.