Re: Please Help- my first program does something weird !
Originally Posted by Jeff++
ok thank... but the g++ part went right over my head.
where does the g++ come from ? and the switch names ?
thanks.
When you installed Dev-C++, it also installed the g++ (mingw) compiler. Look at the directories where it was installed. Also, those switches are probably set in the project options, but I don't have Dev-C++ in front of me to tell you exactly where to set them.
You need to do this, since otherwise you will inadvertantly create invalid C++ programs (so many posters using g++ make this same error of not setting the appropriate compiler switches beforehand).
Re: Please Help- my first program does something weird !
so Paul... I hear what you're saying, sort of. In other words without adding the switch name....
I'm using a different compiler than I should be ?
the switch name is instructions to use compiler g++ instead ?
if this is so, why does it not mention it anywhere in the included instructions or tutorials ?
Re: Please Help- my first program does something weird !
I'm seeing so many differences in what you folks are saying and my tutorial I've been reading...
like...
the tutorial has taught me the use of printf and scanf
but everyone seems to be using cout and cin,
yet this tutorial makes no mention of cout and cin.
can someone tell me the difference between them PLEASE.....
which one should I use ? where ? why ?
or better yet
maybe direct me to a better tutorial for total newbies.
so I can start over.
Re: Please Help- my first program does something weird !
Originally Posted by Jeff++
I'm using a different compiler than I should be ?
the switch name is instructions to use compiler g++ instead ?
You don't need to use g++ directly -- the DevC++ environment does this for you.
If you go to the Project and Project Settings, there should be an option to set the compiler parameters. Those parameters are the ones I mentioned:
-pedantic -Wall
You can test if you've set the switches correctly by compiling the following:
Code:
int main()
{
int num = 10;
int array[num];
}
If this code doesn't compile and gives you an error where the array is declared, you have set the switches correctly. If it does compile successfully, then you still have the extensions turned on and you need to try something else to turn them off.
if this is so, why does it not mention it anywhere in the included instructions or tutorials ?
DevC++ only has instructions on how to use DevC++, not the g++ compiler it runs underneath the hood. For that, you need to go to the Gnu website or mingw website.
Re: Please Help- my first program does something weird !
Originally Posted by Jeff++
well thanks Paul, but when I click " Project Options " I get an error. Obviously a bug, it does it every time.
I get this
Application version: 4.9.9.2
I would suggest getting the CodeBlocks IDE for g++. It is currently maintained, while Dev C++ is basically a dead project.
1. what is the difference between " cin, cout " and " printf, scanf " ? should I use one over the other ?...
3. can someone direct me to a more up to date tutorial for total newbies ?
one that explains cout and cin better.
A good C++ book describes the difference between cin and cout. If you don't have a good C++ book, you need to get one since C++ isn't something you can learn "on the fly" or winging it. It just doesn't work that way. As to websites, many, if not most are notorious for giving wrong, outdated, or bad information concerning C++ programming. Unless the website has been recommended by C++ industry professionals, don't learn from them.
On the other hand, books are peer-reviewed, so any wrong information is usually corrected before publication (not all C++ books, but most) -- the same cannot be said about C++ websites.
2. What's the dif between C++ and Visual C++ ?
C++ is a computer language invented by Bjarne Stroustrup and officially specified by ANSI/ISO in the year 1998.
Visual C++ is a brand of compiler created by Microsoft that compiles C++ code.
Re: Please Help- my first program does something weird !
hey thanks again paul.
I think you misunderstood me about the cin , cout thing.
I understand there use fine...
what I meant was, why use printf vs. cout ? is one better in certain situations ?
PS- I just use Dev C++ because I wanted to learn and I found it for free on the net.
Re: Please Help- my first program does something weird !
Originally Posted by Jeff++
ok,
I see, you left off the other part. If I combine that with what I had at the end I get
Code:
#include <stdio.h>
#include <conio.h>
int main()
{
int r=0;
do
{
int c=0;
while(c <=9){
printf("Print 10 !\n\n");
++c;
}
printf("want to re-print ? then you need to find the lucky number !\n\n if you get it wrong you will have to quit !");
scanf("%d",&r);
}
while(r==4);
{ printf("click any key to quit");
}
getche();
}
which seems to work just fine.
is this considered good syntax now ?
cheers.
It's ok, but you don't need to restrict the scope of your "click any key to quit" statement. i.e.
Code:
{
printf("click any key to quit");
}
can become simply
Code:
printf("click any key to quit");
With regards to printf/scanf and cout/cin, it depends on the language in which you are writing. Your program is a C program, it includes C headers (e.g. #include<stdio.h>) and uses C IO functions (printf, scanf). A C++ program includes C++ headers (e.g. #include<iostream>) and consequently will use C++ IO objects (cout, cin).
Although you can use the C printf and scanf functions in C++, they are not typesafe and this is a problem when writing C++ programs where you may want to stream non-POD (Plain Old Data) types such as classes.
The program you have written is clearly a C program. An equivalent C++ program would be
Code:
#include <iostream>
int main()
{
int r=0;
do
{
int c=0;
while(c <=9){
std::cout << "Print 10 !\n\n";
++c;
}
std::cout << "want to re-print ? then you need to find the lucky number !\n\n if you get it wrong you will have to quit !" << std::endl;
std::cin >> r;
std::cin.clear();
}
while(r==4);
system("PAUSE");
}
Bookmarks