-4

main.cpp:

#include "Login.h"

int main () {
    Login();
}

Login.h:

#ifndef LOGIN_H
#define LOGIN_H
#include <string>
#include <iostream>

using namespace std;


class Login
{
public:
    Login ();
    string getInput () {
        return input;
    }
    void setInput (string x) {
        x=input;
    }
private:
    string input;
};

#endif

Login.cpp:

#include "Login.h"

Login::Login ()
{
    Login lo;
    lo.setInput("hello");
    cout << lo.getInput();
};

I'm just learning to program and I'm trying to make a simple program to display input, but to use a class and object to do it, so I can learn how and ultimately make a program that starts with a login (hence all the "login" names).
When I run this it just crashes, and I have no idea why, or how i would search for a solution to this online, because I don't know even remotely what the problem is.

My question is two-fold:
1. Why is this just crashing?
2. How could I set the parameter in lo.setInput to a user input? (cin)

Sean McCallum
  • 158
  • 2
  • 10
  • 7
    You create a `Login` object in your `Login` constructor. That's an infinite loop. – Galik Sep 12 '14 at 22:47
  • Haha nevermind guys sorry i just realized i forgot the parenthesis after the function. THANKS for all the help! I was really confused, but i understand now that i didn't need a constructor at all. – Sean McCallum Sep 13 '14 at 18:05
  • 1
    You totally changed the question and now the answers don't make any sense! Does is still crash? Because that's in the question title. – Galik Sep 13 '14 at 18:47
  • Just found this again, sorry about that I had no idea what I was doing back then (if you couldn't tell). I rolled back to the original version. – Sean McCallum Feb 24 '18 at 07:55
  • [What does your step debugger tell you?](http://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) –  Feb 26 '18 at 21:51

3 Answers3

2

For 1,

Login::Login()
{
    Login lo; // here

You're recursively-calling Login constructor infinitely. When Login lo, the constructor Login::Login is called. it makes its new Login object, and repeat...

Probably you want this:

Login::Login()
{
    setInput("hello");
    cout << getInput();
}

For 2, just receive input and call with it.

string str;
getline(cin, str);
setInput(str);

In addition, C++ is too hard for newbies to study at first. I recommend you to start with other easier language, such as C, python, etc.

ikh
  • 10,119
  • 1
  • 31
  • 70
0

The problem is that your constructor recursively calls itself when it creates within its body local object lo

Login::Login ()
{
    Login lo;
    lo.setInput("hello");
    cout << lo.getInput();
};

Change it the following way

Login::Login ()
{
    setInput("hello");
    cout << getInput();
};

Though it would be better to define it like (or to allow the compiler to define it itself)

Login::Login () {}

and in main to write

Login lo;

lo.setInput("hello");
cout << lo.getInput();

Also function setInput is invalid

void setInput (string x) {
    x=input;
}

There must be

void setInput (string x) {
    input = x;
}

The class could be defined like

class Login
{
public:
    Login() = default;
    Login( const std::string &s ) : input( s ) {}

    std::string getInput () const { return input; }

    void setInput( const std::string &s ) { input = s; }

private:
    std::string input;
};
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

You may want to create an instance of the Login class and use its methods:

int main(void)
{
  Login my_login;
  cout << my_login.getInput();
  return 0;
}

Another possibility is to create static methods within the Login class. This would essentially be like grouping functions and variables into one package:

class Login
{
  public:
    static std::string getLoginText(void)
    {
        std::string text;
        cout << "Enter login ID: ";
        cin >> text;
        return text;
    }
};

int main(void)
{
  std::string login_text;
  login_text = Login::getLoginText();
  cout << "Person " << login_text << " is logged in\n";
  return 0;
}

Your original main program is confusing as to whether you want to create a temporary instance of the Login class, or call a function called Login. The above techniques make your code more readable by explicitly stating your intentions, rather than relying on unconventional techniques.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
  • I think there was some setting wrong with my original file because no matter what I changed the code to it still just terminated immediatly. I created another file using what you gave me, but i want to be able to create a function and put that definition in another .cpp file (to make main cleaner). – Sean McCallum Sep 13 '14 at 17:53
  • I updated the code to reflect the changes I made, yet I'm still having problems. – Sean McCallum Sep 13 '14 at 18:01