gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > Name Entry[Basics]

#147629 - mog123 - Mon Dec 24, 2007 10:49 pm

I learned C a bit and want to make a very simple function, the thing is I don't know how to loop it back(just like in assembly you could just use something like JMP START)

So my main idea is this:

NAME ENTRY

if name is less than or even 15 characters printf: name accepted
if name is over 15 characters printf: name too long, print again
I know how to do it till that part, but writing functions is somewhat complicated for me and after assembly i just can't understand it, how do I go back to NAME ENTRY?

#147631 - tepples - Mon Dec 24, 2007 11:39 pm

To make a JMP START, wrap the program in a while loop.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#147633 - mog123 - Mon Dec 24, 2007 11:55 pm

so like:

main()
..
..
..
if..
else while(main)

?

#147636 - tepples - Tue Dec 25, 2007 12:23 am

Code:

int main(void) {
  while (1) {
    /* do stuff */
  }
}

_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#147638 - mog123 - Tue Dec 25, 2007 12:54 am

I can't seem to do it:

here's my program:
Code:
#include <stdio.h>
#include <string.h>

int main(void)
{
while(1)
   {
   printf("Please type in your name\n");
   char name[]=scanf();
   char len[10];
   if(strcmp(len,name)>0 || strcmp(len,name)=0)
      {
      printf("name accepted\n");
      return 0;
      }
   else
      {
      printf("name too long\n");
      return 1;
      }
   }
}


I get an error from the compiler saying:
name.c:9: '{' expected

Where should i put that damn bracket?

#147640 - Dwedit - Tue Dec 25, 2007 1:17 am

You can not assign directly to a char array.
The compiler thinks you want to define the array like
char foo [] = {1,4,2,6,7,4,7,2,4,5,7,5};
so it's asking for a {
_________________
"We are merely sprites that dance at the beck and call of our button pressing overlord."

#147641 - mog123 - Tue Dec 25, 2007 1:39 am

Do you have any solution? I can't think of anything right now(too tired).

#147642 - Dwedit - Tue Dec 25, 2007 3:12 am

Code:

#include <stdio.h>
#include <string.h>

int main(void)
{
   printf("Please type in your name\n");
   char name[12];
   int name_accepted=0;
   while (name_accepted==0)
   {
      scanf("%11s",name);
      int length=strlen(name);
      //we specify 11, so it will affect up to 12 characters
      if (length>10)
      {
         printf("name too long\n");
      }
      else
      {
         name_accepted=1;
         printf("hello %s\n",name);
      }
   }
   return 0;
}

_________________
"We are merely sprites that dance at the beck and call of our button pressing overlord."

#147650 - mog123 - Tue Dec 25, 2007 12:06 pm

Thanks, I think I finally understand :)