Thread: Lot of questions.

  1. #1
    C++ noob Ryan0773's Avatar
    Join Date
    Jan 2009
    Posts
    43

    Lot of questions.

    I haven't used C++ for that long and I have a few questions. I checked the FAQ which didn't have the answers i was looking for.

    -When do I know when to put the int main() into my program? I was looking at the tutorial and it's usually right at the begginning right after "using namespace std;" but in the structure tutorial (basic) it's input later.

    -Are the spaces neccessary after brackets and such?

    -What exactly is an argument? It's been mentioned before but it never really went into detail (unless it did and I forgot) Sorry if this is a stupid question.

    -I have no clue what the use of a pointer is...

    -When i tried to write an extremely basic file that would show a certain text, only the first word came up. This is the code

    Code:
    #include <fstream>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        char str[10];
        
        ofstream a_file ( "example.txt" );
        a_file<<"This text will now be inside example.txt";
        a_file.close();
        ifstream b_file ("example.txt" );
        b_file>> str;
        cout<< str <<"\n";
        cin.get();
    }
    the only word it shows is "This" If you spot an error, please tell me.

    -What's the point of structures and classes? they dont seem to do much.

    -How can i make a loop that will make something happen at least twice? When i try and make a do...while loop it either only does it once or it keeps doing it into infinity. how would i make it repeat only 5 time?


    I know that these are all extremely basic things that i should probably know, but from the tutorials i just couldn't figure out what the heck some of this stuff does.

    Any help would be appreciated.

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    1) It does not matter where you put main(). Actually, you want to include whatever file you will need and define your typedefs, globals and such before it but that's pretty much it. It does not matter if you implement other functions before or after it.

    2) No but sometimes they help make your code more readable. Use with good taste.

    3) It is a parameter (either value or variable) of a function.

    4) There have been a few threads about pointers lately. Search for these threads.

    5) This is because the >> operator will only read words separated by ' '. What you want to do is read sentences that are terminated by '\n'. You'll need to use the getline() function.

    6) They represent concepts that (most of the time) are needed in many instances. For example, if you are making a game, you could have let's say a hundred monsters which all share the same characteristics : AI, health, speed, etc. Without classes, you'd need thousands and thousands of variables and it would become quite an ugly mess. You don't want that. Classes and structs make you able to group those characteristics and create a new type that you may call Monster, for example.

    7) There are many ways to do this. Each loop has its uses. In this case you'd want to do something like this:

    Code:
    for(int i = 0; i < 5; i++)
    {
        std::cout << i;
    }

  3. #3
    C++ noob Ryan0773's Avatar
    Join Date
    Jan 2009
    Posts
    43

    Talking Wow

    Wow, i din't expect someone to reply that fast.

    Thanks for the help, ill look into the pointers.

    just one thing about the loop, your code makes it input integers while the statment is true. What would i have to write if i wanted to make a loop of a sentence?


    Thanks alot for the help!!!

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    903
    I'm afraid I don't understand what your question is. What do you mean by "make a loop of a sentence" ?

  5. #5
    C++ noob Ryan0773's Avatar
    Join Date
    Jan 2009
    Posts
    43

    ...

    Like if I want my program to make a text using cout<< how could i loop it so that whatever the text thta cout<< has made will be said again 5 times without me actually making another cout function.

    for example, lets say i have a do...while statement like this

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main ()
    {
        int x = 0;
        
        do {
            cout<<"Hello World!!!\n";
           } while (x != 0); 
           cin.get ();
    }
    this will write down Hello World!!! once because it is written before the while function shows that x does equal 0, making the statement false and stopping the loop. I want to make it so that the statement loop at least twice and then have it stop.

    Sorry if this doesn't clear it up.

  6. #6
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    If I understand correctly you want something like this:
    Code:
    for (int i = 0; i < 5; ++i)
    {
       std::cout << "Hey, dude" << std::endl;
    }
    which will print 5 times "Hey, dude". The loop works like this:
    1) A variable named "i" is declared and set to 0.
    2) There is a condition. In this occasion if i is less than 5
    3) If the above condition is true the code inside the loop is executed. In this case the "std::cout << "Hey, dude" << std::endl;"
    4) If the condition is false then the loop ends there.
    5) After the code is executed the last part of the for loop line is executed. That is "++i". And a new iteration starts from 2).

    Basically in every iteration i is increment by one. So you start from 0 and go until 4. When i == 5 the condition i < 5 is false and the for-loop ends.
    If you do this:
    Code:
    for (int i = 0; i < 10; i = i + 3)
    {
       std::cout << "Hey, dude" << std::endl;
    }
    How many heydudes will you get?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So change your condition to something else. (You'll want to make sure that something happens in the loop so that your loop will eventually stop -- otherwise you'll get an infinite loop.)

    Normally if you want a counted loop, you use for.

  8. #8
    C++ noob Ryan0773's Avatar
    Join Date
    Jan 2009
    Posts
    43

    Talking Thanks alot!

    Thanks C_ntua, "Hey dude" is written for times with the i guess quiz code you gave me. one question though, is ++i the same as i++?



    To tabstop, will counted loops only work for for or can you use other things to make a loop like that?

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    903
    ++i is pre-increment (i.e. increment and then use i) while i++ is post-increment(i.e. use i and then increment).

    You can use other types of loops. Your best bet would be to find a tutorial with examples of each type of loop.

  10. #10
    C++ noob Ryan0773's Avatar
    Join Date
    Jan 2009
    Posts
    43

    Talking Difference???

    What's the difference between pre and post-increment? I tried it out on the loop and it still worked the same way. Does it have a greater significance when writing a more complicated program?

    I'll keep doing the tutorials, now i have a better understanding of what the syntaxes im using meen. I am surprised at how fast the replies were.


    Thanks alot for all your help!!

  11. #11
    Registered User
    Join Date
    May 2006
    Posts
    903
    Not necessarily (hugh, sp ?) in more complex programs. In this case it does not matter but if you had used while() it would have mattered.

    Code:
    int i = 0;
    while(++i < 5)
    {
        std::cout << i; // should output 1234
    }
    
    i = 0;
    while(i++ < 5)
    {
        std::cout << i; // should output 01234
    }
    I hope I didn't make any mistake, I haven't tested this code.

  12. #12
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Correct
    In a for loop i++ and ++i are exactly the same. But here is an example to understand them better:
    Code:
    i = 5;
    a = i++;
    b = i;
    VS
    Code:
    i = 5;
    a = ++i;
    b = i;
    The equivalent codes are:
    Code:
    i = 5;
    a = i;
    i = i + 1;
    b = i;
    VS
    Code:
    i = 5;
    i = i + 1;
    a = ++i;
    b = i;
    So in both cases b = 6. But in the first case a = 5 and in the second a = 6.

    You can also use while for a loop. That is simpler. You just loop until one condition is made:
    Code:
    while(flag == true)
    {
       ...
    }
    Will loop until flag is not true. Thus false. How flag will become false depends. The equivalent for loop we posted below can be written with a while loop like this:
    Code:
    int i = 0;
    while(i < 5)
    {
       std::cout << "Hey, dude" << std::endl;
       ++i;
    }
    A for loop is kind of more "powerful" than a while loop, but both can do exactly the same things.

    Here is another common example with a while loop. This will print all the elements inside an array of size arrSize:
    Code:
    int i = 0;
    while (i < arrSize)
        std::cout << array[i++];

  13. #13
    C++ noob Ryan0773's Avatar
    Join Date
    Jan 2009
    Posts
    43

    Ok

    Alright I think i get it. When you use while() the i++ will add the extra integer, 0, wheras when using the ++i with while() will start at 1 and stop when the statement becomes false.

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Desolation View Post
    Not necessarily (hugh, sp ?) in more complex programs. In this case it does not matter but if you had used while() it would have mattered.

    Code:
    int i = 0;
    while(++i < 5)
    {
        std::cout << i; // should output 1234
    }
    
    i = 0;
    while(i++ < 5)
    {
        std::cout << i; // should output 01234
    }
    I hope I didn't make any mistake, I haven't tested this code.
    Pre-post increment doesn't wait that long, only to the end of the statement. More like this:
    Code:
    int i = 5;
    int j = i++; // now j is 5 and i is 6
    int k = ++i; // now k is 7 and i is 7

  15. #15
    C++ noob Ryan0773's Avatar
    Join Date
    Jan 2009
    Posts
    43
    Ok, so the differnece between ++i and i++ is that when using something like

    Code:
    int i = 2
    int j = ++i
    This should make it so that j and i are the same, they should both be 3. but when using i++ like this

    Code:
    int i = 3
    int k = i++
    This will make k the equivalent of i, 3, but then i has one added to it so i would become 4. is that basically it?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Several Questions, main one is about protected memory
    By Tron 9000 in forum C Programming
    Replies: 3
    Last Post: 06-02-2005, 07:42 AM
  2. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 09:44 AM
  3. Questions.
    By anonytmouse in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-19-2004, 02:16 PM
  4. C++ test questions
    By Mister C in forum C++ Programming
    Replies: 9
    Last Post: 09-08-2002, 12:05 PM

Tags for this Thread