Hello all,

I have a few questions about C++ (I have programmed quite a bit in VBA and some in the past using C++, and have been working through relearning C++). Currently I am working through a bit on the "Char" variable type.

regarding the following (from the FAQ):
* Don't use system("pause") to pause your program if possible. Use getchar( ) if you are using C and cin.get( ) if you are using C++.

I was using cin.get() to do this recently, but ran into a problem where it no longer worked? See the code below for where it was happening. I am not sure why cin.get() was working before, but as soon as I added a cin of my own, it stopped.

I also am not entirely sure why this is not working as I thought it would, using the following program, I attempted to implement a couple things where it takes a user input char array (I am not sure I did this correctly) and then determines how many numbers/letters are in it. It also does not seem to like using a space as an input (for example if I put "this is a testing string 12345!" as the input, it seems to only get "this" from it).

Now, I have found through some cout-ing that when I create the char array, it fills it with random stuff (so I get incorrect results).

Am I completely missing something regarding the use of the Char here?

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


using namespace std;

//define the functions used later
int numbLetters(char inputChar[], int length);
int numbNumbers(char inputChar[], int length);

int main() {
	// There is a difference between single and double quotes
	// When declaring a character use single quotes
	// For strings use double quotes

	char userChar[50];

	cout << "Input some text to be tested" << endl;

	cin >> userChar;

	cout << endl << "You gave: " << userChar << endl;
	cout << "Number letters: " << numbLetters(userChar, (int)sizeof(userChar)/sizeof(char));
	cout << endl << "You gave: " << userChar << endl;
	cout << "Number of numbers: " << numbNumbers(userChar, (int)sizeof(userChar)/sizeof(char)) << endl;

	//pause (needs two for some reason?) actually I have no idea exactly how this works
	cin.get();
	cin.get();
	system("pause");
	return 0;
}
int numbLetters(char inputChar[], int length)
{
	int counter = 0;
	//loop through entire string to find number of characters
	for (int i=0; i<length; i++)
	{
		if (isalpha(inputChar[i]))
			counter++;
	}
	cout << endl << "str length: " << length << endl;
	return counter;
}
int numbNumbers(char inputChar[], int length)
{
	int counter = 0;
	//loop through entire string to find number of numbers
	for (int i=0; i<length; i++)
	{
		if (isdigit(inputChar[i]))
			counter++;
	}
	return counter;
}

edit - ok while no forum searches helped, it turns out that someone else in the currnet last 5 posts or so was having a similar problem about the char input being cut off by a space and was referenced to here - http://www.cplusplus.com/reference/string/getline/ - but I'm not sure how that relates to the Char array I have above

for your first problem of :
cin.get(); not working:

You need to use:
cin.ignore(1); // if that does not work put a 2 in your ()


for your second problem:
instead of using cin.get(variablename);

you should use this to get the entire line:
getline(cin, variablename);

So does getline only work with the following:

getline(cin, StrVariable);

It threw errors when I first put it but seemed to work when I switched it to a string (instead of char).

isalpha() and isdigit() cannot handle values outside of the 0-255 range, so whatever garbage data that is appearing after your characters and the null terminator '\0' would have to be in the range from 0 to 255 if they're not it will crash, so you could use a while loop to continue checking isalpha or isdigit until you reach the null terminator.

This should work inside your numbLetters() function and a similiar one inside your numbNumbers() should do the same.

int counter = 0;
//loop through entire string to find number of characters
int i=0;
while( inputChar[i]!='\0')
{
     if (isalpha(inputChar[i]))
	counter++;
     i++;
};
cout << endl << "str length: " << length << endl;
return counter;

Sweet, that is great! Thanks a bunch!

So does getline only work with the following:

getline(cin, StrVariable);

It threw errors when I first put it but seemed to work when I switched it to a string (instead of char).

There are two different versions.

This one works with C-strings (char *) and is a method of the stream object (i.e., invoked as cin.getline(mystring,capacity) ).
http://www.cplusplus.com/reference/iostream/istream/getline/

This one works with std::strings and takes the stream object as an argument (i.e.,invoked as getline(cin,mystring) )
http://www.cplusplus.com/reference/string/getline/