From: Ben Bacarisse on
Timmy <Timmy15(a)Spamnot.com> writes:

> I'm trying to write a little random password generator,
<snip>
> That is the error that g++ is spiting out..
>
> 1.cpp: In function 'int main()':
> 1.cpp:32: error: expected `;' before 'cout'
>
> Ain't any such error, I checked and double checked.

No?

> cout << passwrd;
> cout<< "\n "
^ wot, no ';' ?
> cout<<"press any key to end \n";

Trust your tools. They will be right way more often than you will be
(and by "you" I mean "you the programmer" in which I include myself).

--
Ben.
From: Francis Glassborow on
Daniel T. wrote:
> In article <20080105184652.694e040e(a)suddenlink.net>,
> Timmy <Timmy15(a)Spamnot.com> wrote:
>
>> int main ()
>> {
>>
>> char
>> alpha[80]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
>
> If you decide to add/remove characters to the above list, you will have
> to make more than one edit to the program. Can you think of a way to
> reduce the number of edits necessary to just one?
>
>> char passwrd[80]="";

well my choice would be to use an std::string in both cases.

string
alpha("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")

and

string password;

BTW why on earth spell password as "passwrd" ?
From: Philip Potter on
Daniel T. wrote:
> In article <20080105184652.694e040e(a)suddenlink.net>,
> Timmy <Timmy15(a)Spamnot.com> wrote:
>> I'm trying to write a little random password generator, g++ refuses to
>> compile it and spits out these error
>>
>> 1.cpp: In function 'int main()':
>> 1.cpp:32: error: expected `;' before 'cout'
> ^^ ^
>
> Start at line 32 of your program, then start scanning up the lines, one
> at a time, until you see where a ';' is missing. Step 2: Hit yourself on
> the head with the palm of your hand and say "Doh!" :-)
>
> I've been programming in C++ for going on 15 years, I still forget
> semi-colons often. Your next big milestone is when you forget a closing
> brace. Sometimes the compiler won't catch it until it's 10-15 lines past
> where the error is...

Although I miss lots of semicolons, I can't recall ever having missed a
curly bracket. Mainly because when I enter a new block, I enter the two
curlies together:

for(;;) { }

before inserting newlines:

for(;;) {

}

and then continuing with code entry:

for(;;) {
func1();
func2();
}

By only inserting curlies in pairs, I maintain the curly-bracket
matching invariant.

>> code:
>>
>> #include <stdio.h>
>> #include <iostream.h>
>> #include <string.h>
>
> The above are deprecated. Prefer their 'c' counterparts:
> #include <cstdio>
> #include <cstring>
> #include <iostream> // I don't think iostream.h is standard

You are correct, iostream.h isn't standard.

>> #include <cstdlib>
>>
>>
>> using namespace std;

I dislike 'using namespace std'; but that's a well-documented side-issue:
http://www.parashift.com/c++-faq-lite/coding-standards.html#faq-27.5

Phil
From: Daniel T. on
Timmy <Timmy15(a)Spamnot.com> wrote:

> The only compiler I've used is GCC because it's already installed
> on the type of operating systems I use.. If I make the slightest
> error like leaving out a \n"; or; the compiler stops the processes
> immediately.

That is correct, standard conforming, behavior.

> This leads me to a question, buffer overflows be they stack or heap
> are caused by programing errors overflowing in memory, this causes
> a great security risk to computers, how do these program errors
> happen?

The program you presented is an excellent example. If a user enters any
value 80 or greater as input, your program will have a buffer overflow.
This is not something that the compiler can possibly guard against, it
won't even be running at the time the overflow occures.

> are their some compilers that will continue compiling when code
> errors are found?

Some do, some don't. They are required to issue a diagnostic, but for
some errors, some compilers will go ahead and at least finish compiling
the cpp file they are currently working on.
From: Mike Wahler on

"Timmy" <Timmy15(a)Spamnot.com> wrote in message
news:20080106180649.3558d0fe(a)suddenlink.net...
> On Sun, 06 Jan 2008 17:48:42 -0500
> "Daniel T." <daniel_t(a)earthlink.net> wrote:
>
>
>> The program you presented is an excellent example. If a user enters
>> any value 80 or greater as input, your program will have a buffer
>> overflow. This is not something that the compiler can possibly guard
>> against, it won't even be running at the time the overflow occures.
>
> OMG!! I didn't know that, and I don't know why that would occur. I'm
> trying to figure this stuff out on my own. I've got some books, some
> are older. My mom just got me some books from Amazon but I have
> received them yet..
>
>
>> > are their some compilers that will continue compiling when code
>> > errors are found?
>>
>> Some do, some don't. They are required to issue a diagnostic, but for
>> some errors, some compilers will go ahead and at least finish
>> compiling the cpp file they are currently working on.
>
> I was just playing with C++ variables. Here is a program I just made.
>
> How can I tell if its overflowing? It complies with no errors.

Overflow cannot occur until the program is actually running.
During compilation, the program is not running. So of course
a compiler cannot determine if overflow will happen or not.
It's the programmer's responsibility to prevent overflow (e.g.
with input validity checks).

>
> =============
>
> #include <iostream>
> using namespace std;
>
> int main()
>
>
> {
>
>
> std::cout << "This is a C++ variables program\n";
>
>
> int a, b, c;
> int result;
>
> a = 1;
> b = 2;
> c = 3;
>
> c = c + b;
> result = c - a;
> cout << result;
> return 0;
> }

Since all the operations above use 'hard coded' values,
and the results fall within the language's guaranteed
ranges, no overflow will occur. If some or all of those
values come from external input, then those values must
be verified to still produce 'in-range' results.
The programmer must write code that does thorough checking
of input, and reject those values which could cause overflow
or other unacceptable conditions. This is the case in
any language.

-Mike


 |  Next  |  Last
Pages: 1 2
Prev: Colored bitmaps in Visual C++
Next: delete pointer