invalid-conversion-from-const-char-to-char

Pages: 12
invalid conversion from const-char to char. This has something to do with choice and the if statements that I generated. Any help?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
{
    int s;
    s=c+d;
    return (s);
}
int multiplication (int e, int f)
{
    int t;
    t=e*f;
    return (t);
}
int devision (int g, int h)
{
    int u;
    u=g/h;
    return (u);
}
    
int main ()
{
  int x,y,result;
  char choice;
  cout<<"Would You Like To Add, Subtract, Multiply or Divide"<< '\n';
  cout<<"Please Enter Add/Subtract/Multiply/Divide"<< '\n';
  cout<<": ";
  cin>>choice;
  if (choice = "Add")
     result = addition (x,y);
     cout<<"The A Result Is: "<<result<<'\n';
  if (choice = "Subtract")
     result = subtraction (x,y);
     cout<<"The S Result Is: "<<result<<'\n';
  if (choice = "Multiply")
     result = multiplication (x,y);
     cout<<"The M Result Is: "<<result<<'\n';
  if (choice = "Divide")
     result = devision (x,y);
     cout<<"The D Result Is: "<<result<<'\n';
     std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
     return 0;
}
In your if statements, you are trying to compare a single character (choice) to a string of characters ("Add" and such). Also note that you are trying to compare with = when you need to use ==.
to clarify:

char holds a single character

If you want multiple characters... like a whole word, you want to use std::string (include the <string> header)
Last edited on
ok thanks, how would I implement the std::stringinto my program as I have been very used to defining variables at the top of the function.

I'm not sure if you need to include it somewhere else?
1
2
3
4
5
6
7
#include <string>

int main()
{
   std::string choice;
   // other stuff
} 
thanks dude, appreciate it, I tried a char array for choice but I'l try this as i'm still having problems with it =]
IceyEyez wrote:
I have been very used to defining variables at the top of the function.
You should decide whether you want to use C or C++. In C++ you can define variables at any point in a function - normally you define them when you are about to start using them. Defining them all up-front doesn't really make sense in programming, unlike in real life where you would want all ingredients at once to start cooking. ;)
Last edited on
closed account (3qX21hU5)
You also have undefined behavoir since you dont define x or y but you use them in your functions..

int x,y,result; result = addition (x,y);

Also there is no need to declare a temp variable in your functions just return the result of your expression.

1
2
3
4
5
6
int devision (int g, int h)
{
    int u;
    u=g/h;
    return (u);
}


is the same as

1
2
3
4
int devision (int g, int h)
{
    return g / h;
}


Also in your if condtions you are using the assignment operator = instead you should be using the == equality operator.

IE it should be if (choice == "Add")

Last edited on
@Zereo, Thanks so much for all your tips =]. I have altered the current issues but I still have an issue with running it.

I can't seem to get the response I want. Open this gyazo to see the CMD output I get: http://gyazo.com/7c477d90b67f198617e873ea2c63146d

I then enter "Add" next to the colons and I get no other output. At line 38 I am meant to get a prompt to enter "Number 1". It doesn't show. The CMD indicator just goes down onto the next line with no output. Any Ideas?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <string>
using namespace std;

int x,y,result,c,d,e,f,g,h;
char choice[10];

int subtraction (int a, int b)
{
    int r;
    return a-b;
}
int addition (int c, int d)
{
    int s;
    return c+d;
}
int multiplication (int e, int f)
{
    int t;
    return e*f;
}
int devision (int g, int h)
{
    int u;
    return g/h;
}

