C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 7th, 2003, 09:20 PM
erbonjo erbonjo is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 1 erbonjo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question int *getInteger(int **ptr, const char *prompt)

I have to write a function that gets a pointer from a user.
kinda like scanf() but understands spaces(I guess).

So the type of function is:
int *getInteger(int **ptr, const char *prompt);

Question 1:
I know that char is ** ptr going to = &somevar
I'm not sure what int *prompt is going to be. (maybe %d?)

Question 2:
I have to allocate the required memory for the data type .
So the data type will be Integer. And then store this info in ptr with error checking. I'm not sure how to write this, if someone could help.

Question 3:
Ok, I'm just going to copy this from my teachers notes because I don't understand a word this is saying (if someone understands coders trying to explain stuff to people that don't understand).
I would really just like someone to explain to me what the hell he is trying to say, not just give me the anwser.

after the allocation in question 2 I have to:
display the passed in prompt to the user

read in a string (store it in input)

behavior for all functions except getString
determine whether or not the string starts with the appropriate kind of data

if the string contains the wrong kind of data, display an error message and have the user try again (repeat this until the user types in the correct kind of data)

if the string contains the appropriate kind of data, convert from the string to the appropriate data type. Store the new data in the memory allocated earlier.

If someone would like to look at the whole assignment its on:
URL

I'm not really looking for anwsers because I can do this type of stuff with a little bit of help, I just can't catch what he is saying.

Thanks

Reply With Quote
  #2  
Old May 8th, 2003, 01:43 AM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,365 7stud User rank is Private First Class (20 - 50 Reputation Level)7stud User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 18 h 9 m 25 sec
Reputation Power: 14
"I have to write a function that gets a pointer from a user."

You can't get a pointer from the user. A pointer is a type of variable that stores an address to a location in memory where there is some data. All you can get from the user is data, which you have to store in the appropriate type of variable. In simpler terms, if you needed to get an integer from the user, you might display a message that says, "Please enter an integer: ". You wouldn't ask, "Please enter an integer variable: ", otherwise they might enter: int my_number.

Question 1: unintelligible

Question 2: You have to create a variable that will store the user input. Usually, you are given what data is going to be entered and then you decide what variables are going to handle that. This problem is approaching things in reverse. You need to create the variable type you've been given, and then read data into the variable. If you don't know how to create a variable of one of the basic types and get input from the user, I suggest you buy a good C++ book(Ivor Horton's Beginning C++) or read some online tutorials.

Question 3:
display the passed in prompt to the user --> in the function, you need to write some code that will display the variable called prompt, which is a parameter of the function, to the screen

read in a string (store it in input)-->in the function ask the user for the appropriate input(=string) and store it in the variable called input, which is also a parameter of the function.

behavior for all functions except getString
determine whether or not the string starts with the appropriate kind of data-->make sure characters in the string entered by the user are "appropriate". What is "appropriate"? I can't be sure because the problem doesn't explicitly say so, but given the function names and what you're instructed to do with the input, it looks like you're supposed to check to make sure the user entered ints for the GetInteger() function, doubles for the Get Double() function, etc. You are reading the input into a char variable, which can store anything, so for the GetInteger() function if the user entered: abde, that isn't what you want. You want them to enter something like 9.

if the string contains the wrong kind of data, display an error message and have the user try again (repeat this until the user types in the correct kind of data) --> seems clear

if the string contains the appropriate kind of data, convert from the string to the appropriate data type. Store the new data in the memory allocated earlier-->if the data entered is of the correct form you can use functions like atoi()--alpha to int--to convert the numbers to the appropriate type and store them in the two dimensional array ptr.

Last edited by 7stud : May 8th, 2003 at 04:17 AM.

Reply With Quote
  #3  
Old May 8th, 2003, 10:18 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,132 dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 19 h 54 m
Reputation Power: 1949
Re: int *getInteger(int **ptr, const char *prompt)

Quote:
Originally posted by erbonjo
I have to write a function that gets a pointer from a user.
kinda like scanf() but understands spaces(I guess).

So the type of function is:
int *getInteger(int **ptr, const char *prompt);

...



Part of your confusion seems to be centered around the function and its parameters.

prompt is a character string, or more properly a character array. It is const because the function will use it, but cannot change it. This is the string you will output as a prompt for the user.

ptr is a pointer to an integer array. I only scanned your message quickly, but my understanding is that you will receive a number of integer values from the user which you will then create an array for and store them in that array. ptr has to be a pointer to a pointer to integers so that you can return the address of the memory space you just allocated dynamically (with malloc() if in C, or with new if in C++).

My recommendation is to have a local variable that is an int* and that you use to allocate the array. Then assign that value to *ptr just before you return. Just to be able to keep everything straight (I recently got turned around trying to do the same kind of thing without local variables).

Then it appears that the function's return value is the address of the integer array that you created.

Last edited by dwise1_aol : May 8th, 2003 at 10:21 AM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > int *getInteger(int **ptr, const char *prompt)

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap