5 Replies - 1022 Views - Last Post: 19 July 2012 - 03:55 PM Rate Topic: -----

#1 zachkt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 19-July 12

String characters in array. Convert lowercase to uppercase

Posted 19 July 2012 - 03:17 PM

So i have to create a program which lets the user enter a string into a character array. It should then convert all the lowercase letters to uppercase. If a character is already uppercase, or is not a letter, it should be left alone. What is wrong with my code? I am just too confused now to figure out how this can be fixed.

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

int main()
{

	// define variables
	const int string = 8;
	char letters [string];
	char capletters[string];
	int i = 0;

	// get user input string and store it in a character array
	cout << "Please enter 8 letters." << endl;
	cin.getline(letters, string);

	// go through the array
	// if an element is a lowercase letter, convert it to its uppercase equivalent

	for (int i = 0; i < string; i++) // checking if character is lowercase.
	{
		cout << letters[i] << "\t";

	if(letters[i] >= 97 && letters[i] <= 122) 
	{
		letters[i] - 32; // convert to uppercase.
		cout << capletters[i];
	}

	}



	return 0;
}




Is This A Good Question/Topic? 0
  • +

Replies To: String characters in array. Convert lowercase to uppercase

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1916
  • Posts: 5,731
  • Joined: 05-May 12

Re: String characters in array. Convert lowercase to uppercase

Posted 19 July 2012 - 03:24 PM

On line 27, you are trying to do a conversion, but not storing it anywhere.

On line 28, you are outputing some uninitialized value from your capletters array.

Do you even need the the capletters array?
Was This Post Helpful? 0
  • +
  • -

#3 zachkt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 19-July 12

Re: String characters in array. Convert lowercase to uppercase

Posted 19 July 2012 - 03:30 PM

Does that look right?

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

int main()
{

	// define variables
	const int string = 8;
	char letters [string];
	char capletters[string];
	int i = 0;

	// get user input string and store it in a character array
	cout << "Please enter 8 letters." << endl;
	cin.getline(letters, string);

	// go through the array
	// if an element is a lowercase letter, convert it to its uppercase equivalent

	for (int i = 0; i < string; i++) // checking if character is lowercase.
	{
		cout << letters[i] << "\t";

	if(letters[i] >= 97 && letters[i] <= 122) 
	{
		capletters[string] = letters[i] - 32; // convert to uppercase.
		cout << capletters[string];
	}

	}



	return 0;
}





Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1916
  • Posts: 5,731
  • Joined: 05-May 12

Re: String characters in array. Convert lowercase to uppercase

Posted 19 July 2012 - 03:42 PM

That sort of functions. Have you tried running it? What would you change in its behavior to make it act more like what your assignment requires?

Actually on closer look, it looks like you have a buffer overrun there. You should fix that first. Consider what index you are passing into the capletters array?

For later, also consider, do you really need that capletters array?

This post has been edited by Skydiver: 19 July 2012 - 03:44 PM

Was This Post Helpful? 0
  • +
  • -

#5 zachkt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 19-July 12

Re: String characters in array. Convert lowercase to uppercase

Posted 19 July 2012 - 03:51 PM

Hey i got it. You do need capletters array. I would post the answer but this is a class question so i will message it to you. Thank you though for your first corrections, they made me think!
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1916
  • Posts: 5,731
  • Joined: 05-May 12

Re: String characters in array. Convert lowercase to uppercase

Posted 19 July 2012 - 03:55 PM

Good job!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1