Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 151 152 [153] 154 155 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 881327 times)

Stargrasper

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2280 on: April 22, 2012, 09:40:25 pm »

So...what do you use instead of scanf? That's the only thing we've used in class so far.

I use std::cin>> more than anything else.
I'm getting a 'Error: name followed by '::' must be a class or namespace name' for the std.

As near as I can tell, you're using the C89 standard.  std::cin is a C++ command that doesn't exist in any C standard.  You should be able to use strcmp() for your purposes.  I got it to work like this:

Code: [Select]
#include <string.h>
#include <stdio.h>

int main(int argc, char **argv) {
        char str[100];
        fgets(str, sizeof(str), stdin);
        if(!strcmp(str, "test\n")) {
                printf("match\n");
        } else {
                printf("failure\n");
        }
}

Entering test at the prompt yields a match.  fgets() takes a pointer to the const char * you're using as a buffer (where you're storing the input), the size of the buffer so that it can check for buffer overruns for you, and then the input stream, in this case stdinfgets() will take in the newline character if you're ending the input with the ENTER key, so I accommodated that by adding a newline character to the end of the string I was comparing against.  If you're confused about any of that, let me know.
Logged

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #2281 on: April 22, 2012, 09:43:12 pm »

Oh, he was using C. My bad >.>
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

Sirus

  • Bay Watcher
  • Resident trucker/goddess/ex-president.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2282 on: April 22, 2012, 09:51:02 pm »

Wait, std::cin isn't C? No wonder it didn't look right. But then...why am I not getting error markings? O_o
Logged
Quote from: Max White
And lo! Sirus did drive his mighty party truck unto Vegas, and it was good.

Star Wars: Age of Rebellion OOC Thread

Shadow of the Demon Lord - OOC Thread - IC Thread

Stargrasper

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2283 on: April 22, 2012, 09:57:57 pm »

Wait, std::cin isn't C? No wonder it didn't look right. But then...why am I not getting error markings? O_o

Funny thing is...if you're using gcc and it starts seeing C++ stuff, it basically calls g++ for you.  Most C is technically valid C++, so it won't error.  At least that's my guess why it isn't erroring.  Or maybe it is erroring and it's just an obscure enough error that it isn't clearly apparent that that's the cause.

Anyway, is my method working for you?
Logged

Sirus

  • Bay Watcher
  • Resident trucker/goddess/ex-president.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2284 on: April 22, 2012, 10:04:36 pm »

I have no idea

I changed the string variable to a char and updated everything, so no errors were showing up in the text editor. However, when I tried to run it, I started getting errors in cmath, a file that isn't even included in my program.

ETA: Ok, I think that std::cin has something to do with it.

And I don't understand your stuff at all, sorry. I think I'm trying to tackle something above my current level.
« Last Edit: April 22, 2012, 10:06:54 pm by Sirus »
Logged
Quote from: Max White
And lo! Sirus did drive his mighty party truck unto Vegas, and it was good.

Star Wars: Age of Rebellion OOC Thread

Shadow of the Demon Lord - OOC Thread - IC Thread

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2285 on: April 22, 2012, 10:15:23 pm »

A = r*r*pi, so r = sqrt(A)/pi, not sqrt(A/pi).
Not so: if A=r*r*pi, then r*r=A/pi therefore r=sqrt(A/pi).

Oh man I should try to get more than 4 hours of sleep before attempting basic algebra. I'm an idiot, please ignore that.

I have no idea

I changed the string variable to a char and updated everything, so no errors were showing up in the text editor. However, when I tried to run it, I started getting errors in cmath, a file that isn't even included in my program.

ETA: Ok, I think that std::cin has something to do with it.

And I don't understand your stuff at all, sorry. I think I'm trying to tackle something above my current level.

cmath shouldn't be in your program at all if you're using straight C, so something must look like C++ to the compiler. Mind pastebinning your entire code and posting the link? It's easier to find the cause of strange errors like that if the entire code is available.

Sirus

  • Bay Watcher
  • Resident trucker/goddess/ex-president.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2286 on: April 22, 2012, 10:28:20 pm »

