Good afternoon,

I have been working on a simple problem for a long time because I am really not sure how to approach it.
I need a user to type in a sentence at a command prompt, after that I trim the String and place it into an array to make it all lower case but the first letter of that sentence, that is why I take the String^ and place it into the array, I thought it would be the cleanest approach.

Example of the command prompt result needed.
The user types:
this IS a TEST

Once processed the output should be:
This is a test

My code:

#include "stdafx.h"

using namespace System;

int main(array<System::String ^> ^args)
{
    // User input
    Console::WriteLine(L"Please enter a sentence: ");
	String ^ userSentence;
    userSentence = Console::ReadLine();

	//Triming 
	Console::WriteLine();
	String^ newStr = userSentence->Trim();
	Console::WriteLine("Sentence trimmed applied:");
    Console::WriteLine(newStr);

    //array section
	array<String^>^ data;
	data = gcnew array<String^>{newStr};
	Console::WriteLine("from array:");
	//loop through each character and make it lower-case.
	int i =0;
	for(int i = 0; data[i] != '\0'; i++)
		{
			data[i]->ToLower(data[i]);
		}
            data[0]->ToUpper(data[0]);
            Console::WriteLine(data);

	return 0;
}

However things don't workout the way I would hope and I get this error message on line 24:
error C2446: '!=' : no conversion from 'int' to 'System:: String ^'

I see that my data array content is trying to be used as an "int" but I don't how to fix this?

I would greatly appreciate your help.

John

Your compiler is right:

>data != '\0'
Data is an array of System::String. '\0' is a character literal. I'm not sure what you were trying to do here (perhaps compare data to nullptr?), but you could just as easily use the Length property of the managed array type:

for ( int i = 0; i < data->Length; i++ ) {

Also note that your calls to ToUpper and ToLower don't do diddly. Managed strings in C++/CLI are immutable. You need to save the modified copy somewhere. Currently you're just throwing it away.

Thank you Naru, the Lenght worked well.
My logic was to place the String^ into the array, so I could go through the array and perform a ToLower case on all the character but the first one which I am planning to do an ToUpper case. to have this result.

User type: this is a TEst.
Once it goes through the code, my output is: This is a test.

Thanks for the note, I will be working on that now

John

Then you want an array of char, not an array of String. The String::ToLower() method works on the whole string, not a single character within the string. You'd probably be best served by looking up the String::ToCharArray() method, and the Char structure has a ToLower() method.

Thank you, I will be looking at the "String::ToCharArray() method" right now for indeed what I have so far is not working to give me the result I am looking for.
Thanks for the tips

This article has been dead for over six months. Start a new discussion instead.