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!

Edited 3 Years Ago by Dani: Formatting fixed

Comments
Please note the EDIT BUTTON on the left...

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"

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;
}
This article has been dead for over six months. Start a new discussion instead.