Sign in to follow this  
Titan2400

Cant program anything

Recommended Posts

Titan2400    122
Hi guys im new to programming and my friend sent me a compiler called bloodshed-dev-c++ basically for c ++ i guess but when i try to put in words such as main void() it says its wrong and then change it to int(). The main i think is for c though then when i get the program to compile it opens it real fast then closes the window i even tried adding a enter (so the comp waits for me to press enter) like %s but it didnt work and still turned off and few erros also occured. here this is what i wrote for a simple program can you tell me if its right or wrong with a c ++ (this doesnt have a escape variable and jus closes) #include <stdio.h> int main() { printf("GoodBye, Cruel World!\n"); } ~thank you for reading~

Share this post


Link to post
Share on other sites
Ainokea    435
Your cod eis right the only problem is that it opens the console window only long enough to do all its porcesses.
To fix this add system("PAUSE"); after the printf statment.

Share this post


Link to post
Share on other sites
Ainokea    435
One other thing if you are programming in c++ and not c then you should do this:

#include <cstdio>

instead of this;

#include <stdio>

another thing is that the c++ version of printf (not that it matters what you use) is:
cout<<"text goes here";

Share this post


Link to post
Share on other sites
Titan2400    122
what does the include window.h thing do? and can i program c stuff into a c++ compiler or do i need a c compilier ? and whats the diffrence from the stdio thing? Becuase i got that upper program from the c for dummies book from teh begining and it didnt work and didnt ask for a pause series i was wondering why it is so diffrent :(

Share this post


Link to post
Share on other sites
Titan2400    122
so if c++ is a backward thing to c programs then that means c variables and c keys will work in the c++ compiliers cuz in my eyes it seems that it doesnt :/ and should i put the insert windows.h now for now on ? also what if i suppose i wanted to add a scanf sequence whhat should i do>>?

Share this post


Link to post
Share on other sites
Titan2400    122
i got another problem my friend jus gave me a c compilier and stuff but it says this error can you tell me what it means

C:\Documents and Settings\Administrator\Desktop\C
Projects\WHORU.c: line 7: Parse Error, expecting `SEP'
'char me[20] printf("What is your name?")'
aborting compile

from this source

#include <stdio.h>

void main()
{
char me[20;

printf("What is your name?");
scanf("%s",&me);
printf("Darn glad to meet you, %s!\n",me);
}

also when i answer the first question it closes the window once agaain. :(
and it doesnt show the dark glad to meet you part

Share this post


Link to post
Share on other sites
Ainokea    435
C++ is backwards compatible with c however you will need to include header files the c++ way. So if you are reading a c book then simply replace all their
#include <headerfile.h>

with

#include <cheaderfile>

However if you want to do it the c way then when you make a new project in dev C++ make it a c project and not a c++ project.

Share this post


Link to post
Share on other sites
Ainokea    435
oh you code should be:


void main()
{

/* you forgot the right bracket */
char me[20];

printf("What is your name?");
scanf("%s",&me);
printf("Darn glad to meet you, %s!\n",me);

//and pause so the window doesnt flash
system("PAUSE");
}

Share this post


Link to post
Share on other sites
Trap    684
Your program has a security hole...

scanf("%s",&var) is a sure road to a buffer overflow exploit. Such code should never be written, especially in an example for beginners.

Share this post


Link to post
Share on other sites
Titan2400    122
i have made this program in a c compilier :) and it works fine now thank you all for helping me wit these problmes hehe this is my final begining project:
#include <stdio.h>

void main()
{
char adjective[20];
char food[20];
char chore[20];
char furniture[20];
char hi[1];

printf("enter an adjective:");
scanf("%s",adjective);
printf("enter a food:");
scanf("%s",food);
printf("Enter a house chore(past tense):");
scanf("%s",chore);
printf("enter an item of furniture:");
scanf("%s",furniture);

printf("\n\nDon't touch that %s %s!\n",adjective,food);
printf("I just %s the %s!\n",chore,furniture);
printf("type in 1 to exit");
scanf("%s",hi);
printf("%s",hi);
}
this is for c compiliers though so dont use in c++ unless you can transdfer :)

one problem if i have any spaces in the answers to the quetsions it closes it out any way to fix this>>?

Share this post


Link to post
Share on other sites
demonkoryu    980
Titan2400: With that char hi[1], you don't have enough room for even one character because scanf("%s", hi) will get you a string, automatically adding a zero '\0' at the end, so it is already 2 bytes. Will probably work though, but can crash.
You could scanf("%c", hi), this will read only a char.

But my advice is, drop C altogether and learn C++ from the start, it is about as hard to begin with as C. Most C libraries aren't needed today, anyway, so don't waste your time with scanf and the like.

Thermo

Share this post


Link to post
Share on other sites
Trap    684
Now you have 5 security holes ;)

in C++ your program would look like this:
#include<iostream>
#include<string>
using namespace std;

int main()
{
string adjective;
string food;
string chore;
string furniture;
char hi;

cout << "enter an adjective:";
cin >> adjective;
cout << "enter a food:";
cin >> food;
cout << "Enter a house chore(past tense):";
cin >> chore;
cout << "enter an item of furniture:";
cin >> furniture;

cout<<"\n\nDon't touch that "<<adjective<<" "<<food<<"!\n";
cout<<"I just "<<chore<<" the "<<furniture<<"!\n";
cout<<"type in 1 to exit";
cin >> hi;
cout<<hi;

return 0;
}

The C++ version has 0 security holes and is just as easy.

Share this post


Link to post
Share on other sites
Trienco    2555
Quote:
Original post by Trap
The C++ version has 0 security holes and is just as easy.


just never show him how hideously the code will look like to get a halfway decent formatted output with cout ,-) yet, scanf for strings does seem about as close to a crime as it gets, considering there are functions like fgets to make it safe ,-)

Share this post


Link to post
Share on other sites
anist    100
Quote:
Original post by Trap
Now you have 5 security holes ;)


no offense, but why are we worried about overflow security flaws in a Windows based console apps done by a beginner? does this in anyway affect his learning of concepts? if this app was accessible by the most 133t, wouldn't they be on his system and helping him instead of using it to retrieve a procedures address?

sorry, but i deal with a lot of "noob paranioa", and i think it's time they learn what is legitmately bad practices as opposed to something they will learn not to do when it is important.

Share this post


Link to post
Share on other sites
Seriously, these guys all know what they're are talking about, but your are just learning the basics now. None of this is really relevant to you.

I agree, these days C++ really is a better choice than C. However, when you are just starting out, none of the C++ features are useful anyways. Just learn the basics.

Try looking for books by Greg Perry. He's usually a beginner-level writer.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Quote:
Original post by anist
Quote:
Original post by Trap
Now you have 5 security holes ;)


no offense, but why are we worried about overflow security flaws in a Windows based console apps done by a beginner? does this in anyway affect his learning of concepts? if this app was accessible by the most 133t, wouldn't they be on his system and helping him instead of using it to retrieve a procedures address?

sorry, but i deal with a lot of "noob paranioa", and i think it's time they learn what is legitmately bad practices as opposed to something they will learn not to do when it is important.


So you're saying you should learn how to do something the wrong way and then correct it?

Share this post


Link to post
Share on other sites
demonkoryu    980
Since he has to stick with his C book, yes.
By the time his applications will be installed on computers around the world, he will probably have learned about security holes, but one advice about scanf security issues is probably not going to help his learning process, but instead confuse him.

Thermo

Share this post


Link to post
Share on other sites
Titan2400    122
hehe guys guys you so know what your doing but right now really dont care bout sevurity holes im jus trying to get the hang of programming so i can then go on i cna jus read all the books im a fast reader anyways. (also confusing me lol im still a noob at this >.<)


o ya and if i was to learn c++ what kind of books should i buy or which ones is easily understood maybe c++ for dummies is that a good book ?

Share this post


Link to post
Share on other sites
Can't really say what is available anymore, all my books are over fifteen years old. And I don't have the need for any new ones.

Some authors and editor I can recommend looking for:
Pappas & Murray
Greg Perry
Robert Lafore
Andre' LaMothe (he's mostly about game programming ;)

There are a few others, but I really don't remember off hand.

And don't forget the reference section here at GameDev :)

Share this post


Link to post
Share on other sites
MaulingMonkey    1728
Quote:
Original post by anist
Quote:
Original post by Trap
Now you have 5 security holes ;)

no offense, but why are we worried about overflow security flaws in a Windows based console apps done by a beginner?

Quote:
Original post by lack o comments
Seriously, these guys all know what they're are talking about, but your are just learning the basics now. None of this is really relevant to you.

And people wonder why computers are so insecure, with people actively advocating for the teaching of insecure programming practices from day 1.
*repeatedly bashes head against the nearest wall, wondering why the world is such a twisted place*.

Next, he'll put char name[20], and the program will crash when I attempt to enter my name (including spaces, my full name is 26 characters long - so name[27] would be required to ensure it didn't crash - or worse, behave in an undefined manner - assuming I didn't accidentally hit an extra key).

*continues bashing head*

I am now going to download the lastest firmware for my router, turn of my computer (as leaving it on is probably quite dangerous in this age), and go have a stiff rootbeer (bite me, it tastes better than that crappy "real beer").

</hateandpointingouttheironyifthisclassifysasironic>

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this