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

Invalid Conversion error... please help

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
yajoros
Asked:
yajoros
  • 8
  • 4
  • 2
  • +1
1 Solution
 
grg99Commented:
You can't do an atoi of a single character that way.

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

0
 
jkrCommented:
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
 
yajorosAuthor Commented:
if i make toparr a string, should a stringstream like this work?

stringstream dg;
dg << digit;
dg >> toparr[i];
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
jkrCommented:
Yes, that should work, but remeber tat you should test if there's enough space available in the array to hold the input.
0
 
yajorosAuthor Commented:
is there a way to define the length?
0
 
jkrCommented:
For a stringstream extraction operation? Sure, e.g

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

should work.
0
 
yajorosAuthor Commented:
it says 'setprecision' is undefined.
0
 
yajorosAuthor Commented:
and yes i just realized i had it backwards
0
 
jkrCommented:
>> it says 'setprecision' is undefined.

Ooops :o)

Add

#include <iomanip>
0
 
yajorosAuthor Commented:
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
 
yajorosAuthor Commented:
i mean result, not record sorry
0
 
itsmeandnobodyelseCommented:
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
 
itsmeandnobodyelseCommented:
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
 
yajorosAuthor Commented:
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
 
yajorosAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

  • 8
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now