Storing information in C++?

Pages: 123
i am studying as much as i can, so i don't want people to mistake me for being a lazy asshole haha :P
and i don't want people to do my homework. i only want help on what i need and how i implement it in the coding
I gave you an example of a movie class and adding movies, if I do any more it would be your homework, you have to do the menu system, removing, and sorting.
okay well i will watch these videos and check on what you gave me and i will write later and see if i have understood anything lol
allthough i have to ask, can i use C coding in C++? and can i mix the codings? the videos i am watching seem to use only C.
closed account (j2NvC542)
If you really need to use C in C++, you can:
1
2
3
extern "C" {
    // C code goes here
}


But C and C++ are pretty much very similar anyway.
thanks, i have only just begun watching these vids and i am only at 4 of 15 folders containing lessons and he is only showing C so far. so i thought i must ask
This is a month-one assignment people, he doesn't need a movie class or sorting method. Have you been taught about vectors in class?

Do you know about switch cases, while loops, and arrays? Those are the three things you need to complete this assignment.
i wrote a 1500 words essay on what i know and don't know and it didn't post it.. so...

basicly i know too little about vectors, switch cases, while loops and arrays to actually efficiantly use them.
the reason i have a hard time learning this is because english is my second language, and reading / hearing about c++ programming contains to much technical talk. Instead of saying like "#include <iostream> is the start of the program" they get all technical and use words that i am not familiar with.


And to clarify things, i have no classes i can attend and no teachers to help me. i have to study this by myself and online since teachers are limited to only grading our work. And i have to do this because i want to go to college so i have to upp my grades and therefore i am limited to online studying.

