473,385 Members | 1,973 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Splitting a string into different variables

a string contains an equation and I have to break the string into integers and other variables :

example of the string is "2 + 3 = 5"

I have tried to use substr but it does not work as I can have "120 - 30 = 90" so the integers are of no fixed length.

How can I do that, please help!
Oct 11 '06 #1
4 3131
a string contains an equation and I have to break the string into integers and other variables :

example of the string is "2 + 3 = 5"

I have tried to use substr but it does not work as I can have "120 - 30 = 90" so the integers are of no fixed length.

How can I do that, please help!

Hi you can use sscanf
like this
const char* p = "100-200=300";
int i,j,k;
sscanf(p,"%d-%d=%d",&i,&j,&k);

But make it sure that there should not be any space in your string expression
Oct 11 '06 #2
if(isdigit(*expr_Ite) || isDot(*expr_Ite)) // for numbers and DOT
{
while(isdigit(*expr_Ite) || isDot(*expr_Ite))
{
if(isDot(*expr_Ite)) // Flag to check the occurrence of DOT
{
Flag=1;
}
else
{
Temp1*=10;
Temp1+=((*expr_Ite)-48);//-48 is used to get the numbers.. ASCII value for 0 is 48... 9 is 57
}
if(Flag==1) ++Temp2; // to count number of digits after DOT
++expr_Ite;
loop++;
}//while(isDot() || is dot())
while(Temp2>1) { Temp1=Temp1/10; Temp2--; } // to manipulate the integer into float value
Value.push(Temp1);
}
Oct 11 '06 #3
vermarajeev
180 100+
Expand|Select|Wrap|Line Numbers
  1. int main(int argc, char** argv[])
  2. {
  3.     string str;
  4.     string variable;
  5.     string integers;
  6.     cout<<"Enter the string"<<endl;
  7.     cin>>str;
  8.  
  9.     int j=0;
  10.     bool flag = false;
  11.     for(int i=0; i<strlen(str.data()); ++i)
  12.     {
  13.         if(!isdigit(str[i]))
  14.         {
  15.             variable.push_back(str[i]);                
  16.             flag = true;
  17.         }
  18.         else
  19.         {            
  20.             integers.push_back(str[i]);    
  21.         }
  22.         if(flag)
  23.         {
  24.            integers.push_back('\n');    
  25.            variable.push_back('\n');    
  26.            flag = false;
  27.         }
  28.     }    
  29.     cout<<"variable:"<<variable<<endl;
  30.     cout<<"integers:"<<integers<<endl;
  31.  
  32.     return 0;
  33. }
Oct 11 '06 #4
Thanks for the code, guys. Another question, I have to test the maximum input done on the screen, let me explain when keying the input after the 5th input the pgm should display a message like invalid input.

example : I type the equation

2 + 3 = 5 (input = 5 )

but if I type

2 + 3 = 5 + 0 (input more than 5)

how do I do the test since the coding is like that@

int var1, var2 , result;
string op, eq;

cin >> var1 >> op >> var2 >> eq >> result;

I am stuck who can help pls?
Oct 11 '06 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: Skybuck Flying | last post by:
Hi, I would like to split main.c which contains all code into the following Step 1. Machine.h Step 2. drand.h and drand.c Step 3. nrand.h and nrand.c Main.c then only contains some test...
4
by: JeffM | last post by:
Quick C# question: I have comma delimited values in a string array that I want to pass to seperate variables. Any tips on splitting the array? Thanks in advance! JM
20
by: Opettaja | last post by:
I am new to c# and I am currently trying to make a program to retrieve Battlefield 2 game stats from the gamespy servers. I have got it so I can retrieve the data but I do not know how to cut up...
15
by: Daren | last post by:
Hi, I need to be able to split large string variables into an array of lines, each line can be no longer than 70 chars. The string variables are text, so I would additionally like the lines...
5
by: jonkersbart | last post by:
Dear, I have wrote a script and want to group some functions of the script in a separate modulo so that I can import the module in other scripts and use the same functions there.. The problem...
2
by: shadow_ | last post by:
Hi i m new at C and trying to write a parser and a string class. Basicly program will read data from file and splits it into lines then lines to words. i used strtok function for splitting data to...
1
by: Sheena777 | last post by:
I am getting a string from a user and Would like to split the Data base on certain cinditions, I know how to split the sting with an Array and the Split method. I am fine if the user has put that...
2
by: thanawala27 | last post by:
Hi, How to split this string: I wanted the filename and directory path in two different variables. I tried this, but it doesn't work
2
by: Alan Bak | last post by:
I have a text file that I am reading in with perl line by line and breaking into variables for processing. The lines are being broken on white spaces using split. Here are a couple of lines from...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.