Using a Array and Capitalizing letters

I am trying to Take a input, convert it into a array of chars, Capitalize them , and then output the chars(like a string). I have tried multiple different ways and can't seem to get a output that is correct or I just get errors. StackOverflow is how I usually ask questions , but every time I do I get really complicated answers and get treated like a idiot which is really frustrating as a computer science student.


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
#include <cctype>
#include <iostream>              
#include <string>

using namespace std;                

int main ()

{

  
  string input;
  string output;
  

  cout<<"Enter a String of Charchters to be Capitalized : ";
  cin>>input;
  string arrayinput[]= input;
  output = toupper(arrayinput[]);
  cout<<"\n\n\n"<<output<<"\n\n\n";
  
  
  
cout<<"Press <Enter> to Exit";
cin.ignore();
cin.get();      
return 0;                        
}
I am pretty sure I am using the array incorrectly
> I am pretty sure I am using the array incorrectly

yes, in several places.

Since the input is being read into a std::string, you don't need to mess around with raw C-style arrays at all. Just convert each lower case char in the input string to its upper case equivalent and print it out.

1
2
3
4
5
6
7
8
9
10
11
string input ;


cout << "Enter a String of Charchters to be Capitalized : " ;
cin >> input;   // read a string

// convert each lowercase char in the string to uppercase
for( int i=0 ; i < input.size() ; ++i ) input[i] = toupper( input[i] ) ;

// and print it out
cout << "\n\n\n" << input << "\n\n\n" ;



Actually in the problem I was given , the data had to be put in a array. So it seems I need the input type to be a char array? So Then I need the input and output to be char?


thanx for the reply
Last edited on
Effectively you could do the same thing with a char array.

1
2
3
4
5
6
7
8
const int ARRAY_SIZE = 100;  // Set to 100. You can change this to whatever length you want
char input[ARRAY_SIZE];
cin >> input;

for (int i=0; i < ARRAY_SIZE; i++)
{
   input[i] = toupper(input[i]);
}
> So it seems I need the input type to be a char array?

Yes. See: http://www.cplusplus.com/reference/iostream/istream/getline/
http://www.cplusplus.com/reference/iostream/istream/get/

Avoid this; it creates a buffer overflow vulnerability in your code.
1
2
char input[ARRAY_SIZE];
cin >> input; 


Instead, use something like:
1
2
char input[ARRAY_SIZE];
cin.get( input, ARRAY_SIZE ) ; 



> So Then I need the input and output to be char?

An array of char.
Last edited on
Now I am getting a invalid conversion error when it tries to use toupper on the array.




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using namespace std;                

int main ()

{

  
  char input[20];
  char output[20];
  

  cout<<"Enter a String of Charchters to be Capitalized : ";
  cin>>input;
  
  output = toupper(input);
  cout<<"\n\n\n"<<output<<"\n\n\n";
  
  
  
cout<<"Press <Enter> to Exit";
cin.ignore();
cin.get();      
return 0;                        
}

Use this example.
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
// toupper.cpp : main project file.

#include "stdafx.h"
#include <cctype>
#include <iostream>              
#include <string>

using namespace std;                

int main ()
{
  char input[20];
  char output[20]; // not really needed

  cout<<"Enter a String of Characters to be Capitalized : ";
  cin >> input;
  for (int x=0;x<20;x++) // Step through each of the characters in the array named 'input'
	  if (input[x]!='x\0') // Checking if equal to end of typed input
		  input[x]=toupper(input[x]); // If not, capitilize it
	//    output[x]=toupper(input[x]); // If you HAVE to use two arrays. Delete the line above
	  else
		  break; // End of typed input, quit searching
  
    cout<<"\n\n\n"<<input<<"\n\n\n";
//	cout<<"\n\n\n"<<output<<"\n\n\n";   // If you HAVE to use two arrays. Delete the line above
  
cout<<"Press <Enter> to Exit";
cin.ignore();
cin.get();      
return 0;                        
}
Topic archived. No new replies allowed.