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: BBustos is an unknown quantity at this point 
Solved Threads: 0
BBustos BBustos is offline Offline
Newbie Poster

Converting From Pointer/Address to String for Validation

 
0
  #1
Apr 13th, 2008
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:

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;
}
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Converting From Pointer/Address to String for Validation

 
0
  #2
Apr 13th, 2008
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.
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 275
Reputation: dougy83 is on a distinguished road 
Solved Threads: 45
dougy83 dougy83 is offline Offline
Posting Whiz in Training

Re: Converting From Pointer/Address to String for Validation

 
0
  #3
Apr 13th, 2008
I must take that pointer or address (I can't remember which it is) and convert it to a string
I think you're mixed up a bit (or maybe I am). I'm assuming GetScore returns true if the user enters a valid number. You've got this correct.

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).
  1. bool GetScore(int *number)
  2. {
  3. bool valid = true;
  4. char buf[30];
  5. int length;
  6. int i = 0;
  7.  
  8. cin.getline(buf, 30);
  9.  
  10. length = strlen(buf);
  11.  
  12. for (i = 0; i < length; i++)
  13. {
  14. if ((buf[i] < '0') || (buf[i] > '9'))
  15. {
  16. valid = false;
  17. }
  18. }
  19.  
  20. if(valid){
  21. // convert string to integer and assign it to number
  22. *number = atoi(buf);
  23. }
  24.  
  25. return valid;
  26. }
Last edited by dougy83; Apr 13th, 2008 at 2:50 am. Reason: sounded rude
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 7
Reputation: BBustos is an unknown quantity at this point 
Solved Threads: 0
BBustos BBustos is offline Offline
Newbie Poster

Re: Converting From Pointer/Address to String for Validation

 
0
  #4
Apr 13th, 2008
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.

  1. bool GetScore(int *number)
  2. {
  3. bool valid = true;
  4. char buf[30];
  5. int length;
  6. int i = 0;
  7.  
  8. cin.getline(buf, 30);
  9.  
  10. length = strlen(buf);
  11.  
  12. for (i = 0; i < length; i++)
  13. {
  14. if ((buf[i] < '0') || (buf[i] > '9'))
  15. {
  16. valid = false;
  17. }
  18. }
  19. if (valid)
  20. {
  21. *number = atoi(buf);
  22. }
  23.  
  24. if ((*number < 0) || (*number > 100))
  25. {
  26. valid = false;
  27. }
  28.  
  29. return valid;
  30. }
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum


Views: 2738 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC