Posted: Sun Jul 24, 2005 12:11 am Post subject: Hello world wtf?
I have a small problem with line 1661 of my program. You see I was doing a Hello World Tutorial. I read it all, understood most of it, wrote it and when I compiled it I got 91 errors, the last one being on line 1661...I only have 10 lines of code.
I got 2 error lines with rem statements. Here is the code I have, what can you make of it? (Using Compiler - Bloodshed Dev-C++)
code:
//This is my first program using C++ Dev-Bloodshed
//Name(Taken out) July 24, 2005
#include <iostream>
using namespace std;
int main()
{
cout <<"Hello World\n";
}
Sponsor Sponsor
wtd
Posted: Sun Jul 24, 2005 12:45 am Post subject: (No subject)
Don't use Dev-C++.
[Gandalf]
Posted: Sun Jul 24, 2005 1:22 am Post subject: (No subject)
Yeah, look at the first C++ tutorial . But then let's see (after you stop using Dev-C++).
First off, you probably shouldn't put "using namespace std" because that will increase the size of your program, but it doesn't matter too much.
At first you shouldn't use \n when you don't need to. Start off using std::endl.
Then, if you don't want your program to simply pop up and exit you have to put something like std::cin.ignore() or std::cin.get() so that the user has to enter something before the program exits.
Lastly, it's good to put a return statement at the end of your main function, returning 0 (means that the program is finished).
IDE's... Well, you have to keep in mind that you are making a console application, not something else. It's easy to get confused, so stick to a text editor and a simple flat-out compiler.
wtd
Posted: Sun Jul 24, 2005 1:36 am Post subject: (No subject)
[Gandalf] wrote:
First off, you probably shouldn't put "using namespace std" because that will increase the size of your program, but it doesn't matter too much.
The right advice for the wrong reason. Any decent C++ compiler will weed out unnecessary code.
The real reason is for the sake of learning. If you have:
c++:
#include <iostream>
usingnamespace std;
int main() { cout << "Hello world" << endl;
return0;
}
Where did "cout" and "endl" come from? But if you write:
c++:
#include <iostream>
int main() {
std::cout << "Hello world" << std::endl;
return0;
}
Now you know exactly where cout and endl are coming from: the "std" namespace.
Flikerator
Posted: Sun Jul 24, 2005 1:18 pm Post subject: (No subject)
I got up this morning downloaded Dev C++ on my good computer. I got this Dev from the actual site not from Downloads.com and it works fine, but since you suggest a simple Text editor/ compiler I will get one of those. Is textpad a text editor (In tutorials section)? and does it come with a flat out compiler?
I have a phew questions about the code.
What does std stand for, and what does endl stand for, and what does cin do. EDIT - Standard Library
Is the line std::cin.ignore(); asking the user for input and not using it?
Lastly what does return 0; do, the same thing appears to happen without it, but im sure it has a purpose.
Thanks for all your help ^^ Ive found a phew "learn C++" sites and im going to read through them now.
[Gandalf]
Posted: Sun Jul 24, 2005 1:35 pm Post subject: (No subject)
Flikerator wrote:
I got up this morning downloaded Dev C++ on my good computer. I got this Dev from the actual site not from Downloads.com and it works fine, but since you suggest a simple Text editor/ compiler I will get one of those. Is textpad a text editor (In tutorials section)? and does it come with a flat out compiler?
Yes, it is a good text editor with syntax highlighting extentions for many languages.
Flikerator wrote:
What does std stand for, and what does endl stand for, and what does cin do. EDIT - Standard Library
std stands for 'standard' - yes, the standard library. It tells you pretty much where it's getting this magical function from. endl ends the line (like <br> in html) and you should use it instead of \n. It also flushes the info whenever it is called (good for when you are writing to a file). cin simply get input from the user.
Flikerator wrote:
Is the line std::cin.ignore(); asking the user for input and not using it?
Yes, it simply doesn't do anything at all with whatever is inputted.
Flikerator wrote:
Lastly what does return 0; do, the same thing appears to happen without it, but im sure it has a purpose.
"Return" is used in a function to return a value from the function to it's calling function. So, you could do return 3.1415; or return answer;. In this case, since we are returning something from the main() function, it returns '0' to the operating system, telling it that the program is finished. It's a good habit to get into.
Flikerator wrote:
Thanks for all your help ^^ Ive found a phew "learn C++" sites and im going to read through them now.
Watch out, because a lot of those sites aren't really the most accurate (since C++ has been around for a while), and can sometimes give bad information.
Hope this helps!
Flikerator
Posted: Sun Jul 24, 2005 2:07 pm Post subject: (No subject)
Yes it helps a lot thank you. I just learned how to use variables and made a short little program (Not copied) with my own rem statements. Could you or someone make sure my rem statements are accurate to what the line of code is doing?
code:
//This is my second program using C++
//June 24, 2005 "Using Variables - Vars"
int num; //Creates a variable. Same as var num : int in turing
#include <iostream> //Includes code from the file iostream
int main() //The main function of the program, every program has one
{ //The begining of the function
std::cout << "Please enter a number\n" << std::endl; //Asks the user for input
std::cin>> num; //Stores any input into the num variable
std::cin.ignore(); //Told it ignores the enter
std::cout << "\nYou entered the number " << num << std::endl; //Displays the number
std::cin.ignore(); //Lets you see the input, waits for you to hit enter
return 0; //Returns a value of 0 for function "main"
} //The end of the funtion
[Gandalf]
Posted: Sun Jul 24, 2005 2:18 pm Post subject: (No subject)
Well... First off, you don't have to always put std::cin.ignore(), just at the end of the program, where you put a statement like "enter anything to exit". Also, you don't need to use \n when you already have std::endl. Lastly, and more importantly, you shouldn't use global variables. In such a simple program that int num should be inside the main function. Variables should be declared in functions, and if another function needs them, should be passed to the other function through arguments.
Sponsor Sponsor
Flikerator
Posted: Sun Jul 24, 2005 3:42 pm Post subject: (No subject)
If I take out std::cin.ignore(); after I ask for input it counts the enter as input with the integer and exits the program because it is no longer an int value. As for the \n I used it to space out my lines so it looks a bit neater.
[Gandalf]
Posted: Sun Jul 24, 2005 4:38 pm Post subject: (No subject)
I think you misunderstand, but I see your problem. Now that you have the user inputting something, you should add an extra line (as shown in the example). You really shouldn't be using \n though for this kind of basic thing.
Here is what you program should look like:
c++:
#include <iostream>
int main() { int number;
std::cout << "Please enter a number: ";
std::cin >> number;
std::cout << "The number was: " << number << std::endl;
std::cout << "Press enter to exit.";
std::cin.ignore(1,'\n');
std::cin.ignore();
return0;
}
The first cin.ignore ignores when the enter key is inputted and is in the buffer (because you already had a cin() statement).
wtd
Posted: Sun Jul 24, 2005 4:48 pm Post subject: (No subject)
All of this confusion over "Hello world". Perhaps I was right about C++ being wrong for learning at this level...
Flikerator
Posted: Sun Jul 24, 2005 5:16 pm Post subject: (No subject)
wtd wrote:
All of this confusion over "Hello world". Perhaps I was right about C++ being wrong for learning at this level...
Im passed Hello World. Im just asking questions here so I dont make lots of mistakes when moving on to more complex things. Im on variables and implimenting them in my program. I can use all mathimatical signs using vars and straight numbers (+ - * / ext). Im only two days into programming, the first consisting of problems with my compiler and I didn't get any programming done. Thanks to [Gandalf] and my C++ sites im making good progress without spending hours on little things. I would agree that this is a little more complicated then Turing, but in no way above my level of comprehenshion or understanding. If it is above my programming ability I would have to say good because I have never learned this before and it would not be fun or exciting to learn if it were very easy.
Tonight if I can get a compiler to work on my slow computrer (good computer shared with brother) I will start to learn if statements. I briefly glanced them and it looks simple to understand, but I won't know untill I try.
[Gandalf] wrote:
I think you misunderstand, but I see your problem. Now that you have the user inputting something, you should add an extra line (as shown in the example). You really shouldn't be using \n though for this kind of basic thing.
Here is what you program should look like:
c++:
#include <iostream>
int main() { int number;
std::cout << "Please enter a number: ";
std::cin >> number;
std::cout << "The number was: " << number << std::endl;
std::cout << "Press enter to exit.";
std::cin.ignore(1,'\n');
std::cin.ignore();
return0;
}
The first cin.ignore ignores when the enter key is inputted and is in the buffer (because you already had a cin() statement).
Ok I think I get it. So I only have to but a std::cin.ignore(); once in my program, not after each time I ask the user for input (Like every std::cin >> var).
EDIT - I have another question. without using \n how would I make say, 5 spaces between 2 sentences. I know that std::endl puts the cursor on the next line, and so does \n so how would I do it without puting 5 \n's ?
wtd
Posted: Sun Jul 24, 2005 5:30 pm Post subject: (No subject)
Flikerator wrote:
Ok I think I get it. So I only have to but a std::cin.ignore(); once in my program, not after each time I ask the user for input (Like every std::cin >> var).
The real question you need to ask yourself is this: Do you know what std::cin.ignore() is doing?
Flikerator
Posted: Sun Jul 24, 2005 5:33 pm Post subject: (No subject)
wtd wrote:
Flikerator wrote:
Ok I think I get it. So I only have to but a std::cin.ignore(); once in my program, not after each time I ask the user for input (Like every std::cin >> var).
The real question you need to ask yourself is this: Do you know what std::cin.ignore() is doing?
It discards input entered by the user. I need to know if it does it just once, or throughout the entire program.
EDIT - From my tutorial site it sais - cin.ignore() is another function that reads and discards a character.
Im guessing the last character entered in all cases ENTER. Im not 100% thats why I ask.
wtd
Posted: Sun Jul 24, 2005 5:36 pm Post subject: (No subject)
Flikerator wrote:
wtd wrote:
Flikerator wrote:
Ok I think I get it. So I only have to but a std::cin.ignore(); once in my program, not after each time I ask the user for input (Like every std::cin >> var).
The real question you need to ask yourself is this: Do you know what std::cin.ignore() is doing?
It discards input entered by the user. I need to know if it does it just once, or throughout the entire program.
It does it just the once, and just for that particular input stream.