int main ()
{
cout<<"Would You Like To Add, Subtract, Multiply or Divide?"<< '\n';
cout<<"\n";
cout<<"Please Enter Add/Subtract/Multiply/Divide"<< '\n';
cout<<": ";
cin>>choice;
{
if (choice == "Add")
     cout<<"Number 1: ";
     cin>>c;
     cout<<"\nNumber 2: ";
     cin>>d;
        result = addition (x,y);
            cout<<"The Result Is: "<<result<<'\n';
}
{
if (choice == "Subtract")
     cout<<"Number 1: ";
     cin>>c;
     cout<<"\nNumber 2: ";
     cin>>d;
        result = subtraction (x,y);
            cout<<"The Result Is: "<<result<<'\n';
}
{
if (choice == "Multiply")
     cout<<"Number 1: ";
     cin>>e;
     cout<<"\nNumber 2: ";
     cin>>f;
        result = multiplication (x,y);
            cout<<"The Result Is: "<<result<<'\n';
}
{
if (choice == "Divide")
     cout<<"Number 1: ";
     cin>>g;
     cout<<"\nNumber 2: ";
     cin>>h;
        result = devision (x,y);
            cout<<"The Result Is: "<<result<<'\n';
}
return 0;
}
char choice[10];

Change it to:

std::string choice;

The problem is that when you wrote choice == "Add", you're comparing if the memory address of "Add" matches the memory address of choice, which can never be true. std::string will properly compare to see if it matches the contents of "Add".
In C++ you can define variables at any point in a function - normally you define them when you are about to start using them

It's the same way in C, by the way.
@LB

This still doesn't fix the issue. I'm not sure why this is happening. I have moved the declaration from global to local in main() but it still hasn't fixed it.
The using of braces in this code snip is invalid and has no any sense.

1
2
3
4
5
6
7
8
9
{
if (choice == "Subtract")
     cout<<"Number 1: ";
     cin>>c;
     cout<<"\nNumber 2: ";
     cin>>d;
        result = subtraction (x,y);
            cout<<"The Result Is: "<<result<<'\n';
}


It would be more correctly tp write


1
2
3
4
5
6
7
8
9
if (choice == "Subtract")
{
     cout<<"Number 1: ";
     cin>>c;
     cout<<"\nNumber 2: ";
     cin>>d;
        result = subtraction (x,y);
            cout<<"The Result Is: "<<result<<'\n';
}


Also you entered values for variables c and d but called the function with x and y.

It seems you do not understand what you are doing.
Last edited on
closed account (3qX21hU5)
I did a bunch of reworks. Most of the things I just commented out when they weren't suppose to be there and then added what was suppose to be there to show you.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
#include <string>
using namespace std;

// int x,y,result,c,d,e,f,g,h;  //Move these to local scope
// char choice[10];             // NEVER use global if you don't have to

int subtraction (int a, int b)
{
    //int r;  Delete this
    return a-b;
}
int addition (int c, int d)
{
    // int s; Delete this
    return c+d;
}
int multiplication (int e, int f)
{
    // int t;  Delete this
    return e*f;
}
int devision (int g, int h)
{
    // int u;Delete this
    return g/h;
}

int main ()
{
    // int x,y,result,c,d,e,f,g,h; You don't need all these I decaled what you do below
    // char choice[10]; // Change to string
    string choice;
    int x, y;
    int result;

    cout<<"Would You Like To Add, Subtract, Multiply or Divide?"<< '\n';
    cout<<"\n";
    cout<<"Please Enter Add/Subtract/Multiply/Divide"<< '\n';
    cout<<": ";
    cin>>choice;

    // Below here you had a lot of problems. One was using opening brackets before your
    // If statements which should come after

    // Another your use off all different variable to store info that you don't need
    // I deleted all the variables except x and y (Which you were still using uninitilized)

    if (choice == "Add")
    {                           // You had this before the if statement they come after
        cout<<"Number 1: ";
        // cin>>c;              // Changed to x
        cin >> x;
        cout<<"\nNumber 2: ";
        // cin>>d;              // Changed to y
        cin >> y;
        result = addition (x,y);
        cout<<"The Result Is: "<<result<<'\n';
    }
    else if (choice == "Subtract") // Changed to else if
    {
        cout<<"Number 1: ";
        cin >> x;
        cout<<"\nNumber 2: ";
        cin >> y;
        result = subtraction (x,y);
        cout<<"The Result Is: "<<result<<'\n';
    }
    else if (choice == "Multiply") // Changed to else if
    {
        cout<<"Number 1: ";
        cin >> x;
        cout<<"\nNumber 2: ";
        cin >> y;
        result = multiplication (x,y);
        cout<<"The Result Is: "<<result<<'\n';
    }
    else if (choice == "Divide") // Changed to else if
    {
        cout<<"Number 1: ";
        cin >> x;
        cout<<"\nNumber 2: ";
        cin >> y;
        result = devision (x,y);
        cout<<"The Result Is: "<<result<<'\n';
    }

    return 0;
}


