Are you sure that this program compiles? You seem to have used string in your code, but you haven't included <string.h> at all...!
That's because std::string is not declared in -- its in header file. And some compiler include with
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
Another approach might be to display as you go. Here's some pseudocode to rough out how that might work:
1) declare variables needed---stream to read file and two int variables
2) use a while loop to read in numbers from file
body of the while loop will:
3) divide number read in from file by 10 using integer math
4) increase the number obtained in 3) by one
5) use another loop to display the number of *s indicated by the number obtained in 4)
6) start a new line
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
and it (I mean, including <string.h> ) does not produce any backward warnings at all...
why should the compiler produce warnings? string.h defines functions such as strcmp(), strcat(), etc. which are related to character arrays, not std::string. Those functions and that header file were inherited from C language.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
Instead of the big if...else if block, how about
for( int i = 0; i < 10; i++ )
{
index = arr[i] / 10;
str[index] += "*";
}
vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
And for your future reference, when you have an if...else if block that is separating values into range groupings, you don't need to test both upper and lower bounds in the succeeding conditions.
if( arr[i] >= 0 && arr[i] < 10 )
{
str[0] += "*";
}
else if( arr[i] < 20 )
{
str[1] += "*";
}
else if( arr[i] < 30 )
{
//and so on
The fact that you arrive at the test for < 20 means the value must be >= 10 to have not been caught by the first test, and so on.
And do be afraid to put a space between operators and operands - it makes the code a bit more readable. Spaces are cheap.
vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
Once you have the data stored (counted), find the largest value. Then begin printing lines, looking at each count, print an asterisk as needed, line by line
vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228