hello, i got a error, declaration of string shadows a parameter...mind help me with it

#include <fstream>
#include <string>

using namespace std;

int numToRom(string&,string&,string&);

int main()
{
    string data;
    string result;
    string romNum;

    cout<<"Please enter a number"<<endl;
    cin >> data;
    numToRom(data, result, romNum);
    cout<<"Output :    "<<result<<endl;
}

int numToRom(string& data, string& result, string& romNum)
{

    string romNum[] =
    { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };

    int num;
    num = atoi(data.c_str());



    for(int i = 0; i<data.length(); i++)
    {
        if(num <= 0 || !isdigit(data[i]))
        {
            result = "Don't Type zero or alphabet";
        }
        else if(num >= data[i])
        {
           num -= data[i];
           result.append(romNum[i]);

        }

    }
}

thanks a million

Recommended Answers

All 7 Replies

Ok, first, the code won't compile without #include <iostream> As for the shadowing. What have you written is:

int myfunction(int a_number){
    int a_number = 4;
    //do stuffz
    return some_num;
}

See the problem? You are creating a variable a_number at the moment the function starts (in line 1 of my code). Immediately after that you are declaring another variable with same name (in line 2).

EDIT: and for the worst, you aren't declaring the same thing. Function parameter is string type, and your declared variable is string[] type.

I figure the problem might be in declaring a local variable called rowNum while at the same time having a parameter called the same. Also, your function doesn't return anything. For future reference please post the errors/warnings you get too.

i see..i think i know how to solve...already...but now i got 1 more prob

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int numToRom(string&,string&,string&,int&);

int main()
{
    string data;
    string result;
    string araNum;
    int v;

    cout<<"Please enter a number"<<endl;
    cin >> data;
    numToRom(data, result, araNum, v);
    cout<<"Output :    "<<result<<endl;
}

int numToRom(string& data, string& result, string& romNum, int &v)
{
    int values[] = { 1000, 900, 500, 400, 100,90, 50, 40, 10, 9, 5, 4, 1 };
    string araNum[] = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };

    int anum;
    anum = atoi(data.c_str());

    for(int a = 0; a<data.length(); a++)
    {
        if(anum <= 0 || !isdigit(data[a]))
        {
            result = "invalid";
        }

    }
    for(int i = 0; i<13; i++)
        {
           while (anum >= values[i])
           {
           anum -= values[i];
           result.append(araNum[i]);

           }

    }
        }

My expected result is when is type any digit = it will convert to roman...but if i type like 123a...it will return invalid.. but when i try...it will return invalid with conversion for 123...can someone assist me or give me hint for solving it.

That's because... let's say you write 123c.
First for loops finds error, and inserts "invalid" into result. But then it simply continues to another loop. You have to either return inside that if statement or insert some flag that checks if "invalid" is, and if it is, don't go with second for-loop. (by the way, try tagging code with program name: , that way your code will be numbered, so I can tell you: for loop in line 3, switch in line 13... etc)[code=cplusplus], that way your code will be numbered, so I can tell you: for loop in line 3, switch in line 13... etc)

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int numToRom(string&,string&,string&,int&);

int main()
{
    string data;
    string result;
    string araNum;
    int v;

    cout<<"Please enter a number"<<endl;
    cin >> data;
    numToRom(data, result, araNum, v);
    cout<<"Output :    "<<result<<endl;
}

int numToRom(string& data, string& result, string& romNum, int &v)
{
    int values[] = { 1000, 900, 500, 400, 100,90, 50, 40, 10, 9, 5, 4, 1 };
    string araNum[] = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };

    int anum;
    anum = atoi(data.c_str());

    for(int a = 0; a<data.length(); a++)
    {
        if(anum <= 0 || !isdigit(data[a]))
        {
            result = "invalid";
        }

    }
    for(int i = 0; i<13; i++)
        {
           while (anum >= values[i])
           {
           anum -= values[i];
           result.append(araNum[i]);

           }

    }
        }

I see...so the whole for loop is the problem why invalid and it still do conversion right...haha so now what should i do...i no idea what to do so when invalid..it will just put invalid and dun do the conversion

anyone there can help ><

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.