Help with string conversion from LC to UC

Hey guys,
I don't understand why I am getting an error after my conversion in the console. Any help would be greatly appreciated!

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

using namespace std;

int main()
{
string sentence;
int size;

cout << "Please enter a sentence. This sentence will be converted to upper case." << endl;
getline(cin, sentence);
size = sentence.length();

char* sentenceTwo = new char[size];
if (sentenceTwo == NULL)
{
cout << "Unable to allocate memory." << endl;
}

for (int i = 0; i < sentence.length(); i++)
{
sentenceTwo[i] = sentence[i];

if (isalpha(sentenceTwo[i]))
{
sentenceTwo[i] = toupper(sentenceTwo[i]);
}
}

cout << sentenceTwo << endl;
delete [] sentenceTwo;
sentenceTwo == NULL;

system("pause");
return 0;
}



EDIT: Here is a better formatted look: http://pastebin.com/QaGZR41p
Last edited on
The C-style string - sentenceTwo - should be null-terminated.

Use a std::string instead?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream>
#include<string>
#include<cctype>

int main()
{
    std::string sentence;

    std::cout << "Please enter a sentence. This sentence will be converted to upper case.\n" ;
    std::getline( std::cin, sentence ) ;

    std::string sentenceTwo ; 
    for( std::size_t i = 0; i < sentence.length(); i++ ) sentenceTwo += std::toupper( sentence[i] ) ; 

    std::cout << sentenceTwo << '\n' ;
}

http://coliru.stacked-crooked.com/a/6eba27a78399c018
closed account (z1CpDjzh)
Why you making a char... there clumsy and lack memory management and stuff... but that ain't my problem.



I look upon your code and see:
1 security hole
2 NULLs
1 wrong method
---------------------------------------------------
SYSTEM('PAUSE")
If you were in a class of mine and i saw that hideous function i would give you a big fat F. WHO EVER TAUGHT YOU SHOULD HAVE KNOWN BETTER!


system("anything") is un-portable, slow and clumsy. let me tell you what it does.
suspend your program

1- call the operating system

2- open an operating system shell (relaunches the O/S in a sub-process)

3- the O/S must now find the PAUSE command

4- allocate the memory to execute the command

5- execute the command and wait for a keystroke

6- deallocate the memory

7- exit the OS

8- resume your program

------------------------------------

Just use
std::cin.get()


--------------------------------------
NULL
NULL = 0 right? That means:
1
2
3
4
5
6
int foo(NULL);

int foo (0);


//they both do the same thing! 

Use nullptr.... if its compatible with your compiler. if its not you better not put too mich nulls in your code...... i warn you.

-------------------------------------------
ou heard of a stringstream.... its used to convert to and from strings... its the altmate tool for converting....


New code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include<iostream>
#include<sstream>
#include<string>
 
using namespace std;
 
int main()
{
        string sentence;
        stringstream SS;
		char sentence2;

        cout << "Please enter a sentence. This sentence will be converted to upper case." << endl;
        getline(cin, sentence);

		SS << sentence;
		SS >> sentence2;
		
		cout << sentence2;


		cin.get();
        
        return 0;
}


-----------------------
i also would not put Using namespace std; ans it makes the point of namespaces absoulete.

------------------------------------
you want to make it upercase... here:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include<iostream>
#include<string>
#include <locale>         //std::toupper
 
using namespace std;
 
int main()
{
        string sentence;
		string sentence2;
		std::locale settings;

        cout << "Please enter a sentence. This sentence will be converted to upper case." << endl;
        getline(cin, sentence);


		for(short i = 0; i < sentence.size(); ++i){
			sentence2 += (std::toupper(sentence[i], settings));}
		
		cout << sentence2;


		cin.get();
        
        return 0;
}
I need it to be a dynamic array for my class. I need to allocate and delete the memory. Anything else will be helpful.
closed account (z1CpDjzh)
ok here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include<iostream>
#include<string>
#include <locale>         //std::toupper
#include <sstream>
 
using namespace std;
 
int main()
{
        string sentence;
		string sentence2;
		std::locale settings;
                string sentenceS

        cout << "Please enter a sentence. This sentence will be converted to upper case." << endl;
        getline(cin, sentence);


		for(short i = 0; i < sentence.size(); ++i){
			sentenceS += (std::toupper(sentence[i], settings));}
		
		cout << sentenceS;

                SS << sentenceS;
		SS >> sentence2;
		
		cout << sentence2;


		cin.get();

        
        return 0;
}


Now was that hard? (THAT IS SARCASM... DO NOT ANSWER THE QUESTION)
Last edited on
> I need it to be a dynamic array. I need to allocate and delete the memory.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
#include <cctype>

int main()
{
    std::string sentence;

    std::cout << "Please enter a sentence. This sentence will be converted to upper case.\n" ;
    std::getline( std::cin, sentence ) ;
    
    const std::size_t size = sentence.size() ;
    char* sentenceTwo = new char[size+1] ; // one extra char for the null 

    for( std::size_t i = 0 ; i < size ; i++ ) sentenceTwo[i] = std::toupper( sentence[i] ) ; 
    sentenceTwo[size] = 0 ; // null terminate

    std::cout << sentenceTwo << '\n' ;
    
    delete[] sentenceTwo ;
}

http://rextester.com/IGKFN26767
Topic archived. No new replies allowed.