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.
Cant program anything
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");
}
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");
}
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.
scanf("%s",&var) is a sure road to a buffer overflow exploit. Such code should never be written, especially in an example for beginners.
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>>?
#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>>?
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
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
Now you have 5 security holes ;)
in C++ your program would look like this:
The C++ version has 0 security holes and is just as easy.
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.
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 ,-)
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.
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.
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.
Quote:Original post by anistQuote: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?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement