Solved

Invalid Conversion error... please help

Posted on 2004-08-30
16
188 Views
Last Modified: 2012-06-27
I am fairly new to this, and cannot figure out why I get this error.
Here is the whole function with the error in it.

int random(int top){
    int i, digit;
    int numdigits = 0;
    while(top >= 1){
        top = top / 10;
        numdigits++;
    }      
    char toparr[7];
    itoa(top, toparr, 10);
    for(i = 0; i <= numdigits; i++){
-->   digit = atoi(toparr[i]);   <-- this line gives error, invalid  conversion from `char' to `const char*'
        int prev = i-1;
        int randnum;
        if(i != 1 && result[prev] == toparr[prev]){
            randnum = digit;
        }else{
            randnum = 9;
        }
        srand(static_cast<unsigned>(time(0)));
        int random_integer;
        for(int num = 0; num < 250; num++){
            rand();
        }    
        result[i] = int(randnum * rand()/(RAND_MAX+1.0));        
    }    
    int r = atoi(result);
    return (r);
}
0
Comment
Question by:yajoros
  • 8
  • 4
  • 2
  • +1
16 Comments
 
LVL 22

Expert Comment

by:grg99
Comment Utility
You can't do an atoi of a single character that way.

Try   digit = toparr[ i ] - '0';

0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
Just use

 digit = atoi(&toparr[i]);

since you want to call a conversion routine with an offset into a char array, you can the index operator, but you need to be aware that it returns a 'char', thus the error message. All you have to do now is to apply the 'address operator'. BTW.

 digit = atoi(toparr+i);

would work also.
0
 

Author Comment

by:yajoros
Comment Utility
if i make toparr a string, should a stringstream like this work?

stringstream dg;
dg << digit;
dg >> toparr[i];
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
Yes, that should work, but remeber tat you should test if there's enough space available in the array to hold the input.
0
 

Author Comment

by:yajoros
Comment Utility
is there a way to define the length?
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
For a stringstream extraction operation? Sure, e.g

dg << digit;
dg >> setprecision(number_of_bytes) >> toparr[i];

should work.
0
 

Author Comment

by:yajoros
Comment Utility
it says 'setprecision' is undefined.
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:yajoros
Comment Utility
and yes i just realized i had it backwards
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
>> it says 'setprecision' is undefined.

Ooops :o)

Add

#include <iomanip>
0
 

Author Comment

by:yajoros
Comment Utility
ok, that works, but i dont think its necessary, the max would be 7 digits.
now my problem is this:
first, here is updated code:

int random(int top){
    int i, digit;
    int numdigits = 0;
    int top2 = top;
    while(top2 >= 1){  //gets number of digits in 'top'
        top2 = top2 / 10;
        numdigits++;
    }      
    cout << "top: " << top << endl;    
    stringstream re;
    string toparr;
    re << top;
    re >> toparr;
    cout << "toparr: " << toparr << endl;
    string result;
    for(i = 0; i < numdigits; i++){  //makes random number by making random individual digits with max of the corresponding digit
        stringstream dg;
        dg << toparr[i];
        dg >> digit;
        int randnum;
        if(i == 1){if(result[i-1] == toparr[i-1]){randnum = digit + 1;}else{randnum = 10;}}
        if(i == 2){if(result[i-1] == toparr[i-1] && result[i-2] == toparr[i-2]){randnum = digit + 1;}else{randnum = 10;}}
        if(i == 3){if(result[i-1] == toparr[i-1] && result[i-2] == toparr[i-2] && result[i-3] == toparr[i-3]){randnum = digit + 1;}else{randnum = 10;}}
        if(i == 4){if(result[i-1] == toparr[i-1] && result[i-2] == toparr[i-2] && result[i-3] == toparr[i-3] && result[i-4] == toparr[i-4]){randnum = digit + 1;}else{randnum = 10;}}
        if(i == 5){if(result[i-1] == toparr[i-1] && result[i-2] == toparr[i-2] && result[i-3] == toparr[i-3] && result[i-4] == toparr[i-4] && result[i-5] == toparr[i-5]){randnum = digit + 1;}else{randnum = 10;}}
        if(i == 6){if(result[i-1] == toparr[i-1] && result[i-2] == toparr[i-2] && result[i-3] == toparr[i-3] && result[i-4] == toparr[i-4] && result[i-5] == toparr[i-5] && result[i-6] == toparr[i-6]){randnum = digit + 1;}else{randnum = 10;}}
        srand(static_cast<unsigned>(time(0)));
        int random_integer;
        for(int num = 0; num < (9*i); num++){
            rand();
        }    
        char randomch;
        int random = int(randnum * rand()/(RAND_MAX+1.0));
        stringstream rr;
        rr << random;
        rr >> result[i];
        cout << endl << "digit from array: " << digit << " toparr: " << toparr[i] << endl;
        cout << "range for digit: " << randnum << endl;
        cout << "result in array[" << i << "] " << result[i] << endl;
        cout << "cumulative result: " << result << endl;        
    }    
    stringstream rs;
    int finalresult;
    rs << result;
    rs >> finalresult;    
    return (finalresult);
}