Eh, I managed to get rid of that problem by deleting std::cin and #include <iostream>. Of course, now the while loop isn't working properly and gives the same text no matter what is entered, but at least the bloody thing starts.

In any case, here ya go.
Logged
Quote from: Max White
And lo! Sirus did drive his mighty party truck unto Vegas, and it was good.

Star Wars: Age of Rebellion OOC Thread

Shadow of the Demon Lord - OOC Thread - IC Thread

RulerOfNothing

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2287 on: April 22, 2012, 10:34:05 pm »

You should have if (command == 'h') instead of if (command = 'h'). The first one tests for equality, while the second one assigns 'h' to command (and in C and its derived languages, an assignment expression evaluates to what is on the right hand side of the equals sign, so the second if statement will always execute).
Logged

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2288 on: April 22, 2012, 10:35:07 pm »

Code: [Select]
if (command = 'h')

Classic, simple mistake. == is comparison, while = is assignment.

Also ninja'd.

Stargrasper

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2289 on: April 22, 2012, 10:58:18 pm »

You shouldn't be using iostream at all.  It's a C++ library.  In C, you should be using stdio.h for all of your standard input and output operations.  Also, cmath is a C++ library.  The equivalent C library is math.h.  You still shouldn't need it for the code snippet I was looking over for you.  Again, std::cin is a C++ operation that doesn't exist in C.

Anyway, here's my working sample for you again...just this time really heavily commented.  If you still don't understand, just try to tell me precisely what's getting you and I'll give you a more appropriate explanation.

Code: [Select]
/**
 * You need stdio.h in order to use printf() and fgets()
 */
#include <stdio.h>
/**
 * You need string.h to use strcmp
 */
#include <string.h>

/**
 * When you run a program, it runs preprocessor commands (commands that start with #),
 * and then begins execution at the main().
 */
int main(int argc, char **argv) {
/**
* You really shouldn't use // for comments in C.  That's a C++ thing.
*
* This initializes an array of characters.  You have space for up to ten characters.
* Really that's nine characters, though, because you need the null terminator.
*/
char command[10];

/**
* As you're aware, this prints a line to the console.  Notice that I'm not ending
* with a newline character.  I'm relying on the next printf in the loop to handle that
* because I want to make sure it starts with a new line every time the loop runs.
*/
printf("\n\n******************\nEnter a command. Type 'help' for a list of commands.");

/**
* A do loop is assured to run at least once before ever checking the continuation
* condition.  You want to use a do loop here partially because you always want it
* to ask the user for input at least once and partly because you aren't initializing
* command to contain anything.  There is an astronomically low chance it could contain
* "quit" initially.  It just contains whatever is in memory at that location if you
* don't tell it otherwise.
*/
do {
/**
* This starts with a newline because I always want that prompt to be
* on its own line.
*/
printf("\n-> ");
/**
* You know, I didn't know this command existed until looking to solve
* your problem.  I couldn't get scanf to work, so I checked the manual
* for stdio.h and started looking for commands.  If you're running
* anything Unix based (Linux, MacOSX, etc), you can get to that entry
* by opening a terminal/console window and typing the following:
*
* man stdio.h
*
* That manual page contains information on the contents of that header
* file.  To get to the manual page for fgets(), use the following:
*
* man 3 fgets
*
* Or just use Google.  That works too.
*
* The signature for fgets() is:
* fgets(char *s, int size, FILE *stream);
*
* --s is the character array that you're storing the input in.  Make sure
* the array is large enough for the largest input you expect.
* --size is the size of the buffer.  Just hand it the size of the
* character array using sizeof();
* --stream is the input stream you're using.  If you want to take input
* directly from the terminal, just pass it stdin.
*
* stdin - Standard Input - stdin is an input stream that is always open
* and all input goes through it unless you tell it differently.
* Similarly stdout and stderr are where normal output and error output go
* by default.  You can always redirect any of these, but it usually
* isn't necessary.
*/
fgets(command, sizeof(command), stdin);
/**
* strcmp() takes two const char * variable and compares them against
* each other.  If they are the same, it returns 0, if they aren't
* it returns either a positive or negative number to denote the
* result of the comparision.
*
* conditionals (if, while, etc) can be passed a number.  C assumes that
* 0 is FALSE and anything else is TRUE.  Conveniently, strcmp() returns
* 0 if and only if the strings are a match.  Calling strcmp() with an
* ! asks if it is false, or returns 0.  If it does, display help info.
* Otherwise move on.
*/
if (!strcmp(command, "help\n")) {
printf("Here is a list of commands in the game:\n");
printf("Check your progress:\t'status'\n");
printf("Move to the next room:\t'next'\n");
printf("Rest:\t\t\t'rest'\n");
printf("Attack (battle only):\t'attack'\n\n");
printf("There may be additional, secret commands. Try to find them!\n");
}
/**
* In a do loop, the continuation condition appears after the code block.  In this case, we aren't
* using a ! because we want to keep going so long as the commands isn't quit.
*/
} while (strcmp(command, "quit\n"));

printf("END OF PROGRAM\n");

/**
* The C standard specifies that the main() should return 0 on a normal exit.  It's actually
* returning a code to the operating system that describes why the program ended execution.
* Theoretically, if the program crashes, it should return an error code specifying what
* went wrong.  Returning 0 is considered a normal exit.
*/
return 0;
}

