?
Solved

Stupid syntax question

Posted on 2003-03-17
4
Medium Priority
?
285 Views
Last Modified: 2010-04-01
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
Comment
Question by:pflugg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 

Expert Comment

by:e12voltsdac
ID: 8156578
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
 
LVL 1

Expert Comment

by:igor_sk
ID: 8156665
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 8157359
>> 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
 
LVL 12

Accepted Solution

by:
Salte earned 1000 total points
ID: 8158013
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

766 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