Thread: Something weird about Strings

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    19

    Something weird about Strings

    I have just started a code that will hopefully end up being able to differentiate any inputted function.
    It is relatively simple function, so at the moment it can only do powers. so
    Code:
    x^3+x^2
    would go to:
    Code:
    x^3+x^2 //this is just for bug finding at the moment, and for demonstration purposes.
    3x^2+2x^1 //this is the answer
    At least, that's the idea. What I actually get is:
    Code:
    x^3+x^2
     - ( ...then some random characters which look like something out of wingdings.
    I know that what is happening is that it is both doing some crazy stuff to the function and failing to differentiate it. What I can't figure out is why.
    Here is the code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int simple(char x);
    int main()
    {
        char f[50];
        int i;
        char p;
        int n;
        printf("Write the function you want to be Differentiated: ");
        fgets(f, 50, stdin);
        printf("\n");
        for(i=0;i<50;i++)
        {
            printf("%c", f[i]); //This is just in to make sure the string is right, and this is where the problems are.
        }
        getchar();
        for(i=0;i<50;i++)
        {
            if (f[i]=="x" && f[i+1]=="^")
            {
                simple(f[i+2]);
            }
            if (f[i]=="+")
            {
                printf("+");
            }
            if (f[i]=="-")
            {
                printf("-");
            }
        }
        getchar();
        return 0;
    }
    int simple(char x)
    {
      int n;
      n = atoi(&x);
      printf("n");
      printf("%d", n);
      printf("x^");
      printf("%d", n-1);
      return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    You're just blindly running past any legitimate characters in the array. You should determine the inputted length and the loop should terminate there.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    19
    Ok, that got rid of the random characters. But it's still not running the actual differentiation process.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by mortalc View Post
    Code:
            if (f[i]=="x" && f[i+1]=="^")
            {
                simple(f[i+2]);
            }
            if (f[i]=="+")
            {
                printf("+");
            }
            if (f[i]=="-")
            {
                printf("-");
            }
    f is an array of chars, so f[i] refers to a single char, which should be enclosed in single quotes. See all those double quotes? They make strings, which act like char pointers. That is fine for printf, but not for comparing to f[i]. Change all the double quotes to single quotes in your comparisons. Turn the warnings up on your compiler, so you can see the warnings/errors that were produced by your incorrect usage of ". Then, make sure you correct all the problems the compiler tells you about.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    19
    Thanks! It works perfectly now!

    I have another question. Different problem, same code. Should I ask it here or on a new thread? anyway, it is how to check if a char variable is a number. So for example '3' will return positive but 'n' will not.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Look in ctype.h for functions like isdigit()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Weird Strings??
    By Paul22000 in forum C++ Programming
    Replies: 12
    Last Post: 07-14-2008, 01:02 PM
  2. Replies: 5
    Last Post: 04-15-2008, 10:24 AM
  3. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  4. weird strings
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 07-06-2002, 01:42 PM
  5. Getting weird characters in Strings
    By steve8820 in forum C Programming
    Replies: 3
    Last Post: 09-18-2001, 02:49 AM