I have an assignment that is due by midnight tonight. I have completed 2 of 3 programs to write, and I have a very unusual (to me) problem. Here is the Header file:
Code:
// interface file for the counter class -- counter.h
#ifndef COUNTER_H
#define COUNTER_H
class Counter
{
public:
// constructors
Counter();
// modifier methods
void clear();
void inc();
void dec();
// accessor methods
int value() const;
private:
int count;
};
#endif
The implementation file:
Code:
// Implementation file for the counter class -- counter.cc
#include<iostream>
#include<string>
using namespace std;
#include "counter.h"
//**************************************************************************
// Constructor to set the initial count to zero.
//**************************************************************************
Counter::Counter() { count = 0; }
//*************************************************************************
// Function to clear the counter.
//*************************************************************************
void Counter::clear() { count = 0; }
//*************************************************************************
// Function to increment the counter by one.
//*************************************************************************
void Counter::inc()
{
count++;
cout << "INCREMENT" << endl;
}
//*************************************************************************
// Function to decrement the counter by one.
// - If the counter is already at zero, an error message is returned.
//*************************************************************************
void Counter::dec()
{
if(count == 0)
cout << "Error: Cannot go less than zero." << endl;
else
count = count - 1;
}
//*************************************************************************
// Function to return the counter's value.
//*************************************************************************
int Counter::value() const { return count; }
and the Client program:
Code:
//**************************************************************************
// Program to determine how many of each digit occurs in a string.
//
// Written by Alan Johnson
// 001085624
// April 14th, 2004
//**************************************************************************
#include<string>
#include<iostream>
using namespace std;
#include "counter.h"
int main()
{
string input;
cout << "Enter a string (Ctrl-D to stop): ";
getline(cin, input);
if(!cin.eof())
{
while(!cin.eof())
{
Counter A[10];
for(int i = 0; i < 10; i++)
for(int k = 0; k < input.length(); k++)
{
// HERE IS WHERE THE PROBLEM LIES
if((input.at(k)) == i)
A[i].inc();
}
for(int j = 0; j < 10; j++)
{
cout << A[j].value() << " ";
A[j].clear();
}
cout << endl << "Enter a string (Ctrl-D to stop): ";
getline(cin, input);
}
cout << endl;
return 0;
}
else
cout << endl;
return 0;
}
I bolded the comment where the problem is.
For some reason, when the input.at(k) DOES equal i, It doesn't actually go into the if statement. I tested it out, and no matter what the user puts in it does not make the if statement true. I searched on the internet high and low, and nothing has helped me. I would really very much appreciate it if anyone can help.
Thanks!