Hi ppl,
I've written a simple program with strings. this just recives the name and last name of a person, and then print it.
i had a few problems with this: at first, it didnt let me define a variable as "string", it gave this error:

C2679: binary '>>': no operator defined wich takes a right-hand operand of type 'class std::basic_string <char, struct std::char_traits <char>, class std::allocator <char> >'

and dont know what it is, so i had to change those variables to char.
another problem i had was with the

strcat (name, last_name);

. i should print the result of it at the end, but tried and didnt work (actually did, but gave as a result a number, so its the same as it didnt work)
what can i do with both problems??

tnks!!

gispe!!

#include "stdafx.h"
#include <iostream>
#include <string.h>


int main(int argc, char* argv[])
{
	using namespace std;

	char name [20], last_name [20];

	cout << " Type the name: ";
	cin >> name;
	cout << "\n";

	cout << " Type the last name: ";
	cin >> last_name;
	cout << "\n";

	strcat (name, last_name);

	cout << " The name you entered is: " << name << " " << last_name << "\n";

	

	return 0;
}

std::string is declared in the header file <string>, not <string.h>. The error you reported is because you failed to include <string> in your program.

strcat: You failed to declare name large enough to hold both last name and first name, so if you enter last name with 18 characters and first name with more than 2 characters strcat() will scribble all over memory, causeing great harm to your program.

The best solution is to learn to use std::string class in your c++ programs, unless of course the intent is to learn character arrays.

1. Where is a code snippet with this diagnostic message?

2. Presented source is obviously incorrect but it can't print a number. Why strcat? You print name and last_name but one line before strcat appends last_name to name...

Never use cin >> char_array ! Type a long (>19 chars) name and your program overwrite its memory. Use

#include <string>
#include <iostream>
using namespace std;
...
string name;
...
getline(name,cin);
...

if you accept names with blanks (why not? Eric Maria and so on).

hi!
im back here,
ive modified the code so it "work"

#include "stdafx.h"
#include <iostream>
#include <string>


int main(int argc, char* argv[])
{
	using namespace std;

	char name [20], last_name [20], full_name[40];

	cout << " Type the name: ";
	cin >> name;
	cout << "\n";

	cout << " Type the last name: ";
	cin >> last_name;
	cout << "\n";

	strcat (full_name, name);
	strcat (full_name, last_name);

	cout << " The name you entered is: " << full_name << "\n" << "\n";

	
	return 0;
}

now problem is that, ive created and set a variable with a max caracter. but when try to show this variable, it isnt shown.
in the file attached, there is how its printed on screen

1. You didn't listen to one of the above posters. Don't do this:

cin >> name;

If I type a name that's larger than 20 characters your program will crash.

2. Unless this is for a class and your instructor says you must use character arrays, use std::strings. They're so much easier to work with.

3. Are you sure the output in the image came from the program above? Nothing in the program should cause "Press any key to continue" to appear (unless it's something to do with stdafx.h, of which I know little).

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.