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: - void rankSearch()
-
{
-
string str1 = "", str2 = "";
-
-
int rank, count = 0, leave;
-
-
cout << "Please enter a ranking (1-1000): ";
-
getline(cin >> ws, str1);
-
do
-
{
-
for(count = 0; count < 4; count++)
-
{
-
if(isdigit(str1.at(count)))
-
str2.push_back(str1.at(count));
-
else
-
break;
-
}
-
-
rank = atoi(str1.c_str());
-
-
if(rank <= 0 || rank > 1000)
-
{
-
cout << "Invalid rank. Please try again: ";
-
getline(cin >> ws, str1);
-
leave = 0;
-
}
-
else
-
leave = 1;
-
}while(leave == 0);
-
-
cout << "Names for that rank are: " << rank << endl; //This doesn't actually do anything yet. Just for testing purposes.
-
-
}
Please tell me what I'm doing wrong, because the program crashes if I don't enter a 4 digit number.
4 1714
One thing I see is you are mixing getline with cin >> statements. Shouldn't it be:
Next, after you setup str2 to hold all the digits, you use atoi on str1? Then what is the point of str2?
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.
Here's my final function. Tell me if you think there is a better way to go about doing this: - void rankSearch()
-
{
-
string str;
-
int rank = 0, count = 0, leave = 0;
-
-
cout << "Please enter a ranking (1-1000): ";
-
-
do
-
{
-
getline(cin >> ws, str);
-
str += "fillfillfill"; //Fill up the string to prevent issues if there are less than 4 characters in it.
-
-
for(count = 0; count < 4; count++)
-
{
-
if(str.at(0) == '0')
-
{
-
rank = -1;
-
str.erase((count + 1), str.length());
-
break;
-
}
-
if(isdigit(str.at(count)))
-
continue;
-
else
-
{
-
rank = -1;
-
str.erase((count + 1), str.length());
-
break;
-
}
-
}
-
-
rank = atoi(str.c_str());
-
-
if(rank <= 0 || rank > 1000)
-
{
-
cout << "Invalid rank. Please try again: ";
-
leave = 0;
-
}
-
else
-
leave = 1;
-
}while(leave == 0);
-
-
cout << endl;
-
cout << "Names in rank " << rank << " are: " << endl << endl;
-
for(count = 0; count < 10; count++)
-
cout << " " << (count * 10) + 1900 << ": " << info[count][rank - 1][0] << " and "
-
<< info[count][rank - 1][1] << endl;
-
-
}
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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 =...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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: 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...
|
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...
| |