?
Solved

Class Question

Posted on 2003-03-18
6
Medium Priority
?
343 Views
Last Modified: 2008-02-01
I'm trying to understand, so forgive my ignorance. I'm running the following tiny program just to test a class. It doesn't look as though a.inputHugeInteger is ever executing, and I don't understand why.

Output
------
We are indeed in main

Press any key to continue

Code
----
// definition of HugeInteger class

#ifndef HugeInteger_H
#define HugeInteger_H

class HugeInteger
{
private:
     char cTheNumber[40];
     bool bCarry;
public:
     HugeInteger();
        void inputHugeInteger();
     };
#endif

// implementation of HugeInteger class

#include"HugeInteger.h"
#include<iostream>

using std::cout;

HugeInteger::HugeInteger()
{
     cTheNumber[0] = '\0';
     bCarry = false;
}

void HugeInteger::inputHugeInteger()
{

     cout <<"In input huge integer";
     //local variables
     char cInputString [40]; //the characters read in from the input stream
     bool bFailed = false;
     int i = 0;

     cout << "Input the huge integer";
     //get the value from the input stream
     fgets(cInputString, sizeof cInputString, stdin);

     //see if it's really an integer
     while(i < sizeof cInputString && cInputString[i] != '\0')
     {
          if (cInputString[i] < '0' ||
               cInputString[i] > '9' ||
               cInputString[i] == '-');
          else
               bFailed = true;
          i++;
     }

     //if it's an integer, set the value of iTheNumber
     if (!bFailed)
     {
          i = 0;
          while(i < sizeof cInputString && cInputString[i] != '\0')
          {
             cTheNumber[i] = cInputString[i];
             i++;
          }
     }

  return ;
}

void HugeInteger::outputHugeInteger()
{
          int i = 0;
          while(i < sizeof cTheNumber && cTheNumber[i] != '\0')
          {
             cout <<  cTheNumber[i];
             i++;
          }

  return ;
}

//Program file to test HugeInteger Class

#include "HugeInteger.h"

#include <iostream>

using namespace std;

void pause();

int main()
{
  cout << "We are indeed in main";
  HugeInteger a;
  a.inputHugeInteger;
  cout << '\n';
  a.outputHugeInteger;
  cout << '\n';
  pause;
  return 0;
  }
0
Comment
Question by:pflugg
  • 3
  • 2
6 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 8159649
Hmm, your 'main()' function looks a bit bogus - shouldn't that be

int main()
{
 cout << "We are indeed in main";
 HugeInteger a;
 a.inputHugeInteger (); // <-- !
 cout << '\n';
 a.outputHugeInteger (); // <-- !
 cout << '\n';
 pause;
 return 0;
 }
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 8159671
Corrections:

1) add line
void outputHugeInteger();

to class definition

2)

a.inputHugeInteger();
a.outputHugeInteger();

You missed ()
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 8159700
jkr, sorry, you are first.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LVL 86

Expert Comment

by:jkr
ID: 8159730
>>jkr, sorry, you are first.

Just being lucky :o)

BTW, to elaborate:

a.inputHugeInteger

*without* the function brackets is a valid expression, but a NULL expression - nothing is done. It is like writing

int i = 1;

i;

0
 

Author Comment

by:pflugg
ID: 8159731
Gee -- I'm missing the forest for the trees.... Thanks!
0
 
LVL 86

Expert Comment

by:jkr
ID: 8159781
You are welcome :o)
0

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

600 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question