Hi again.

I'm having two issues with this code, first of then strlen only counts the characters in the first word of the line I put into console. Then the second one is that the console takes the second word put into the console in to next "cin" and does not ofcourse then stop after asking for the second text since it has it already from the first input.

Ok I guess I can solve the input problem with clearing the memory between lines but what about the strlen issue then.

Here is the current code.

#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
#include <cstring>
#include <cstdlib>
#include <iostream>
using namespace std;

int count (char[]);
void strCopy (char[]);

int _tmain(int argc, _TCHAR* argv[])
	char pszString[256];
	char pszSource[256];
	int size = 0;

	cout << "Write a text that is under 256 characters = pszString" << endl;

	cin >> pszString;

	size = count(pszString);

	cout << "Your text is " << size << " characters long." << endl;

	cout << "Write another text that is under 256 characters = pszSource" << endl;

	cin >> pszSource;

	return 0;

int count ( char pszString[] )
	int size;
	size = strlen(pszString);
	return (size);

void strCopy(char pszSource[])
	char pszDestination[256];
	strcpy (pszDestination,pszSource);
	cout << "The text in pszDestination is now " << pszDestination << " which should be the same you entered for pszSource" << endl;


Please advice.

Recommended Answers

All 2 Replies

>>first of then strlen only counts the characters in the first word of the line I put into console
The problem is your code, not strlen(). The >> operator stops accepting data from the keyboard when it encounters the first space or tab. All the rest of what you typed is still in the keyboard buffer. If you want all the spaces too then you have to use cin.getline() with character arrays or std::getline() with std::string.

Correcting that will also probably fix the other problem.

Thanks for the cin.getline() comment it was right. But I needed to redo anything from scratch since I misunderstood the assignment, but the cin.getline() is still needed.

Be a part of the DaniWeb community

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