• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 291
  • Last Modified:

Stupid syntax question

Trying to read in a huge integer (which is just an array of up to 40 numbers). Using the code, below. Don't understand the compiler errors I'm getting. Appreciate any help...

HugeInteger HugeInteger::inputHugeInteger()
{
    bool bFailed;
    char array_of_characters[40];

     bFailed = false;

    fgets(array_of_characters,40,stdin);

     for(int i = 0; i<40; i++)
     {
          if (array_of_characters[i] < "0") //just the first condition for checking int
               bFailed = true;
     }

  return "12345";//dummy
}
Compiling...
HugeInteger.cpp
D:\Data\School\HugeInteger.cpp(26) : error C2446: '<' : no conversion from 'char *' to 'int'
        This conversion requires a reinterpret_cast, a C-style cast or function-style cast
D:\Data\School\HugeInteger.cpp(26) : error C2040: '<' : 'int' differs in levels of indirection from 'char [2]'
D:\Data\School\HugeInteger.cpp(30) : error C2664: '__thiscall HugeInteger::HugeInteger(const class HugeInteger &)' : cannot convert parameter 1 from 'char [6]' to 'const class HugeInteger &'
        Reason: cannot convert from 'char [6]' to 'const class HugeInteger'
        No constructor could take the source type, or constructor overload resolution was ambiguous
D:\Data\School\HugeInteger.cpp(30) : error C2553: no legal conversion of return value to return type 'class HugeInteger *'
Error executing cl.exe.

HugeInteger.obj - 4 error(s), 0 warning(s)


Whole banana to date...
// implementation of HugeInteger class

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

using std::cout;

HugeInteger::HugeInteger()
{
     for (int i=0;i<40;i++)
        iTheNumber[i] = 0;
     bCarry = false;
}

HugeInteger HugeInteger::inputHugeInteger()
{
    bool bFailed;
    char array_of_characters[40];

     bFailed = false;

    fgets(array_of_characters,40,stdin);

     for(int i = 0; i<40; i++)
     {
          if (array_of_characters[i] < "0")
               bFailed = true;
     }

  return "12345";
}

0
pflugg
Asked:
pflugg
1 Solution
 
e12voltsdacCommented:
Can i see what your Hugeinteger.h file looks like. There might be an error there. Also, you put return "12345", when it was expecting a HugeInteger.
0
 
igor_skCommented:
You get 2 errors in line 26 because you compare integer value (array_of_characters[i]) and char* ("0"). You should use '0' (which is interpreted as integer) instead of "0" and line 26 will be as following:

if (array_of_characters[i] < '0')

You get 2 errors in line 30 because you return char[6], but described that this method return HugeInteger. When you return the class from the method copy constructor for this method is called. If you don't define a copy constructor, compiler generates it for you. Copy constructor in this case will be defined as HugeInteger::HugeInteger(const class HugeInteger &). You return char[6] (I understand that you use it as a temporary value), but compiler tries to convert it to 'const class HugeInteger &', it fails and prints an error message.

I hope this will help.
Igor.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> if (array_of_characters[i] < "0") //just the first condition for checking int


Make it:

if ( array_of_characters[i] < '0' || array_of_characters[i] > '9' ) // notice the single-quotes

Your inputHugeInteger () function is supposed to return a HugeInteger object as per the specified return-type.

Make a parameterized constructor like:

HugeInteger ( char * str )
{
  strcpy ( array_of_characters, str ) ;
  bFailed = true ; // or false -> whatever you need

} // end of constructor ()

And replace your:

>> return "12345";//dummy

by:

return HugeInteger ( "12345" ) ;


That'll do it!

Mayank.
0
 
SalteCommented:
First off, if you want to test if a char is less than digit 0 the test is:

if (char_array[x] < '0')

The test must be on the character and not on a string. Also, if it was a string you should have used strcmp() and not a simple test for '<' since a test for

if (foo < "0") {

is a test if foo is a character pointer that point to some location in memory prior to the string literal "0". Since the string literal is probably just placed in memory at first and best location the compiler find for it such a test doesn't make sense.

Lesson 1: NEVER do:

if (some_expr < "some character literal")

This is NEVER a good idea, not under ANY circumstance.

testing char pointers can be a good idea, but only if both pointers point to positions within the same string.

char buf[40];

char * p = buf + 2;
char * q = buf + 5;

if (p < q) {

test if p is before q within the array buf. In this case the test is true since p is at buf + 2 while q is at buf + 5.

String literals are placed in their own location at the compiler's choosing so a test like:

if (p < "foo")

would never make sense.

Hope this is of help for you. I believe if you change that "0" to '0' at least some of those errors should go away.

Alf
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now