I'm working on a simple program in which the user types a sentence, the program iterates through the string that the sentence in stored in and alternates each letter's case, and then puts the formatted text into the clipboard.

I know exactly how to allow input and change case/check if characters are alphabetical and therefore able to change case. What I would like to know is how to get the clipboard part working.

I have been able to get the basic code for the clipboard to work, however, if I input a string like "Hello world", it cuts off anything past a space. The only time it has succeeded is when I embedded "Hello World" in the code as a const char variable WITHOUT allowing input.

I understand how to use the clipboard code, however in all the permutations and different ways of passing a user-inputted string, it still cuts off the characters after the space. I am using cin for the input. In short, please provide me an example that gets inputted text (please, no getch() or _getch()), and saves it into the clipboard and if possible, describe why I am having the problem.

I honestly am trying to learn about why it works that way, but programming is a notoriously difficult field to find good walkthroughs for.

Recommended Answers

All 4 Replies

You're probably using cin like cin >> s . That method stops reading at whitespace, so s is only populated with the first word from input. If that's the case then it has nothing to do with how the code copies to the clipboard, and you can fix it by reading a line with getline():

if (getline(cin, s))
    // Copy s to the clipboard

thank you very much for your help! I got the input working, but before I noticed your reply, I fooled around with the code too much and gave up. Now I have a piece of code that executes perfectly, but doesn't save properly to the clipboard. I remembered how I got the major type incompatibilities solved, but it would appear that I have the actual saving of the data incorrectly set up.

#include <conio.h>
#include <stdio.h>
#include <cstdlib>
#include <Windows.h>
#include <winuser.h>
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdio>
#include <math.h>
#include <cmath>
#include <ctype.h>

void toClipboard (std::string &text);
void scrambler (std::string &text);

int main () {

	using namespace std;
	HWND hwnd = GetDesktopWindow();
	string AString;
	std::cout << "Please enter sentence: " << endl; 
	if (getline(cin, AString)) {
	return 0; 

void scrambler(std::string &text) {
	bool isCapsConversion = true;
	for (int a=0; a < text.length(); a++) {
			if (isalpha(text[a]) && isCapsConversion == true) {
				text[a] = toupper(text[a]);
				isCapsConversion = false;
			else if (isalpha(text[a])) {
				text[a] = tolower(text[a]);
				isCapsConversion = true;
	std::cout << text << std::endl;
	std::cout << std::endl << std::endl;

void toClipboard(std::string &text) {
		const int nBytes = text.length()+1;
		const char* string = text.c_str();
		HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, nBytes);
		LPVOID pMem = GlobalLock(hMem);
		memcpy(pMem, string, nBytes);
		std::cout << "Succesfully completed clipboard code" << std::endl << "Check if code worked" << std::endl;

Where am I going wrong? I am genuinely curious, because I would like to understand the code I am using a little better. I'm fairly certain it lays either at the HWND declaration, or with the arguments I have passed in the clipboard code. I doubt that I still need that HWND declaration, but it was in my previous code, so there's a possibility that it is necessary.

You never change the clipboard contents. You only copy the text into some global memory, which has no association with a clipboard. Look at SetClipboardData function.

Thanks again! I was avoiding that setClipboardData because it didn't look like the code I had remaining (after fooling around trying to fix the first problem before coming here). I should be less stubborn and a little more willing to try code, even when it doesn't look like what I've tried already. I should be facepalming myself right now for ignoring snippets with a function so aptly named.

Anyways, I learnt a little bit more about programming, and I'm sure that small mischievious programs like this one will help me learn programming 'vocabulary'. Just like learning another language, it's very difficult to learn anything more than syntax without practicing.

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.