Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,269 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,458 people online right now. Registration is fast and FREE... Join Now!




Uppercase Conversion

 
Reply to this topicStart new topic

Uppercase Conversion, Need Help Converting To Uppercase...

supercharged_V6
12 Dec, 2006 - 09:08 PM
Post #1

D.I.C Head
**

Joined: 3 Oct, 2006
Posts: 57


My Contributions
Yo, whats up dudes, i basically have this thing i have to do, were i convert a lower case string into an uppercase string... problem is... the array fields that are not used display weird symbols when the program is executed... anyone know what i am doing wrong?

CODE

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    char input[13];
    
    cout<<"-=***|[This program converts lower case words to\n ";
    cout<<"upper case]|***=-"<<endl;
    cout<<" "<<endl;
    cout<<"Please Enter A Word String (Upto 12 letters long)"<<endl;
    cin>>input;
    
    input[0] = toupper(input[0]);
    input[1] = toupper(input[1]);
    input[2] = toupper(input[2]);
    input[3] = toupper(input[3]);
    input[4] = toupper(input[4]);
    input[5] = toupper(input[5]);
    input[6] = toupper(input[6]);
    input[7] = toupper(input[7]);
    input[8] = toupper(input[8]);
    input[9] = toupper(input[9]);
    input[10] = toupper(input[10]);
    input[11] = toupper(input[11]);
    
    cout<<"After conversion is should look something like this!"<<endl;
    cout<<input[0]<<input[1]<<input[2]<<input[3]<<input[4]<<input[5]<<"\n";
    cout<<input[6]<<input[7]<<input[8]<<input[9]<<input[10]<<input[11]<<endl;
    
    system("pause");
    return 0;
      
      }
      

User is offlineProfile CardPM
+Quote Post

eXceed69
RE: Uppercase Conversion
12 Dec, 2006 - 09:13 PM
Post #2

"Super Sentai Knight Of DawN"
Group Icon

Joined: 12 Nov, 2006
Posts: 682



Thanked: 1 times
Dream Kudos: 675
My Contributions
Ei, how bout used some loop on each indexes...

CODE

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    char input[13];
    int i;
    cout<<"-=***|[This program converts lower case words to\n ";
    cout<<"upper case]|***=-"<<endl;
    cout<<" "<<endl;
    cout<<"Please Enter A Word String (Upto 12 letters long)"<<endl;
    cin>>input;
    
/loop make it easy
for(i=o;i<=13; i++)
{
    input[i] = toupper(input[i]);
}  
    
    cout<<"After conversion is should look something like this!"<<endl;
for(i=o;i<=13; i++)
{
    cout<<input[i];
    
  }  
    system("pause");
    return 0;
      
      }


This post has been edited by eXceed69: 12 Dec, 2006 - 09:15 PM
User is offlineProfile CardPM
+Quote Post

czy1121
RE: Uppercase Conversion
13 Dec, 2006 - 01:43 AM
Post #3

New D.I.C Head
*

Joined: 10 Dec, 2006
Posts: 9


My Contributions
to eXceed69:
when i is equal to 13, follow sentence maybe get a mistake and o is not 0.
CODE

input[i] = toupper(input[i]);


User is offlineProfile CardPM
+Quote Post

eXceed69
RE: Uppercase Conversion
13 Dec, 2006 - 01:57 AM
Post #4

"Super Sentai Knight Of DawN"
Group Icon

Joined: 12 Nov, 2006
Posts: 682



Thanked: 1 times
Dream Kudos: 675
My Contributions
heheh...sorry..typo:D..nice eye!

This post has been edited by eXceed69: 13 Dec, 2006 - 01:57 AM
User is offlineProfile CardPM
+Quote Post

shakti8ie
RE: Uppercase Conversion
13 Dec, 2006 - 04:50 AM
Post #5

New D.I.C Head
*

Joined: 11 Dec, 2006
Posts: 21



Thanked: 1 times
My Contributions
I think this is some better code...

CODE

#include <iostream>

using namespace std;

int main()
{
    char input[13]="";
    
    cout<<"-=***|[This program converts lower case words to\n ";
    cout<<"upper case]|***=-"<<endl;
    cout<<" "<<endl;
    cout<<"Please Enter A Word String (Upto 12 letters long)"<<endl;
    cin>>input;

    for(int i=0;i<12;i++)
    {
        input[i]=toupper(input[i]);
    }
    input[12]='\0';
    
      
    cout<<"After conversion is should look something like this!"<<endl;
    for(int i=0;i<13;i++)
        cout<<input[i];
    cout<<endl;
    
    return 0;
      
}


