Solved

Invalid Conversion error... please help

Posted on 2004-08-30
16
197 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
[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
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

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

Suggested Solutions

Title # Comments Views Activity
Lambda for random numbers problem 7 121
Parsing XML instructions in Java/any other language.... 3 121
Unable to start eclipse ? 17 163
Finding Divisors 5 39
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…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

733 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