![]() |
| | #1 |
| C++ noob Join Date: Jan 2009
Posts: 43
| Lot of questions. -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();
}
-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. |
| Ryan0773 is offline | |
| | #2 |
| Registered User Join Date: May 2006
Posts: 894
| 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;
}
|
| Desolation is offline | |
| | #3 |
| C++ noob Join Date: Jan 2009
Posts: 43
| 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!!! |
| Ryan0773 is offline | |
| | #4 |
| Registered User Join Date: May 2006
Posts: 894
| I'm afraid I don't understand what your question is. What do you mean by "make a loop of a sentence" ? |
| Desolation is offline | |
| | #5 |
| C++ noob 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 ();
}
Sorry if this doesn't clear it up. |
| Ryan0773 is offline | |
| | #6 |
| Registered User Join Date: Jun 2008
Posts: 1,134
| If I understand correctly you want something like this: Code: for (int i = 0; i < 5; ++i)
{
std::cout << "Hey, dude" << std::endl;
}
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;
}
|
| C_ntua is offline | |
| | #7 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| 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. |
| tabstop is offline | |
| | #8 |
| C++ noob Join Date: Jan 2009
Posts: 43
| 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? |
| Ryan0773 is offline | |
| | #9 |
| Registered User Join Date: May 2006
Posts: 894
| ++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. |
| Desolation is offline | |
| | #10 |
| C++ noob Join Date: Jan 2009
Posts: 43
| 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!! |
| Ryan0773 is offline | |
| | #11 |
| Registered User Join Date: May 2006
Posts: 894
| 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
}
|
| Desolation is offline | |
| | #12 |
| Registered User Join Date: Jun 2008
Posts: 1,134
| 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; Code: i = 5; a = ++i; b = i; Code: i = 5; a = i; i = i + 1; b = i; Code: i = 5; i = i + 1; a = ++i; b = i; You can also use while for a loop. That is simpler. You just loop until one condition is made: Code: while(flag == true)
{
...
}
Code: int i = 0;
while(i < 5)
{
std::cout << "Hey, dude" << std::endl;
++i;
}
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++];
|
| C_ntua is offline | |
| | #13 |
| C++ noob 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. |
| Ryan0773 is offline | |
| | #14 | |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| Quote:
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 | |
| tabstop is offline | |
| | #15 |
| C++ noob 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 Code: int i = 3 int k = i++ |
| Ryan0773 is offline | |
![]() |
| Tags |
| basic, basics, c++, lots, noob |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Several Questions, main one is about protected memory | Tron 9000 | C Programming | 3 | 06-02-2005 07:42 AM |
| Trivial questions - what to do? | Aerie | A Brief History of Cprogramming.com | 23 | 12-26-2004 09:44 AM |
| Questions. | anonytmouse | A Brief History of Cprogramming.com | 4 | 05-19-2004 02:16 PM |
| C++ test questions | Mister C | C++ Programming | 9 | 09-08-2002 12:05 PM |