The problem with ur code is that u didn't initialize the array to null...this makes ur whole array containing some garbage value in each element that shows up when u see the o/p...again u should use loops to make ur code short...right?
User is offlineProfile CardPM
+Quote Post

BitByte
RE: Uppercase Conversion
13 Dec, 2006 - 05:04 AM
Post #6

D.I.C Head
**

Joined: 9 Aug, 2006
Posts: 194



Thanked: 2 times
My Contributions
There is a problem there, you have put the null terminator at element thirteen. He is reading letters UP TO thirteen characters. If he only enters four letters then the rest of the letters up to the null terminator are going to be garbage.

CODE
#include <iostream>
#include <string>
#include <cctype>

using namespace std;

int main( void )
{
    char input[13];

    cout << "-=***|[This program converts lower case words to\n ";
    cout << "upper case]|***=-" << endl;
    cout << " " << endl;
    cout << "Please Enter A Word String (Upto 12 letters long)"<<endl;
    cin >> input;
    // You need a check in here to make sure the user does not go over
    // thirteen characters.

    for ( int i = 0; i < strlen( input ); i++ )
    {
        input[i] = toupper( input[i] );
    }

    std::cout << input << std::endl;
    std::cin.get();

    return 0;

}

User is offlineProfile CardPM
+Quote Post

may4life
RE: Uppercase Conversion
13 Dec, 2006 - 05:34 AM
Post #7

New D.I.C Head
*

Joined: 5 Nov, 2006
Posts: 23


My Contributions
You can also do it this way.. Warning, this program depends on the input of the user being 12 characters or below. This is not recommended, its just another way of doing things wink2.gif
CODE

#include <iostream>

using namespace std;

int main()
{
    char input[13] = {'\0'}; // initialize all elements to null

    cout << "Enter a word upto 12 characters long: ";
    cin >> input;

    int i=0;
    while (input[i] != '\0')
    {
        input[i] = toupper(input[i]);
        i++;
    }

    cout << input << endl;

    return 0;
}

User is offlineProfile CardPM
+Quote Post

BitByte
RE: Uppercase Conversion
13 Dec, 2006 - 05:53 AM
Post #8

D.I.C Head
**

Joined: 9 Aug, 2006
Posts: 194



Thanked: 2 times
My Contributions
Or just use the string class which is SO much easier to use:

CODE
#include <iostream>
#include <string>
#include <cctype>
#include <algorithm>

int main( void )
{
    std::cout << "Enter a word: ";
    std::string word;
    std::cin >> word;

    for ( std::string::size_type i = 0; i < word.size(); i++ )
        {
            word[i] = toupper( word[i] );
        }

        std::cout << word << std::endl;

    std::cout << "Enter another word: ";
        std::string word2;
    std::cin >> word2;

    transform( word2.begin(), word2.end(), word2.begin(), toupper );
    std::cout << word2 << std::endl;

    return 0;
}

User is offlineProfile CardPM
+Quote Post

supercharged_V6
RE: Uppercase Conversion
13 Dec, 2006 - 03:06 PM
Post #9

D.I.C Head
**

Joined: 3 Oct, 2006
Posts: 57


My Contributions
thanks dude, really helped out...






User is offlineProfile CardPM
+Quote Post

shakti8ie
RE: Uppercase Conversion
13 Dec, 2006 - 11:16 PM
Post #10

New D.I.C Head
*

Joined: 11 Dec, 2006
Posts: 21



Thanked: 1 times
My Contributions
QUOTE(BitByte @ 13 Dec, 2006 - 06:04 AM) *

There is a problem there, you have put the null terminator at element thirteen. He is reading letters UP TO thirteen characters. If he only enters four letters then the rest of the letters up to the null terminator are going to be garbage.



I think u didn't notice that I've initialized the array to null. That makes all elements initialized to null. Yes, the statement i.e. input[12]='\0' is not required, if the user limits to 12 characters i.e. from 0...11. But to show that character arrays are strings I tried to put a '\0' in the end.

BTW the later versions of code are very nice...thanks.

User is offlineProfile CardPM
+Quote Post

BitByte
RE: Uppercase Conversion
14 Dec, 2006 - 06:10 AM
Post #11

D.I.C Head
**

Joined: 9 Aug, 2006
Posts: 194



Thanked: 2 times
My Contributions
QUOTE
I think u didn't notice that I've initialized the array to null. That makes all elements initialized to null. Yes, the statement i.e. input[12]='\0' is not required, if the user limits to 12 characters i.e. from 0...11. But to show that character arrays are strings I tried to put a '\0' in the end.


My appologies shakti8ie, you are right, i never noticed. Sorry about that. The later versions of code are just incase he might want to use them instead. Just incase it isn't an assignment!!
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 07:43PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month