Make sure to study the code and what I have changed so you understand why there were errors and what bad practices you were using. Don't mean that in a harsh way either, we all were just beginners at some point and all did the same things. Just be sure to learn from it and not just copy and paste it and call it done.


EDIT: Also you might want to add into your if condition something like this,

if (choice == "Add" || choice == "add") which is just saying if choice equals "Add" OR if choice equals "add" (Notice lowercase 'a') run this if condition.

This would be a good idea because a lot of the times users won't take the time to uppercase the first letter of the word and if they don't in your current program it won't work.
Last edited on
You are correct in saying that I am a very naive coder as I have only just started doing this recently, but even you have been in this stage so I just think of it as making silly mistakes in order to gain experience.

I used x,y as I looked at the documentation for (I believe it was) functions and they used an example similar to this. I made a simple addition calculator using x,y and it worked correctly as they are meant to be replaced by the user input. I have just tried to expand it using more functions and it looks like I have tripped up on something.

Somebody mentioned std::string choice as the memory values weren't comparable and this hasn't solved it. I'm wondering if it is multiple code issues or just one single issue that is causing this.

Thanks for your input though I will make sure to correct the brackets =]
This now came up. Something to do with a lack of ability to open an output file.

http://gyazo.com/9df0fe26153fda3d698447416c34fb6e.png?1364831669
@Cubbi boy I would love to use that standard of C, but times have changed. http://ideone.com/jxCrIu

@IceyEyez: Did you make sure you closed the program? That error is saying it can't build your new version of the program because you still have it opened.

Can you also post exactly what your current code is?
@LB I did close the program and reopen it but I did change from Dev C++ to CodeBlocks as people said it was a better IDE.

Current "Output File" error: http://gyazo.com/3913455e2f7705957fc66668bfcf276e
Anyway, here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include <string>
using namespace std;

int x,y,result,c,d,e,f,g,h;

int subtraction (int a, int b)
{
    int r;
    return a-b;
}
int addition (int c, int d)
{
    int s;
    return c+d;
}
int multiplication (int e, int f)
{
    int t;
    return e*f;
}
int devision (int g, int h)
{
    int u;
    return g/h;
}

int main ()
{
    std::string choice;
    cout<<"Would You Like To Add, Subtract, Multiply or Divide?"<< '\n';
    cout<<"\n";
    cout<<"Please Enter Add/Subtract/Multiply/Divide"<< '\n';
    cout<<": ";
    cin>>choice;

if (choice == "Add")
{
     cout<<"Number 1: ";
     cin>>c;
     cout<<"\nNumber 2: ";
     cin>>d;
        result = addition (x,y);
            cout<<"The Result Is: "<<result<<'\n';
}
if (choice == "Subtract")
{
     cout<<"Number 1: ";
     cin>>c;
     cout<<"\nNumber 2: ";
     cin>>d;
        result = subtraction (x,y);
            cout<<"The Result Is: "<<result<<'\n';
}
if (choice == "Multiply")
{
     cout<<"Number 1: ";
     cin>>e;
     cout<<"\nNumber 2: ";
     cin>>f;
        result = multiplication (x,y);
            cout<<"The Result Is: "<<result<<'\n';
}
if (choice == "Divide")
{
     cout<<"Number 1: ";
     cin>>g;
     cout<<"\nNumber 2: ";
     cin>>h;
        result = devision (x,y);
            cout<<"The Result Is: "<<result<<'\n';
}
return 0;
}
@Zeroe

I literally just saw your post on reworked code, so a lot of this I will be changing due to your post. Thanks for all your support
IceyEyez wrote:
@LB I did close the program and reopen it
No, it needs to close and stay closed. If it is open, your IDE can't save the new version of it.
Pages: 12