Data Structure program created with pointers

This is beatiful. 3 weeks ago I was struggling with the i/o syntax, loops and arrays.
My progress is slowly starting to show. I may be a slow newbie learner, but there is hope I guess. I've never copied any code, so I believe this is a big improvement.

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
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int i;
struct movies{
string title;
int year;
};

struct friends{
string name;
string email;
movies favorite_movie;
};

void printmovie(friends* user){
cout << "(" << (i+1) << ")" << endl;
cout << "Your name is " << user->name;
cout << " & your email adress is " << user->email;
cout << endl << "Your favorite movie is " << user->favorite_movie.title << " (" << user->favorite_movie.year << ")." << endl << endl;
}

int main(){
int x;
cout << "How many members to input?: "; string str; getline(cin, str); stringstream(str) >> x;
friends member[x];
friends* p_member = member;
    
    for(i = 0; i < x; i++){
    cout << "(" << (i+1) << ")" << endl;
    cout << "Enter name: "; getline(cin, (p_member+i)->name);
    cout << "Enter Email: "; getline(cin, (p_member+i)->email);
    cout << "Enter title of favorite movie: "; getline(cin, (p_member+i)->favorite_movie.title);
    cout << "Enter year of publishment: "; string str; getline(cin, str); stringstream(str) >> (p_member+i)->favorite_movie.year;
    system("cls");
    }
system("cls");
    for(i = 0; i < x; i++){
    printmovie((p_member+i));
    }
p_member = NULL;
delete p_member;
}


Is this good enough for a month of learning c++?
Don't use global variables (line 5, int i) if you don't have to, you could just define the int i=0 in the separate for loops.

Also multiple statements in one line, as in line 26, is confusing. It makes it more difficult to read code for other programmers.

In line 26 why did you use a string for the number of members to input, and then convert to int for x? You could have just used cin >> x;

And in your struct moves, variable year is a int data type, but again you've used stringstream to put a string in it on line 35

Maybe you were just testing your abilities, and that's why you did things this way. If that's the case, you did well. !

Last edited on
Thanks. Yeah it was pretty much a test on how many different ways I could create this.

I did it with simple structure syntax, then with structure and arrays, then with pointers and arrays. Later I may do it with vectors if possible.

The reason why I use getline alot, and converting string to int on line 26. Is because I believe it tends to cause less bugs and crashes if something goes wrong.
Right, when publishing software to users who don't follow convention it is best practice to make sure the user input is always controlled. Good work. I've been learning for about a month and a half as well, and I can't see anything I don't understand! Good work
Last edited on
Thankyou ;)
Topic archived. No new replies allowed.