i hope you understand what i mean
and i have to ask, why is it that i cant get into this webpage from time to time? i have been trying for several hours and well, here i am 9 hours later :S
closed account (j2NvC542)
Because the site is down sometimes. Quite often recently.
oh that explains it, makes me panik since i am under pressure getting stuff done lol
i really need some help :(
closed account (o3hC5Di1)
Hi there,

I'll try to help you best as I can, but please provide me following information first:

- What resource do you use to learn c++?
- What is your native language (if someone knows it perhaps they could help you)?
- What concepts have you learnt so far, just so we don't use things you haven't seen in your lessons so far.

If I don't reply fast, feel free to drop me a PM.

All the best,
NwN
1. the "lessons" here, and Lynda.com. and all the pages i can find.

2.Swedish is my native language but i do inderstand english really well, only if people use layman's terms. when it comes to technical stuff that is.

3. well i have learned stuff like this

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
#include <iostream>                                                       //början av programmet  
#include <cstdlib> 
#include <cstdio> 

using namespace std; 

int main() 
{ 
    while (1) 
    { 
        int choice; 
        setlocale(LC_ALL,"");                                             //språk ändring 
        cout << "Reseplanerare\n\n";                                      //huvud meny 
        cout << "Skriv in antal kilometer för att få fram resesätt\n\n";      
        cout << "Val: "; 
        cin >> choice; 
                 
        if(choice <= 2)              
        { 
            system ("CLS"); 
            cout << "Att promenera är att föredra\n\n";    
            system ("PAUSE"); 
            system ("CLS");        
        } 
        else if(choice >= 3 && choice <= 7)                               //istället för att skriva choice 3 upp till 7 så
        {                                                                 //använder jag mig av större och mindre än ( <= , >= )
            system ("CLS"); 
            cout << "Att cykla är att föredra\n\n";    
            system ("PAUSE"); 
            system ("CLS");    
                         
        } 
        else if (choice >= 8)
        { 
            system ("CLS"); 
            cout << "Att åka buss är att föredra\n\n";    
            system ("PAUSE"); 
            system ("CLS");                            
        }                     
    }           
}  


mostly i have the problem that i don't know where to put what in the code and how to use things ass effective as possible.

Thanks allot
closed account (o3hC5Di1)
Hi there,

Thanks for answering, that helps me understand your situation a little bit better.

Alright, now as for your assignment:

I'll use a very simple example, which you can directly convert to your situation.
Note that I won't use a vector, as I'm not yet familiar enough with those myself, but the program could easily be modified to use one.
Also, the way I'm presenting may help you understand vectors a little bit better.

I'll use the simple example of making a phone book.
Phone books exist of a long list of phone numbers, and the name of the person whom that number belongs to.

So in object oriented code, we can make a phone number look like this:

1
2
3
4
5
6
7
8
9
class Phonenumber {
    int number;
    std::string name;

    public:
        Phonenumber(int nr, std::string nm) : number(n),name(nm) {}
        int get_number() {return number;}
        std::string get_name {return name;}
};


int number and std::string name; are characteristics that describe a phone number. Now it's important to distinguish between a class and an object. Classes are to object what blueprints are to houses. The blueprint is not the house, the first has to be built (created) first. In a similar way, you need to create objects, in your case this could be done in main():

1
2
3
4
5
6
7
8
int main()
{
    Phonenumber nr1 = Phonenumber(3456789, "Kurt Wallander");

    //You will then be able to access the data by doing
    nr1.get_number();
    nr1.get_name();
}


When creating an object, it's constructor is automatically called. Constructors have the same name as the class, so in this case:

Phonenumber(int nr, std::string nm) : number(n),name(nm) {}

This makes sure that the objects internal data, number and name, are set to the data you pass when creating the object:

Phonenumber nr1 = Phonenumber(3456789, "Kurt Wallander");


In order to represent a list of phone numbers, we can create another class that will represent the phonebook:

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
class Phonebook {
    Phonenumber list[100]; //array of maximum 100 phonenumbers
    int list_count; //stores the amount of items in the list

    public:
        Phonebook() : list_count(0) {}  //constructor sets list count to zero
      
        void add(Phonenumber pn) 
        {
            //check should be added here that list is < 100 elements
            list[list_count] = pn //add phone number to list
            list_count++; //increase list count
        }

        Phonenumber get_by_name(std::string search)
        {
            for (int i=0; i<list_count; i++)  //for every number in the list
            {
                if (list[i].get_name().compare(search)  != 0 ) //if the name is the same as our search
                {
                    return list[i]; //return the phonenumber
                }
            }
            std::cout << "nothing found for " <<search; //if not found
            return Phonenumber(0, "not found");  //return empty phonenumber
        }
};


This is very basic of course, but it suffices for your purposes.
You would use it in main as such:

1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
    Phonenumber nr1 = Phonenumber(3456789, "Kurt Wallander");
    Phonenumber nr2 = Phonenumber(1237654, "Swedish Chef");
    
    Phonebook pb;
    pb.add(nr1);
    pb.add(nr2);

    Phonenumber search = pb.get_by_name("Kurt Wallander");
    std::cout << "Phonenumber sought: " << search.get_name() << " - " <<search.get_number();
}



Hopefully that makes sense to you.
Let us know if you have any further questions.

All the best,
NwN
thank you so much, i will try this right now and i will keep you updated how it goes.

Best wishes
i am really lost, trying to make it work and compile it but i seem to be doing allot wrong since i get allot of errors and i don't even know what i have done wrong
according to my instructions i should use vectors to store information while the program is running.

and i need to be able to print out the list of movies, store new movies and search for a stored movie.

But do i really have to use vectors? and well, i am to stressed to get this to work i think.

don't know what i should include in the program to make it work and where to put it.

i feel really stupid
oh and is that coding C or C++?
C++

what do you know about arrays? can you make an array of 10 ints and print them in a loop? can you make a two dimensional array?

you should watch those videos I linked to before on youtube, they explain things pretty well, I don't know how these lynda.com ones are but if you're having trouble then try something else.

is it this one? you really shouldn't be learning two programming languages at the same time (c/c++)

http://www.lynda.com/Eclipse-tutorials/CC-Essential-Training/94343-2.html

I'd also like to point out that the youtube tutorials come to a total of around 15 hours, 50% more than what you are paying for, heh.
Last edited on
Pages: 123