0

Here is the code I am working on (I'm a beginner and it is practice code). It works fine, but I can't cout the title and director. Other than that, it works fine... I'm using dev cpp.

#include <iostream>
#include <string>
using namespace std;

struct moviedata
{
    string title;
    string director;
    int year_released;
    double run_time;
};

void getinfo();
void showresult();

int main()
{
    getinfo();
    showresult();
    return 0;
}

void getinfo()
{
    moviedata m;

    cout << "What is title of movie?"<<endl;
    cin >> m.title;

    cout << "Who is the director of " << m.title << " ?"<<endl;
    cin >> m.director;

    cout << "What year was it released?"<<endl;
    cin >> m.year_released;

    cout << "What is the run time of " << m.title <<" ?"<<endl;
    cin >> m.run_time;
}

void showresult()
{
    moviedata m;

    cout << "You selected " << m.title << "directed by " << m.director ;

    cout << " which was released in " << m.year_released << " and is " << m.run_time <<" hours long"<<endl;
}

I need help. I can't cout string variables.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Since you're not initializing `m` in any way, the expected result would be no output. Is that what you're getting? You never specified the exact problem. – Mark Ransom Nov 04 '22 at 20:28
  • 1
    At a higher level: The `moviedata` object in `getinfo()` and the one in `showresult()` are two different objects, even though both are called `m`. That's because of "scope". (And then Mark's comment that the `m` in `showresult()` is default-initialized is correct). You either need to have one object in a scope that both functions can see, or pass the objects between functions by using parameters and return values. – Ben Voigt Nov 04 '22 at 20:30
  • The `m` variables in your two functions are distinct variables. This is very basic things explained in any good c++ book. – ChrisMM Nov 04 '22 at 20:30
  • FYI, the `m` variables will disappear after execution leaves the functions. You may want to *pass* the `m` variable by reference or constant reference if you don't change the parameter. – Thomas Matthews Nov 04 '22 at 20:38
  • Every `moviedata m;` declares & default initializes a separate object. You need a way of passing the data from one function to the other. You could e.g. change the signatures of the functions to return the object from `getinfo` and pass the data to `showresult` as function parameter by changing the signatures to `moviedata getinfo();` and `void showresult(moviedata const&);`. This requires an update of the function and `main` logic of course... – fabian Nov 04 '22 at 20:38
  • [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Jesper Juhl Nov 04 '22 at 20:39
  • You don't need the extra stuff that `std::endl` does. `'\n'` ends a line. And, note that `std::cout` and `std::cin` are tied together; when the code reads from `std::cin` it first flushes `std::cout`, so there's all the less reason for `std::endl` before reading. – Pete Becker Nov 04 '22 at 20:51

1 Answers1

1

The problem is not that you aen't able to print out strings. The real problem is that the strings you are printing are simply blank to begin with.

When main() calls getinfo(), a new moviedata object is created, populated, and then destroyed. When main() then calls showresult(), a new moviedata object is created, which is not populated, but is printed out. The strings in that second object have been default-constructed and not had any values assigned to them.

What you need to do is persist a single moviedata object between getinfo() and showresult() so you don't lose your data, for example:

#include <iostream>
#include <string>
using namespace std;

struct moviedata
{
    string title;
    string director;
    int year_released;
    double run_time;
};

void getinfo(moviedata&);
void showresult(const moviedata&);

int main()
{
    moviedata m;
    getinfo(m);
    showresult(m);
    return 0;
}

void getinfo(moviedata &m)
{
    cout << "What is title of movie?" << endl;
    cin >> m.title;

    cout << "Who is the director of " << m.title << " ?" << endl;
    cin >> m.director;

    cout << "What year was it released?" << endl;
    cin >> m.year_released;

    cout << "What is the run time of " << m.title << " ?" << endl;
    cin >> m.run_time;
}

void showresult(const moviedata &m)
{
    cout << "You selected " << m.title << " directed by " << m.director;
    cout << " which was released in " << m.year_released << " and is " << m.run_time << " hours long" << endl;
}

You can then take that a step further by moving getinfo() and showresult() into moviedata itself, for example:

#include <iostream>
#include <string>
using namespace std;

struct moviedata
{
    string title;
    string director;
    int year_released;
    double run_time;

    void getinfo();
    void showresult() const;
};

int main()
{
    moviedata m;
    m.getinfo();
    m.showresult();
    return 0;
}

void moviedata::getinfo()
{
    cout << "What is title of movie?" << endl;
    cin >> title;

    cout << "Who is the director of " << title << " ?" << endl;
    cin >> director;

    cout << "What year was it released?" << endl;
    cin >> year_released;

    cout << "What is the run time of " << title << " ?" << endl;
    cin >> run_time;
}

void moviedata::showresult() const
{
    cout << "You selected " << title << " directed by " << director;
    cout << " which was released in " << year_released << " and is " << run_time << " hours long" << endl;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770