Invalid conversion from 'const char' to 'char'....?

Hello. I get the error message Invalid conversion from 'const char' to 'char' when I try to run this program.
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
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    int j;
    double kapital, procent;
    char ch="j";
    cout<<setiosflags(ios::fixed)<<setprecision(2);

    while(ch!='n'&&ch!='N'){

        cout<<"\nMata in insättningen i kr: "; cin>>kapital;
        cout<<"\nMata in räntesatsen i procent: "; cin>>procent;

        cout<<endl<<setw(3)<<"År"<<setw(9)<<"Kapital"<<endl<<endl;

        for (j=0; j<10; j++){
        cout<<setw(3)<<j<<setw(9)<<kapital<<"kr"<<endl;
        kapital=kapital+kapital*procent/100;

        }
    cout<<"\nVill du fortsätta? (j/n)? "; ch=std::cin.get();

    }


    return 0;
}


Now I dont expect anyone to know Swedish so I will translate here:

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
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    int j;
    double Capital, procent;
    char ch="j";
    cout<<setiosflags(ios::fixed)<<setprecision(2);

    while(ch!='n'&&ch!='N'){

        cout<<"\nInput the deposit in kr: "; cin>>kapital;
        cout<<"\nInput the interest in procent: "; cin>>procent;

        cout<<endl<<setw(3)<<"year"<<setw(9)<<"Capital"<<endl<<endl;

        for (j=0; j<10; j++){
        cout<<setw(3)<<j<<setw(9)<<Capital<<"kr"<<endl;
        Capital=Capital+Capital*procent/100;

        }
    cout<<"\nDo you wish to continue? (j/n)? "; ch=std::cin.get();

    }


    return 0;
}


This is from an example in our programming book. If I havent translated everything correctly you will have to forgive me since English is not my main language. Also I wonder how Capital=Capital+Capital can be? I mean it sounds very strange to declare a variable using the same variable? How can that be?

This could be your problem:
char ch="j";

You make a char variable, i.e. a single character. Now, "j" in double quotes is a string literal. That is, it is a constant string of characters (technically, a pointer to a constant string of characters), which in this case just happens to be one character.

If you want to represent a single character as a character type, put it in single quote marks:
'j'

Question 2:
I wonder how Capital=Capital+Capital can be?

Obviously, it doesn't make sense as a mathematical equation. However, as far as our program is concerned, the original value of capital is added to itself, and then this new value is assigned to the variable. So the value of Capital doesn't change until after the addition is performed.
Oh, thanks. I will try it.

Regarding the variable:

Yeah it seemed strange to me, I guess that it is a little "mindfuck" going from math to programming.
Topic archived. No new replies allowed.