Solved

Invalid Conversion error... please help

Posted on 2004-08-30
16
196 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
ID: 11936804
You can't do an atoi of a single character that way.

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

0
 
LVL 86

Expert Comment

by:jkr
ID: 11937602
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
ID: 11937628
if i make toparr a string, should a stringstream like this work?

stringstream dg;
dg << digit;
dg >> toparr[i];
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 86

Expert Comment

by:jkr
ID: 11937640
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
ID: 11937647
is there a way to define the length?
0
 
LVL 86

Expert Comment

by:jkr
ID: 11937698
For a stringstream extraction operation? Sure, e.g

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

should work.
0
 

Author Comment

by:yajoros
ID: 11937812
it says 'setprecision' is undefined.
0
 

Author Comment

by:yajoros
ID: 11937919
and yes i just realized i had it backwards
0
 
LVL 86

Expert Comment

by:jkr
ID: 11937957
>> it says 'setprecision' is undefined.

Ooops :o)

Add

#include <iomanip>
0
 

Author Comment

by:yajoros
ID: 11938038
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
ID: 11938041
i mean result, not record sorry
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 11940271
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
ID: 11940425
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
ID: 11946759
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
ID: 11946776
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

829 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