Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Invalid Conversion error... please help

Posted on 2004-08-30
16
Medium Priority
?
204 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
15 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 520 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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 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…

580 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