i made it display various variables every time so i can't pinpoint the problem.
i can get every digit to be recorded into record[i], but when i display the contents of record, i get nothing.
any ideas?
thanks a lot for all your help btw
0
 

Author Comment

by:yajoros
Comment Utility
i mean result, not record sorry
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
Comment Utility
It doesn't work because of that:

>>>        rr >> result[i];
       
You defined result as

    string result;

It's an empty string and result[i] isn't valid because of that.

Do that:

   string result;
   for (int j = 0; j < numdigits; ++j)
   {
        result += '0';
   }

Then it should work.

Regards, Alex
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 130 total points
Comment Utility
Ok, i checked your function and reduced the code to that:


int random(int top)
{
    int i, digit;
    int numdigits = 0;
    int top2 = top;
    while(top2 >= 1){  //gets number of digits in 'top'
        top2 = top2 / 10;
        numdigits++;
    }      
    cout << "top: " << top << endl;    
    stringstream re;
    string toparr;
    re << top;
    re >> toparr;
    cout << "toparr: " << toparr << endl;
    string result;
    for (int j = 0; j < numdigits; ++j)
        result += '0';
    //makes random number by making random individual digits with max of the corresponding digit    
    srand(static_cast<unsigned>(time(0)));
    for(i = 0; i < numdigits; i++)  
    {    
        digit = toparr[i];
        int randnum = rand();  // init randnum for case i == 0
        if (i > 0)
        {
            // the following covers all six if statements you had
            // however, it's not very likely that the condition ever will be true
            // in several tests i always got 10
            if (result.substr(0, i) == toparr.substr(0, i))
                randnum = digit + 1;
            else
                randnum = 10;
        }
        // int random_integer;
        // what is that for???
        for(int num = 0; num < (9*i); num++)
        {
            rand();
        }    
        // char randomch;  
        int random = int(randnum * rand()/(RAND_MAX+1.0));
        // by using stringstream you'll get the first digit while i take the last digit by that
        result[i] = static_cast<char>(random%10 + '0');
        cout << endl << "digit from array: " << digit << " toparr: " << toparr[i] << endl;
        cout << "range for digit: " << randnum << endl;
        cout << "result in array[" << i << "] " << result[i] << endl;
        cout << "cumulative result: " << result << endl;        
    }    
    stringstream rs;
    int finalresult;
    rs << result;
    rs >> finalresult;    
    return (finalresult);
}

However, i doubt that all that code gives any significant advantage to calling

    srand(time(0));
    int r = rand();

beside that you get different numbers.

Regards, Alex

0
 

Author Comment

by:yajoros
Comment Utility
excellent, thank you.  and yes, i guess i am messy, i wnet through a lot of snippets of code and sometimes left stuff behind.
0
 

Author Comment

by:yajoros
Comment Utility
the srand is supposed to initialize the random thing, and by using the current time, it gets a different seed every time you run it, just to make it more random.  even though for this purpose its not really necessary.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now