C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
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
  #1  
Old March 7th, 2002, 01:01 PM
moliver moliver is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2000
Location: Jackson, TN, USA
Posts: 18 moliver User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
data type conversions - student help

Hello,

I am a college student learning C++. This is a class administered over the internet and we have a terrible teacher who will not respond to any questions at all. He will not help us out in any way.

Our current assignment requires that we convert a couple of pointers to other data types. They start off as char and one is to be converted to an int and another to a long. We have studied static_cast and had a little about void *, but neither had an example of how to use them and the book is EXTREMELY convaluted and nearly impossible to understand.

This problem is class wide, we have a week extension just for this one exersice.

I have been able to convert the data types using atoi and atol, but these are not studied until a much later chapter, so I don't think it will be accepted. The teacher won't even tell us if they will or not.

Some of us really want to learn to be good C++ programmers, but we can't without help!

Reply With Quote
  #2  
Old March 7th, 2002, 02:27 PM
Lord MJ Lord MJ is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2001
Posts: 34 Lord MJ User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 57 m 2 sec
Reputation Power: 7
Send a message via AIM to Lord MJ Send a message via Yahoo to Lord MJ
Ok, by character pointers I assume you mean strings, because it unless your array is only one element long, or you know it's length, it's impossible to convert it without causing a core dump when you go past your memory space.


Assuming that you have a null terminated character string, what you need to do to convert it to a int is use

strlen (3) to determine the lenght of the string. That will determine the number of digits you need. You must then loop through the character array get the character, then convert the character to an int, this is really easy to do,

for example
if(character == '4'){

return 4;

}

multiply the int result by the length,

add that result to the return number, increment you array counter and decrement the length counter. Repeat.


When you're done with the iteration return your return number.


The long function works exactly the same except you must return long.


Check the man page for strlen (3)

Reply With Quote
  #3  
Old March 7th, 2002, 03:38 PM
moliver moliver is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2000
Location: Jackson, TN, USA
Posts: 18 moliver User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks so much for responding. I fairly well understand what you are saying, but I'm not real clear on how to implement it in my program - bear in mind we are only on Chapter 4 in our book and I've had no prior C++ experience. Visual Basic and PHP are all I know and that's not that extensive.

Our program:

The user inputs a phone number (555) 555-5555. Then using strtok we break it down to 3 variables (using pointers) basically resulting in:

char *areacode = 555;
char *prefix = 555;
char *number = 5555;

Then we have to concatenate the prefix and number resulting in:

char *conc_number = 5555555;

All that I can do. Next we are to convert *prefix to an int data type and *conc_number into a long data type. Now I know the length (by the way, I'm familiar with strlen), but I don't understand exactly how to do the rest.

Pardon my stupidity, I'm just not far enough along to understand this.

Reply With Quote
  #4  
Old March 7th, 2002, 08:44 PM
Lord MJ Lord MJ is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2001
Posts: 34 Lord MJ User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 57 m 2 sec
Reputation Power: 7
Send a message via AIM to Lord MJ Send a message via Yahoo to Lord MJ
Do make sure that you can't use atoi(3) and atol(3)

if you can't then you basically have to code your own atoi() and atol()

as said before,

first strlen(3) the input string, then

create a counter variable.
create a return int (or long)

Create a while loop

like while(mystring[counter] != '\0'){

get the data at the index which will be of type char.

convert the char into a numeric value. You may want to create a helper method for that.

then add the return int to the number times the length.

returnint = returnint + (number*length)

at the end of your loop, increment counter and decrement your length.

counter++;
--length.


At the end of your function, return your return int (or long)

at that's an implementation of atoi(3) and atol(3)

Reply With Quote
  #5  
Old March 7th, 2002, 09:12 PM
MJEggertson MJEggertson is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2002
Location: Seattle WA
Posts: 863 MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 22 sec
Reputation Power: 8
Well, iterating through a pointer to a character array is easy enough. All you need is pointer arithmetic if you know the size of the array.

Code:
// Assume that pMyCharArray points to the beginning of a char[].
for (int i = 0; i < strlen - 1; i++) // Assuming a null terminated array.
{
    // Do stuff to the character at *pMyCharArray.
    pMyCharArray++; // Increment the pointer to the next array element.
}


Now how to get the integer out of each character? The brute force method was already mentioned, where you compare the value of each character via a huge if structure, but there is a way to actually calculate it.

What happens when you recast a char to an int? What does this return?

Code:
char chNum = '0';
int nNum = (int) chNum;
std::cout << nNum << std::endl;


What about if chNum = '1', or chNum = '2', etc? What does the number stored in nNum represent when you cast a char to an int?

Don't forget how base10 numbers work: 1234 = (1 * 10^3) + (2 * 10^2) + (3 * 10^1) + (4 * 10^0).

-Mike

Reply With Quote
  #6  
Old March 7th, 2002, 11:06 PM
Lord MJ Lord MJ is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2001
Posts: 34 Lord MJ User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 57 m 2 sec
Reputation Power: 7
Send a message via AIM to Lord MJ Send a message via Yahoo to Lord MJ
casting a char to an int will result in the ascii value of the char being returned, not the number.


'1' = 56 I believe, so basically you can either use the brute force method, or fuddle around with ascii values, which requires you to know the ascii character set and it's quirks.


Quote:
char chNum = '0';
int nNum = (int) chNum;
std::cout << nNum << std::endl;


Uses unintuitive ascii tricks to work, not to mention use of shifting operators and the like

Reply With Quote
  #7  
Old March 8th, 2002, 12:09 AM
MJEggertson MJEggertson is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2002
Location: Seattle WA
Posts: 863 MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 22 sec
Reputation Power: 8
Heh, I was trying to be a little subtle and not give that away. When it comes to tutoring school assignments, I don't like to give out full answers, provide hints and such, and let the student understand the situation and apply the knowledge.

Ascii codes for the numbers 0-9 are 48-57, consecutive, so it isn't hard to convert and cast an int from a char.

Reply With Quote
  #8  
Old March 8th, 2002, 08:02 AM
moliver moliver is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2000
Location: Jackson, TN, USA
Posts: 18 moliver User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
To all you guys:

Thanks a million!!! I'm sure I will get this now. I happen to be a visual learner and seeing the examples makes it really clear to me. I have no problem applying it to my particular problem.

You have been a great help. I got an email from another classmate that said that the instructor admitted to him that the book failed to cover come aspects of this assignment in the chapter. However, it hasn't inspired the instructor to offer the first bit of help, and I can't understand why he picked this particular assignment out of about 40.

Anyway, you guys are great teachers and a huge credit to the C++ community. None of us are islands to ourselves, when we work together the whole benefits from the effort.

Thanks again!!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > data type conversions - student help


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

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

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





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway