No clue

Hi, got a problem with my code. Keeps saying error : cant convert string to int. But Im not trying to do that :D Cant figure out why it thinks that. Sorry for the language, im latvian and its for my school work.

error: cannot convert ‘std::string’ {aka ‘std::__cxx11::basic_string’} to ‘int’
39 | valm.info(lvalm, pvalm, tvalm);
| ^~~~~

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
#include<iostream>
using namespace std;
 
class Koncerts {
    private:
        string location;
        double price;
        int tickets_sold;
    public:
        void info(int t, string l, double p) { //funkcija, kurai ļauts mainīt privātos datu laukus
            location = l;
            price = p;
            tickets_sold = t;
        }
        
        int summa() {
            return price*tickets_sold;
        }
    
};
 
int main () {
    Koncerts valm;
        string lvalm;
        double pvalm;
        int tvalm;
        cout << "Ievadi koncerta lokāciju - "; cin >> lvalm;
        cout << "Ievadi biļetes cenu - "; cin >> pvalm;
        cout << "Ievadi pārdoto biļešu skaitu - "; cin >> tvalm;
        
        valm.info(lvalm, pvalm, tvalm);
        
        cout << "Ienākumi no biļešu pārdošanas - " << valm.summa() << "\n";
}
Last edited on
It means what it says! See the bold text in the code..
C++ does not automatically convert strings to int or double. You have to do that explicitly, there are functions to do so, stoi(), to_string(), and others depending on what exactly you want.

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
#include<iostream>
using namespace std;
 
class Koncerts {
    private:
        string location;
        double price;
        int tickets_sold;
    public:
        void info(int t, string l, double p) { //funkcija, kurai ļauts mainīt privātos datu laukus
            location = l;
            price = p;
            tickets_sold = t;
        }
        
        int summa() {
            return price*tickets_sold;
        }
    
};
 
int main () {
    Koncerts valm;
        string lvalm;
        double pvalm;
        int tvalm;
        cout << "Ievadi koncerta lokāciju - "; cin >> lvalm;
        cout << "Ievadi biļetes cenu - "; cin >> pvalm;
        cout << "Ievadi pārdoto biļešu skaitu - "; cin >> tvalm;
        
        valm.info(lvalm, pvalm, tvalm);
        
        cout << "Ienākumi no biļešu pārdošanas - " << valm.summa() << "\n";
}
Last edited on
Sorry, didn't know that the sequence in 2 different positions mattered
A lot of languages do automatic translations between types as needed. C++ is not one of them. You would call it “strictly typed”. (It is also statically typed — which is to say, the types of things are known at compile-time, and cannot be changed while the program is running.)

So if you want something to be an integer, you must convert it to an integer. Strings to integers is a common enough operation you get functions to do it for you. I personally prefer to use this for these kinds of things:

1
2
3
4
5
6
7
8
9
10
11
12
#include <sstream>
#include <string>
#include <optional>

template <typename T>
auto string_to( const std::string & s )
{
  T value;
  return (std::istringstream( s ) >> value >> std::ws).eof()
    ? value
    : std::optional <T> {};
}
1
2
    ...
    valm.info(string_to<int>(lvalm).value_or( 0 ), pvalm, tvalm)

Unless something goes wrong, the above is roughly equivalent to:
1
2
3
4
#include <string>

    ...
    valm.info(stoi(lvalm), pvalm, tvalm);

The difference is that my string_to<type>() function will always return zero (or whatever value you specify for error) on error, where stoi() may throw an exception and will accept inputs like “7abc” without complaining.

You can even catch the error and complain if you wish:

1
2
3
4
5
6
7
    auto lvalm_optional = string_to<int>(lvalm);
    if (!lvalm_optional)
    {
        cerr << "You must specify at least one ticket.\n";
        return 1;
    }
    lvalm.info(*lvalm_optional, pvalm, tvalm);

For homework problems I recommend you just stick with atoi() — unless your professor specifically says that you must check for bad input, you can pretty safely assume that all input is good and write your code as such.

Hope this helps.
Also, instead of the info function, write a constructor with initialiser list:

https://en.cppreference.com/w/cpp/language/constructor

You could convert this page into Latvian with google translate if that makes it easier :+)
Have we attracted another report button pusher?
By the look of things in another thread by the OP I''m inclined to say yes, Duthomas.

I wish it was otherwise.
It's because in the info function you put int first, string second and double third. It needs to be in that order.

In valm.info(lvalm, pvalm, tvalm); you have string first, double second and int third.

Change line 31 to this: valm.info(tvalm, lvalm, pvalm);
Topic archived. No new replies allowed.