473,395 Members | 2,689 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.

Getting the first numbers from a string

19
Hey, guys. I'm writing a program that searches through a census file and returns a name for a inputted rank. I've been trying to get this function to parse the rank string to only get only the digits in the first 4 places so that it would only return a string whose value ranges from 0 - 9999. Here's what I have so far, and it's not done by any means:

Expand|Select|Wrap|Line Numbers
  1. void rankSearch()
  2. {
  3.     string str1 = "", str2 = "";
  4.  
  5.     int rank, count = 0, leave;
  6.  
  7.     cout << "Please enter a ranking (1-1000):  ";
  8.     getline(cin >> ws, str1);
  9.     do
  10.     {
  11.     for(count = 0; count < 4; count++)
  12.         {
  13.             if(isdigit(str1.at(count)))
  14.                 str2.push_back(str1.at(count));
  15.             else
  16.                 break;
  17.         }
  18.  
  19.         rank = atoi(str1.c_str());
  20.  
  21.         if(rank <= 0 || rank > 1000)
  22.         {
  23.             cout << "Invalid rank. Please try again:  ";
  24.             getline(cin >> ws, str1);
  25.             leave = 0;
  26.         }
  27.         else
  28.             leave = 1;
  29.     }while(leave == 0);
  30.  
  31.     cout << "Names for that rank are:  " << rank << endl; //This doesn't actually do anything yet. Just for testing purposes.
  32.  
  33. }
Please tell me what I'm doing wrong, because the program crashes if I don't enter a 4 digit number.
Nov 14 '07 #1
4 1714
Ganon11
3,652 Expert 2GB
One thing I see is you are mixing getline with cin >> statements. Shouldn't it be:

Expand|Select|Wrap|Line Numbers
  1. getline(cin, str1);
Next, after you setup str2 to hold all the digits, you use atoi on str1? Then what is the point of str2?
Nov 15 '07 #2
toefraz
19
I had it where it would change str2 into a char array, then just use atoi on the the char array, but i changed it to just use a .c_str() on str1. I just didn't remove the str2. I got it working finally. I just had to fill up the string past 4 characters otherwise it would try to access out-of-bound data.
Nov 15 '07 #3
toefraz
19
Here's my final function. Tell me if you think there is a better way to go about doing this:

Expand|Select|Wrap|Line Numbers
  1. void rankSearch()
  2. {
  3.     string str;
  4.     int rank = 0, count = 0, leave = 0;    
  5.  
  6.     cout << "Please enter a ranking (1-1000):  ";
  7.  
  8.     do
  9.     {
  10.         getline(cin >> ws, str);
  11.         str += "fillfillfill"; //Fill up the string to prevent issues if there are less than 4 characters in it.
  12.  
  13.         for(count = 0; count < 4; count++)
  14.         {
  15.             if(str.at(0) == '0')
  16.             {
  17.                 rank = -1;
  18.                 str.erase((count + 1), str.length());
  19.                 break;
  20.             }
  21.             if(isdigit(str.at(count)))
  22.                 continue;
  23.             else
  24.             {
  25.                 rank = -1;
  26.                 str.erase((count + 1), str.length());
  27.                 break;
  28.             }
  29.         }
  30.  
  31.         rank = atoi(str.c_str());
  32.  
  33.         if(rank <= 0 || rank > 1000)
  34.         {
  35.             cout << "Invalid rank. Please try again:  ";
  36.             leave = 0;
  37.         }
  38.         else
  39.             leave = 1;
  40.     }while(leave == 0);
  41.  
  42.     cout << endl;
  43.     cout << "Names in rank " << rank << " are:  " << endl << endl;
  44.         for(count = 0; count < 10; count++)
  45.         cout << "   " << (count * 10) + 1900 << ":  " << info[count][rank - 1][0] << " and "
  46.              << info[count][rank - 1][1] << endl;
  47.  
  48. }
Nov 15 '07 #4
Ganon11
3,652 Expert 2GB
Well, I don't know exactly what your function is doing, now do I know why (or how) getline(cin >> ws, str) is working. BUT, if your function works, it works. Are there different ways to do this? Probably, but the most important thing about a program is that it works.
Nov 15 '07 #5

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

Similar topics

8
by: Kenny | last post by:
I've run into this problem a couple of times. When I try to get a value from a form text field and add them together like so… a = document.myform.field1.value b = document.myform.field2.value c...
1
by: Thomas | last post by:
Hi! I need help getting some information out of a text string. The text string contains different id-numbers together with a minute specification. Example: Textstring =...
5
by: Francis Bell | last post by:
I just found that my fin stream is not getting passed to my readInASpinnerbait function. Here's what I have: string readInFirstChars(ifstream &fin) { char first; string print; while...
37
by: MLH | last post by:
For example: Nz(,0) returns "300" if the value in field is 300 (currency data type) and "0" if the value is zero or null. I get strings in the query output - they are all left aligned and I...
11
by: Siv | last post by:
Hi, I seem to be having a problem with a DataAdapter against an Access database. My app deletes 3 records runs a da.update(dt) where dt is a data.Datatable. I then proceed to update a list to...
3
by: snow.carriers | last post by:
Let me first state that I'm using Borland Turbo C++, it's relatively old so the new string methods won't work. Anyways, first I'm trying to collect a line of a string (with numbers, letters,...
2
by: rustyc | last post by:
Well, here's my first post in this forum (other than saying 'HI' over in the hi forum ;-) As I said over there: ... for a little side project at home, I'm writing a ham radio web site in...
12
by: magmike | last post by:
Accidentally deleted a record. Anyway to get it back? If not, I know the ID number - which is an autonumber field. Because of the related data from other tables, would I be able to create a new...
2
by: srusskinyon | last post by:
I need some help getting unique records from our database! I work for a small non-profit homeless shelter. We keep track of guest information as well as what services we have offered for...
2
by: WP | last post by:
Hello, below is my very first python program. I have some questions regarding it and would like comments in general. I won't be able to get my hands on a good python book until tomorrow at the...
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
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?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.