-3

I created a class where I am defining member variables of string and integer data types.

When the function is called for a new object, I type in the string value, press Enter, and all the variables left are given some default value, and my program ends.

I do not know how to stop it from just ending, and letting me type each string value.

Here is a copy of my code:

#include <iostream>     //Header file needed to do inputs and outputs
#include <iomanip>      //Header file needed to use setw
#include <math.h>       //Header file needed to use power function
using namespace std;

class MovieData
{
    private:
        string title, director;
        int year, runTime;

    public:
        void setInfo()
        {  
            cout << "Movie title: ";
            cin >> title;
            cout << "Diector: ";
            cin >> director;
            cout << "Year: ";
            cin >> year;
            cout << "Runnign Time: ";
            runTime;
        }
        void displayInfo ()
        {
            cout << "Movie title " << title << endl;
            cout << "Director " << director << endl;
            cout << "Year " << year << endl;
            cout << "Run Time " << runTime << endl;
        }
};

int main() {
    MovieData movie1, movie2;

    movie1.setInfo();
    movie2.setInfo();

    movie1.displayInfo();
    movie2.displayInfo();

    return 0;
}

image

I tried defining string variables as c-string, so character arrays, to see if that would help the issue, but it is the same.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Sergio
  • 29
  • 1
  • *"all the variables left are given some default value, and my program ends."* -- setting the director to "of" does not look like a default value. Especially when "of" is something you typed. Typed right after what was saved as the movie title ("Lord")... Speaking of which, do you not consider the shortened movie title to be a symptom? – JaMiT Feb 16 '23 at 06:06
  • Text-based output should be pasted as text, not as an image. Put it in a code block to preserve the formatting. – JaMiT Feb 16 '23 at 06:07
  • With a bit more debugging on your part, you might be able to simplify your example to something that looks more like ["cin" only reads the first word](https://stackoverflow.com/q/9469264/). – JaMiT Feb 16 '23 at 06:12

2 Answers2

1

Change this line

runTime;

to

cin >> runTime;
1

A couple of issues pointed out in the other comments and answers, but the main issue is this code:

cout << "Movie title: ";
cin >> title;
cout << "Director: ";
cin >> director;

Using >> on a string reads a single word only, so if you type Lord of the Rings, then title will equal "Lord" and director will equal "of", etc.

If you want to read a whole line of text then use std::getline():

cout << "Movie title: ";
getline(cin, title);
cout << "Director: ";
getline(cin, director);

When you start using getline(), it's very important to read (and understand) this post:

Why does std::getline() skip input after a formatted extraction?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
john
  • 85,011
  • 4
  • 57
  • 81