473,395 Members | 1,701 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,395 software developers and data experts.

finding an integer in a string

62
Hello, I've got a problem and would appreciate any help.
I have to count the number of integers in a string like this "1.1 some text. 1.2 some text", where it should return 0, as there are no integers, but I still get 2 on the output.
Here is the code:

Expand|Select|Wrap|Line Numbers
  1. void IntegerNumber()
  2. {
  3.     int NumberOfIntegers = 0;
  4.     string SubString;
  5.     int flag;
  6.  
  7.     while (!file.eof())
  8.     {
  9.         getline(file, SubString, ' ');
  10.         flag = 0;
  11.         for (int i = 0; i < SubString.length(); i++)
  12.         {
  13.             if (SubString[i] >= '0' && SubString[i]<= '9') flag = 1;
  14.             else flag = 0;
  15.         }
  16.         if (flag == 1) ++NumberOfIntegers;
  17.     }
  18.     cout << "The number of Integers = " << NumberOfIntegers << endl; 
  19. }
  20.  
And I also need a second function, which counts the doubles in the same string, could anyone give me a hint what I need to change in this code for that?
Thanks a lot!
Jan 28 '07 #1
2 2163
willakawill
1,646 1GB
In your example the 2 integers will be 1 and 1. You need a more sophisticated algorithm to detect the period in the middle. In a normal sentence the period is folowed by a space so you can use this information to detect both an integer and a double.

good luck
Jan 28 '07 #2
jewel87
62
Thanks a lot for your reply!!!
I now got the idea: In the else statement after the flag goes to 0, I've added one line: break; and it is working now!!! ;))
Jan 28 '07 #3

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

Similar topics

5
by: Mike | last post by:
Hi I've written a module to find the first occurrence of a record in a query (that is my forms datasource) that matches four specified criteria. No matter what I try it doesn't seem to find ...
2
by: B Moor | last post by:
I have a database with 100,000's records, each with a unique reference, eg A123BNK456 I would like to generate a search facility whereby we can choose an exact match or partial match, where the...
10
by: tshad | last post by:
I have a Datagrid with a column: <asp:HyperLinkColumn DataTextField="JobTitle" DataNavigateUrlField="PositionID" DataNavigateUrlFormatString="AddNewPositions.aspx?PositionID={0}"...
2
by: Thomas Müller | last post by:
Hi, I need a way to find the names of the computers that are reachable over a network. Ideally when it is executed it should report back wich computernames are present. I have seen one...
12
by: Mike Smith | last post by:
Hey anyone knows how to find an item in a list view based on text ? Cant seem to get the IndexOf method working. would the LVM_FINDITEM method using SendMessage API work in .Net ?
14
by: Mr. B | last post by:
I want to return the name of the drawing file (DWG) from the end of a string. The string will be of varying lengths... as well as the drawing file name itself. I could do it the long way by...
2
by: dave m | last post by:
I'm trying to create a function that , when passed a value from a cell in a data grid, will return that value if it is an integer, otherwise it returns a 0 (like if the value were from a empty cell...
3
nomad
by: nomad | last post by:
When I run choice == 2 I'm suppose to a an out.println back finding a product. but I get Null for all the values. Can someone help me with this. class Computer_listing { private String id; ...
5
by: aleya | last post by:
I am developing a program using VB.NET that will accept a start and end location (2 list boxes), the system then will generate the shortest path to reach the end point. for your information i got a...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.