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.
> 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?
Effectively you could do the same thing with a char array.
1 2 3 4 5 6 7 8
constint 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]);
}
usingnamespace 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;
}
// toupper.cpp : main project file.
#include "stdafx.h"
#include <cctype>
#include <iostream>
#include <string>
usingnamespace 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
elsebreak; // 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;
}