| | |
Converting From Pointer/Address to String for Validation
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Apr 2008
Posts: 7
Reputation:
Solved Threads: 0
I would first like to start this post with a sincere Thank you to everyone who has posted and will post help for my problem. Without you guys, I would be banging my head on my desk.
Well, I got another problem. This time with a function that takes a pointer as an input. My teacher has informed me that I must take that pointer or address (I can't remember which it is) and convert it to a string. From then, I need to check the validity of it (between 0 and 100).
Here's the code:
Well, I got another problem. This time with a function that takes a pointer as an input. My teacher has informed me that I must take that pointer or address (I can't remember which it is) and convert it to a string. From then, I need to check the validity of it (between 0 and 100).
Here's the code:
bool GetScore(int *number)
{
bool valid = true;
char buf[30];
int length;
int i = 0;
I know something goes here, I just don't know what. I was thinking "itoa" or something like that.
cin.getline(buf, 30);
length = strlen(buf);
for (i = 0; i < length; i++)
{
if ((buf[i] < '0') || (buf[i] > '9'))
{
valid = false;
}
}
return valid;
} Please clarify - what is the pointer pointing to, a string or an int? Is your function supposed to do input? Your description sounds like its role is to validate something which was input elsewhere.
If you are testing that a some string content represents a number, your loop is valid for testing that it contains digits, but is not testing for the overall value of 0-100. Once you are certain the string contains only digits, you can convert it to the numeric value, using functions such as atoi( ), and then test the resulting number.
If you are testing that a some string content represents a number, your loop is valid for testing that it contains digits, but is not testing for the overall value of 0-100. Once you are certain the string contains only digits, you can convert it to the numeric value, using functions such as atoi( ), and then test the resulting number.
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
•
•
Join Date: Jun 2007
Posts: 275
Reputation:
Solved Threads: 45
•
•
•
•
I must take that pointer or address (I can't remember which it is) and convert it to a string
I'm also assuming that GetScore is supposed to set number with the score that is entered (only if it's valid though). This requires a conversion from string to integer (you can use atoi, or preferably a stringstream).
CPP Syntax (Toggle Plain Text)
bool GetScore(int *number) { bool valid = true; char buf[30]; int length; int i = 0; cin.getline(buf, 30); length = strlen(buf); for (i = 0; i < length; i++) { if ((buf[i] < '0') || (buf[i] > '9')) { valid = false; } } if(valid){ // convert string to integer and assign it to number *number = atoi(buf); } return valid; }
Last edited by dougy83; Apr 13th, 2008 at 2:50 am. Reason: sounded rude
•
•
Join Date: Apr 2008
Posts: 7
Reputation:
Solved Threads: 0
I was able to get it. You were right with you're coding and Vnames was right, I forgot to add the part that verifys if its between 0 and 100. which would make it.
C++ Syntax (Toggle Plain Text)
bool GetScore(int *number) { bool valid = true; char buf[30]; int length; int i = 0; cin.getline(buf, 30); length = strlen(buf); for (i = 0; i < length; i++) { if ((buf[i] < '0') || (buf[i] > '9')) { valid = false; } } if (valid) { *number = atoi(buf); } if ((*number < 0) || (*number > 100)) { valid = false; } return valid; }
![]() |
Other Threads in the C++ Forum
- Previous Thread: 2D array and nested for loop
- Next Thread: class question
Views: 2738 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






