Thread: conversion from string to variables

  1. #1
    Registered User
    Join Date
    Oct 2004

    Exclamation conversion from string to variables

    3 days trying to find a solution for this problem but i've failed , its the first problem that i wasnt enable to i thought about finding some help here.
    first, its all about a program which has an input from the user we can say its an equation like:
    x = a+b-c*d/e
    at first, we want to cut each variable alone then we had to scan them from the user after this we replace the values with the variables in the equation so we get the total in x.
    the problem here comes when i've tried to convert from a normal string or text into a variables which the compiler can understand.
    i've made alot of searches about this and i found 2 functions which are: atoi() or strtol()
    but lets try to do it without the help of those functions

    waiting for your help guyz..

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    but lets try to do it without the help of those functions
    Let's not. Can you show us some code of what you have so far? The process you are referring to is called tokenizing, and it's quite simple when broken down. The input will already be in the form of an array of char, go through the array with a loop and analyze each element with a switch/case statement. Ignore spaces, record the locations of symbols ( =, +, -, *, /), and then read the actual numbers (using the functions you already mentioned) into numerical data types. Then you just use some simple logic (simple if broken into pieces*) to determine how to act on the information you've gathered.

    * It's like that movie "What About Bob?". A man has trouble doing everyday activities, so his therapists gives him a book "Baby Steps". Instead of thinking, "How do I get out of this building", he learned to think, "How do you I leve this room", "Then this hall", etc... try that approach with the later logic.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  3. Replies: 4
    Last Post: 03-03-2006, 01:11 AM
  4. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 10:06 PM
  5. string handling
    By lessrain in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 07:36 PM