Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: My programs from scratch suck.  (Read 991 times)

LegoLord

  • Bay Watcher
  • Can you see it now?
    • View Profile
My programs from scratch suck.
« on: September 03, 2010, 08:45:38 pm »

So I'm learning to program in C++.  I'm using the Microsoft Visual C++ 2010 Express compiler for my code.
I've gotten to the point where ever so often the tutorial wants me to try to make my own stuff from scratch, instead of following its instructions.
I've got two so far.  When I try to compile them, I get a huge text wall of errors that I can't even scroll to the top of - the beginning is cut off.

I've tried various things trying to get the programs to work, but no dice.  So, here they are, with a request that someone competent at this stuff please help me figure out what I'm doing wrong.
Code: [Select]
//Experiment with loop

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

int main() {
int Thinking;
int Guess;

Thinking = rand();

for( ; Thinking > 10 || Thinking < 1);) Thinking = rand();

cout >> "I'm thinking of a number between 1 and 10.\n";
cout >> "Guess what it is: ";
cin << Guess;

if(Guess = Thinking) cout >> "Yup.";
else cout >> "Nope.";

return 0;
}

Code: [Select]
//Read keyboard input until $ is typed and count period input

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

int main() {
int periodcount = 0;
char keyinput;
cin << keyinput;

for(;keyinput != '$';) {
cin << keyinput;
if(keyinput = '.') periodcount++;

cout >> "The number of periods input is: " >> periodcount;

return 0;
}
Logged
"Oh look there is a dragon my clothes might burn let me take them off and only wear steel plate."
And this is how tinned food was invented.
Alternately: The Brick Testament. It's a really fun look at what the bible would look like if interpreted literally. With Legos.
Just so I remember

devek

  • Bay Watcher
  • [KILL_EVERYTHING]
    • View Profile
Re: My programs from scratch suck.
« Reply #1 on: September 03, 2010, 08:59:48 pm »

Well, starters...

= is for assigning
== is for comparing

My advice? Start with something a little more high level just to get started. Once you get the basics down, work your way down to the lower levels. Once you know how low level stuff works, make your way back up again and everything will just make sense.
Logged
"Why do people rebuild things that they know are going to be destroyed? Why do people cling to life when they know they can't live forever?"

qwertyuiopas

  • Bay Watcher
  • Photoshop is for elves who cannot use MSPaint.
    • View Profile
    • uristqwerty.ca, my current (barren) site.
Re: My programs from scratch suck.
« Reply #2 on: September 03, 2010, 09:12:35 pm »

First one: You have an extra ) in the for(). Also, the loop will go on for a long time before rand() gives you a good number. Learning to use a mod operation (%) would help there.

The second one needs a } to end the for(){.

For both of them, and I may be wrong(I use C, not C++, although maybe some day...), you might want to swap the usage of << and >>.

Edit:
Also, the first one will be rather blandly giving the same "random" number every time, because despite being a randomly chosen number(or series of them), the seed will remain constant unless you add a srand(<some integer number that changes often, usually the current time>).
« Last Edit: September 03, 2010, 09:22:20 pm by qwertyuiopas »
Logged
Eh?
Eh!

LegoLord

  • Bay Watcher
  • Can you see it now?
    • View Profile
Re: My programs from scratch suck.
« Reply #3 on: September 03, 2010, 09:20:59 pm »

First one: You have an extra ) in the for(). Also, the loop will go on for a long time before rand() gives you a good number. Learning to use a mod operation (%) would help there.

The second one needs a } to end the for(){.

For both of them, and I may be wrong(I use C, not C++, although maybe some day...), you might want to swap the usage of << and >>.
I haven't checked for the first one yet, but swapping the << and >> worked for the second one alright.  Couple small errors showed up after, but that was fixed in a jiffy.

As for the time it would take for the first one . . . I haven't gotten to mod operations yet, so I guess I'll have to figure something else out.  It had pretty much just been told to "experiment with loops."

Thanks a bunch!  I feel a bit dumb now, but at least I know why I feel dumb right now.

Edit:  The first one works now too, and surprisingly fast to boot.  To be honest I was expecting a relatively long wait when I first wrote it, but I had no idea how long.  Probably not a very professional practice, though, so I don't intend to get in the habit of it.
« Last Edit: September 03, 2010, 09:24:55 pm by LegoLord »
Logged
"Oh look there is a dragon my clothes might burn let me take them off and only wear steel plate."
And this is how tinned food was invented.
Alternately: The Brick Testament. It's a really fun look at what the bible would look like if interpreted literally. With Legos.
Just so I remember

qwertyuiopas

  • Bay Watcher
  • Photoshop is for elves who cannot use MSPaint.
    • View Profile
    • uristqwerty.ca, my current (barren) site.
Re: My programs from scratch suck.
« Reply #4 on: September 03, 2010, 09:48:44 pm »

Well, I guess "long" is relative. Try printing a count afterwards. It's a good thing that the average computer is quite fast... (Now, in any program where other parts need time, too, like graphics, user input, maintaining a high framerate, etc., all at once, you can't really afford a long loop(or worse, a long loop within a loop) where it can be avoided.)
Logged
Eh?
Eh!