954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

toupper() stopping when spaces are used

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!

hamby
Newbie Poster
11 posts since Jan 2012
Reputation Points: 6
Solved Threads: 0
 

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

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

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"

hamby
Newbie Poster
11 posts since Jan 2012
Reputation Points: 6
Solved Threads: 0
 

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

NathanOliver
Veteran Poster
1,084 posts since Apr 2009
Reputation Points: 215
Solved Threads: 189
 

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;
}
Mr_Null_andVoid
Newbie Poster
8 posts since Dec 2011
Reputation Points: 10
Solved Threads: 5
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: