Thread: strings...

  1. #1
    Unregistered
    Guest

    Question strings...

    I'm working on a small little project to simulate the linux OS for complete newbies and I was wondering, I kept trying to find a way to use ifs and else statements for them to analyze the char variable and do something. But I can never get it to read the char variables in any of the if's statements. I've researched the net a bit, and have not been successfull. A snippet of the code is:

    printf("\n > ");
    gets(temp);
    if (temp=='ls')
    {
    printf("testing");
    }

    else { printf("Something went wrong....beta code"); }



    btw, yes i declared all vars, and i did everything needed, i'm not THAT stupid ;P. So if someone could please hook me up, i'd be very thankful.


    p.s. if your a C string god, is there anyway to add an effect to the printed characters to make then like seem to type by themselves on letter at a time? If not, thanx anway. =)

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490

    Talking

    i think (temp=='ls) should be (temp=="ls")
    or try if (strcmp(temp,"ls")) ....

    small little project to simulate the linux OS for complete newbies
    small?

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    22
    Uh... You don't say how you declared "temp" well, if you declared it like so:

    char temp;

    then temp can only hold 1 (one) character therefore the statement

    if(temp == 'ls')

    will always return false due to limited range of data type (char) such that "something will always go wrong...beta code". However if you defined temp as

    char *temp;

    Then, I think you would like to use the following statement

    if(temp == "ls") /* note the double quotes */

    If you defined temp as

    char temp[SIZE];

    Then, you should use:

    if((strcmp(temp, "ls")) == 0)

    this also works for the latter case.
    about characters typing themselves one letter at a time. You probably should use an array e.g

    char temp[10];
    int i;

    for(i = 0;i <= 10;i++) {
    putc(temp[i], stdout);
    /*delay for a considerable amount of time e.g*/
    usleep(100); /* delays for 100 microseconds */
    }

    or if you use

    char *temp;

    try:

    while(*temp != '\0') {
    putc(*temp, stdout);
    temp++;
    /*delay for a considerable amount of time e.g*/
    usleep(100); /* delays for 100 microseconds */
    }



    etc....

    have fun,
    -------------------------
    Gerald.

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    6
    Is usleep ANSI?

    I did a quick man and the documentation is written by Redhat people which suggests to me that it may be specific to Linux or even just Redhat.

    I guess it wouldn't matter if you were running it on Linux but I would have thought that one wouldn't write a Linux simulator to run on Linux.

    Alternately you could write a loop that does nothing but look at the time between key outputs.

    eg.
    Code:
    for(i=0;i<(sizeof(string[])/sizeof(char));i++)
    {
      putchar((int)string[i]);
      starttime=time();
      /* loop for some time */
      while(wasted!=WAITTIME)
      {
        wasted=difftime(starttime,time());
      }
    }

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Code:
    char temp[100];
    gets(temp); 
    if ( strcmp(temp,"ls") == 0 )
    { 
       printf("testing"); 
    }
    Later on, you will need to use fgets instead of gets - because gets is a big bug trap

  6. #6
    The Artful Lurker Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    Originally posted by limerick
    Is usleep ANSI?
    usleep() is not ANSI (and was not written by Red Hat). It conforms to BSD 4.3.
    Jason Deckard

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM