#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;
}
5 Replies - 1025 Views - Last Post: 19 July 2012 - 03:55 PM
#1
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.
Replies To: String characters in array. Convert lowercase to uppercase
#2
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?
On line 28, you are outputing some uninitialized value from your capletters array.
Do you even need the the capletters array?
#3
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;
}
#4
Re: String characters in array. Convert lowercase to uppercase
Posted 19 July 2012 - 03:42 PM
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
#5
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!
#6
Re: String characters in array. Convert lowercase to uppercase
Posted 19 July 2012 - 03:55 PM
Good job!
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote




|