Also, if you don't tell it differently, gcc assumes you're using the C89 standard.  If you want to use the C99 standard (or some other standard), you'll have to explicitly tell gcc that with the std parameter.  Here's an example of how you might do that:
Code: [Select]
gcc --std=c99 sirus.c

C99 gives you a number of features that aren't in C89.  Notably is the for loop.

EDIT: You know, it'd probably be helpful to point out that the strange fgets() command I'm using actually does have a meaningful name.  fgets stands for Filestream Get String.  In my example, the filestream I'm using is stdin.  Get is exactly what it says on the tin.  It's getting something from the filestream.  String means that the specific thing I'm getting from the filestream is a string.  In the context of my example, I'm getting a string from stdin.
« Last Edit: April 22, 2012, 11:10:30 pm by Stargrasper »
Logged

fergus

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2290 on: April 23, 2012, 12:47:10 am »

Would strcmp() still be required if I changed the loop and variable to use chars instead?
Is there any particular reason not to use strcmp()?
Logged
BY THE GODS! THIS QUOTE MADE MY SIG BOX HAVE A SCROLL BAR! HAPPY DAYS INDEED!
BY THE GODS! YOU HAVE TOO MANY SIGS!

Stargrasper

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2291 on: April 23, 2012, 01:10:21 am »

I think he removed it because he was confused how to use it successfully...which is a legitimate reason in itself.  There's nothing wrong with using an alternative system if it simplifies the code, assuming it isn't CPU crushingly inefficient.  What he's doing now with chars is less intuitive for the user, but significantly easier to code.  Which end of the tradeoff do you want?  Easy to code or easy to play?
Logged

Sirus

  • Bay Watcher
  • Resident trucker/goddess/ex-president.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2292 on: April 23, 2012, 01:26:27 am »

I guess I'd rather be a good game designer and make something easier to play. Just gotta learn more :P
Logged
Quote from: Max White
And lo! Sirus did drive his mighty party truck unto Vegas, and it was good.

Star Wars: Age of Rebellion OOC Thread

Shadow of the Demon Lord - OOC Thread - IC Thread

Stargrasper

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2293 on: April 23, 2012, 11:57:11 pm »

Out of curiosity, does anyone have a C/C++ IDE that runs on Linux that they like?  I dislike every C/C++ IDE I've ever used (ie, the common ones) so much that I learned to use text editors like vi and just do everything in a bash terminal.  Learning to do it that way is definitely a positive thing for my projects, but it'd still be nice to have an IDE.  As it stands, I usually code Java in Eclipse and everything else in vi.
Logged

SeaBee

  • Bay Watcher
  • Wolves are atheists
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2294 on: April 24, 2012, 09:53:44 am »

Have you tried Geany? I've always seen it as a mix of IDE and editor, so it's more of a light IDE I suppose. It's GTK2 if that matters.
Logged
Pages: 1 ... 151 152 [153] 154 155 ... 796