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;
	
	[B]I know something goes here, I just don't know what. I was thinking "itoa" or something like that.[/B]

	cin.getline(buf, 30);

	length = strlen(buf);

	for (i = 0; i < length; i++)
	{
		if ((buf[i] < '0') || (buf[i] > '9'))
		{
			valid = false;
		}
	}
	return valid;
}

Recommended Answers

All 4 Replies

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.

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).

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;
}

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.

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;
}

Hey Guys, i was just searching for "POINTERS TO STRINGS" , i am having a graphics project for which what i need was to convert from pointer to string , because in graphics you can only display strings.
On the internet , i was unable to find the right solution for converting an "OBJECT" pointer to string until this thing came into my mind, i ve used a streaming object once and this may help someone.
I am adding this thing to this thread cuz i got this thread from search. SO, IF YOU WANT TO CONVERT NOT ONLY INTeger POINTER BUT ALSO SOME OBJECT POINTER TO STRING, THIS WILL HELP YOU:(SD'EEZ)

#include <cstdlib>
#include <iostream>
#include <sstream>

using namespace std;

class object{
      };
int main(int argc, char *argv[])
{
    object obj;
    string s;
    ostringstream ost;
    ost<<&obj;
    s=ost.str();
    cout<<"Object pointer through String = "<<s
        <<"\nNormal Pointer = "<<&obj<<endl;
        
    system("PAUSE");
    return EXIT_SUCCESS;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.