Urgent! Help Needed

This is a discussion on Urgent! Help Needed within the C++ Programming forums, part of the General Programming Boards category; I have an assignment that is due by midnight tonight. I have completed 2 of 3 programs to write, and ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    2

    Urgent! Help Needed

    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!

  2. #2
    ggs
    ggs is offline
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    try doing (i + '0') instead of i
    .sect signature

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    2
    Eureka! Thank you very much. Such a simple solution, as I thought it would be. So if you don't mind, I was just curious why it works that way?

    Thanks again!

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I was just curious why it works that way?
    0 + '0' == '0'
    1 + '0' == '1'
    2 + '0' == '2'
    etc...

    [edit]
    Typo
    [/edit]
    Last edited by Prelude; 04-15-2004 at 06:30 AM.
    My best code is written with the delete key.

  5. #5
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    You can't correctly compare the integer i to the character returned from at() without converting them to the same type. Luckily, characters are represented by codes and all of the characters that represent numbers 0-9 come in order in those codes. That creates an easy way to convert a single digit integer to its character equivalent. Just take the code for '0' (whatever it is) and if you add 4 to it you'll get the code for '4'. Of course, that only works for single digit positive integers. If you have more complicated numbers, you'd have to convert the entire string to a number first (maybe using atoi or atof) and then compare the numbers.

    > 2 + '0' == '3'
    You sure?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Urgent Help Needed In Writing Algorithm!!
    By Vikramnb in forum C++ Programming
    Replies: 1
    Last Post: 01-09-2009, 11:46 AM
  2. Help Needed: Borland C++ 5.5 Installation - URGENT!
    By Linette in forum C++ Programming
    Replies: 12
    Last Post: 03-09-2002, 05:44 PM
  3. urgent help needed
    By david in forum C Programming
    Replies: 0
    Last Post: 11-27-2001, 07:27 AM
  4. Urgent Help Needed
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 11-25-2001, 06:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21