Hey everyone. I would like to start off this post by intoroducing myself as a beginner to C++. I have been working with C++ through Visual Studio 2010 for the past 4 months and have recently began Object Oriented Programming in school. Regardless here is my problem.
I am trying to write a program that ultimately must take a user defined sentence as an input, and output the number of times each letter of the alphabet appears in this input. As well, the program must also state the length of each word.
So, I decided I would go about it by first receiving the users input and storing it in a char array. I then pass this char array to another function that "purifies" the array by removing all characters that are not alphabetic or a space.
The problem I have run into is this. The code compils fine, and after I enter a sentence and hit enter I receive a runtime Error stating:
Expression: (unsigned)(c+1)<=256 |
Using breakpoints, I have narrowed the problem down to my for loop. Before the program exits the for loop and reaches my cout statement, this error occurs.
Any help would be greatly appreciated. Thanks in advanced.
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 35 36 37 38 39 40 41 42 43 44 45 46
|
#include <iostream>
#include <cstdlib>
#include <string>
using std::char_traits;
using std::cout;
using std::cin;
using std::getline;
using std::string;
char initializeArray (char* input, const int size)
{
cout << "Please enter a sentence\n";
cin.get (input, size);
return* input;
}
void cleanArray(char* input, const int size)
{
int counter = 0;
for (int i = 0; i < size; i++)
{
if ( isalpha (input [i]) || isspace (input [i]))
cin.get (input, i);
}
cout << input;
}
int main ()
{
const int size = 2048;
char input [size];
initializeArray(input, size);
cleanArray(input, size);
system ("pause");
return 0;
}
|