Hi guys, I'm writing a small piece of code that will allow the user to input a sentance, and will return the sentance in all uppercase characters. The problem is, as soon as a "space" character is entered the program considers the input to be finished. I don't understand why. At first I thought that maybe it would be the space symbol causing problems so I wrote the code to disregard the character if it was a space symbol. This still didn't help. Also, if I input spaces before writing anything else, the program just skips the spaces, and returns the input after in uppercase.
This is my original code:

string upper(string sentance){

    string s = sentance;

    for (int i=0; i < sentance.size(); i++)



            {s[i] = toupper(sentance[i]);}


    return s;
}

Thanks for the help!

WaltP commented: Please note the EDIT BUTTON on the left... -4

Recommended Answers

All 4 Replies

It's because the input you are probably using, by definition, stops inputting at a SPACE. Your error is not in the code posted.

Thanks, but how do I allow it use include a space as an input?
For example, if I input:
"Hello world"
The program only returns:
"HELLO"

if you want to get a whole of text uasing cin you need to use getline()

It works perfectly on VC++ 6..

#include <string>
#include <iostream>
using namespace std ;

string upper(string sentance)
{

	string s = sentance;	
	for (int i=0; i < sentance.size(); i++)
	{
		s[i] = toupper(sentance[i]);
	}


	return s;
}


int main(void)
{
  string  str("nnmmnnmmn  smallllll sm");
  cout<<upper(str)<<"  \n";
   
  return 0;
}
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.