String problem pls take a look

Please can anyone help me with this code.I dont know how to fix it.Take a look:
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
#include<iostream>
#include<string>
#include<cmath>
#define newline "\n"
using namespace std;
int main()
{
    string str;
    float yn;
    float n;
    float y1;
    float d;
    cout<<"Ky program sherben per te gjetur te panjohura te ndryshme\n"; 
    cout<<"ne nje progresion aritmetik.\n";
    cout << newline;
    cout<<"Kush eshte e panjohura e problemes?\n";
    cout << newline;
    cout<<"Shkruaj y1(per kufizen e pare),yn(per te fundit),n(per numrin\n";
    cout<<"e kufizave te vargut) ose d(per diferencen mes kufizave te njepasnjeshme).\n";
    getline(cin,str);
    if(str=y1)
    {
             cout<<"Jepni te dhenat e tjera :\n";
             cout<<"Sa eshte yn ? ";
             cin>>yn;
             cout<<"Sa eshte n ? "; 
             cin>>n;
             cout<<"Sa eshte d ? ";
             cin>>d;
    y1 = yn/((n-1)*d);
    cout<<"Kufiza e pare e vargut eshte : " <<y1;
}
    system("pause");
    return 0;
}

It gives me these error :
21 C:\Users\Administrator\Desktop\Untitled2.cpp [Warning] passing `float' for converting 1 of `std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(_CharT) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]'

and
21 C:\Users\Administrator\Desktop\Untitled2.cpp could not convert `(&str)->std::basic_string<_CharT, _Traits, _Alloc>::operator= [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((int)#`fix_trunc_expr' not supported by dump_expr#<expression error>))' to `bool'

I really need to finish this program.
Thanks in advance.
Well, for one, line 21 is using the assignment operator (=), rather than the comparison operator (==). That is causing the errors above.

The problem is slightly more involved than just that, though. A string and a float are two completely different types and cannot be compared. To compare them, one or the other must be converted so that they are the same type. Typically strings are converted to their numeric types in this case for efficiency. In this case, I think this is not fixing the root problem--there is a better way.

Rather than using getline on line 20, you could use cin and read a float (not a string). Then it could be compared against y1 more easily.


1
2
3
4
5
6
7
8
float input;
float y1 = 0.5;  // be sure to initialize y1 before testing against it
//...
cin >> input;
if( input == y1 )
{
    //...
}


I'm not sure if this is really your intention or not. I hope this helps.
Last edited on
Thanks dude but it gives me the same problem again :
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
#include<iostream>
#include<string>
#include<cmath>
#define newline "\n"
using namespace std;
int main()
{
    string x;
    float b;
    float c;
    float a;
    float d;
    cout<<"Ky program sherben per te gjetur te panjohura te ndryshme\n"; 
    cout<<"ne nje progresion aritmetik.\n";
    cout << newline;
    cout<<"Kush eshte e panjohura e problemes?\n";
    cout << newline;
    cout<<"Shkruaj a(per kufizen e pare),b(per te fundit),c(per numrin\n";
    cout<<"e kufizave te vargut) ose d(per diferencen mes kufizave te njepasnjeshme).\n";
    cin>>x;
    if(x=a)
    {
             cout<<"Jepni te dhenat e tjera :\n";
             cout<<"Sa eshte b ? ";
             cin>>b;
             cout<<"Sa eshte c ? "; 
             cin>>c;
             cout<<"Sa eshte d ? ";
             cin>>d;
    a = b/((c-1)*d);
    cout<<"Kufiza e pare e vargut eshte : " <<a;
}
    system("pause");
    return 0;
}

and errors
21 C:\Users\Administrator\Desktop\Untitled2.cpp [Warning] passing `float' for converting 1 of `std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(_CharT) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]'
21 C:\Users\Administrator\Desktop\Untitled2.cpp could not convert `(&x)->std::basic_string<_CharT, _Traits, _Alloc>::operator= [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((int)#`fix_trunc_expr' not supported by dump_expr#<expression error>))' to `bool'
and if I put == in place of = it gives 1 error
21 C:\Users\Administrator\Desktop\Untitled2.cpp no match for 'operator==' in 'x == a'
A couple of things:

1. Why are you trying to compare a string to a float? What are you trying to accomplish here?
2. #define newline "\n" Any reason you aren't just entering an additional new line character in your cout << ?
3. if(x=a) Your using the assignment operator. This should be ==.
4.
and if I put == in place of = it gives 1 error
21 C:\Users\Administrator\Desktop\Untitled2.cpp no match for 'operator==' in 'x == a'

Again, you are trying to compare a string (an object) to a float. There is now overloaded == for that.
Last edited on
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
#include<iostream>
#include<string>
#include<cmath>
#define newline "\n"
using namespace std;
int main()
{
    string x;
    float b;
    float c;
    float a;
    float d;
    cout<<"This program is used to find unknown values\n"; 
    cout<<"in a arithmetic progression.\n";
    cout << newline;
    cout<<"Which is the unknown of the problem?\n";
    cout << newline;
    cout<<"Write a(for the first number of the serie),b(per the last one),c(the number\n";
    cout<<"of all the values in the serie) or d(for the difference between the numbers).\n";
    cin>>x;
    if(x==a)
    {
             cout<<"Give the known values of the problem :\n";
             cout<<"How much is b ? ";
             cin>>b;
             cout<<"How much is c ? "; 
             cin>>c;
             cout<<"How much is ? ";
             cin>>d;
    a = b/((c-1)*d);
    cout<<"The first number of the serie(progression) is : " <<a;
}
    system("pause");
    return 0;
}

So this is in english version.
Can you help me to get rid of the problems.
Ok, I understand what your goal is, but I still don't see why you're inputing x as a string and what it's value is. Are you trying to use x for the users choice? For example, if the user wants to find out the first number in the series they enter a? What is x supposed to represent and why are you comparing it to a? Do you mean if(x=='a') ?
Last edited on
I thought using it so the program knows what is unknown.if user puts (a) letter the programs solves the equation for the first number of the serie,
than I was going to continue it because if user writes the (b) letter the program would try to find the last number of the serie,if he/she writes (c) the program would find the total number of values in the serie and (d) to find difference.Afcourse first the program will ask for known things to find the unknown.I hope you understand me and Is this possible???

I tried if(x=='a')
Gives the same error.
Last edited on
Ok so x is the users choice. If that's the case than x should be a character:

char x;

The user enters their choice:

1
2
3
4
5
6
cin >> x;

if ( x == 'a' )
       // calculate the first number in the series
else if ( x == 'b' )
       // calculate the last number in the series 


And so on...
Last edited on
Thanks a lot men,u helped me a lot :)
Lets hope no other problems get in my way !
Topic archived. No new replies allowed.