Hi again, I ran to some problems in input some codes but i'm quite sure that my code is correct but the output is not what I expect it to be, this is my code:

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

struct movie{
    string title;
    int year;
};
struct info{
    string name;
    movie fmovie;
};

void main(){
    info xinfo;

    cout<<"Enter name: ";
    getline(cin, xinfo.name);

    cout<<"Enter movie: ";
    getline(cin, xinfo.fmovie.title);

    cout<<"Enter year: ";
    cin>>xinfo.fmovie.year;

    cout<<"Name is "<<xinfo.name<<endl;
    cout<<"Movie is "<<xinfo.fmovie.title<<endl;
    cout<<"Year is "<<xinfo.fmovie.year<<endl;
}

When I run this, it asks for the name but when I type in my name and press enter it doesn't proceed to asking for the movie, it just hangs there in the newline doing nothing, so I press enter again, and it asks for the movie now, i press enter and its asks for the year now, but then in the final out only name is shown and the other two is fucked up..why is this?

output looks like this:

Enter name: john

Enter movie: avatar
Enter year:

Name is john
Movie is
Year is -85999284

Recommended Answers

All 5 Replies

what compiler are you using? I know Visual Studio 6.0 had a bug that did that but it was fixed with one of its patches. Write another tiny program just to test if your compiler has a similar problem.

im using microsoft visual c++ 6.0, so do I have to get a better compiler then?

get vc++ 2010 express -- its free, so is Code::Blocks/MinGW compiler. Or, download an install SP6 for your compiler from Microsoft web site. I don't know if its still available though.

As an aside, you will want to use int as the return type of main() in the future. Strictly speaking, void main() isn't legal C++, even if some compilers accept it. Just a piece of advice, which you may need to know about as most newer compilers will flag void main() as erroneous, or at least give a warning.

so my code isn't wrong at all then?, well thanks guys!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.