943,608 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 398
  • C++ RSS
Nov 6th, 2008
0

please help with code emmidiately

Expand Post »
hi I have crreated a conde to try and take input rom a user as a string then break it up into bits and pass it o it's
functions but the values are not releacting at the functions and anyone has any other way o converting a string into a float could they please tell me how it's done. The atof() function seems to be changing the values in the array and stoing zero instead o the original values.

here is the code.
C++ Syntax (Toggle Plain Text)
  1. #include<iostream>
  2. #include<string>
  3. #include<stdlib.h>
  4. #include<stdio.h>
  5. using namespace std;
  6.  
  7. void setID(int i);
  8. void setRate(float r);
  9. void setPrice(float p);
  10. //using namespace std;
  11. int main()
  12.  
  13. {
  14.  
  15. string all,msg;
  16.  
  17. cin>>all; //1234,566666,0.9
  18.  
  19. int p=0,q=0;
  20.  
  21. for(int n=0;n<(all.length());n++)
  22.  
  23. {
  24.  
  25. if( p==0 && all[n]==',')
  26.  
  27. {
  28.  
  29. msg=all.substr(0,(n+1));
  30.  
  31. setID(atoi(msg.c_str()));
  32.  
  33. q=n;
  34.  
  35. p++;
  36.  
  37. }
  38.  
  39. if(p==1 && all[n]==',')
  40.  
  41. {
  42.  
  43. msg=all.substr(q,(n+1));
  44. cout<<"ha"<<msg<<endl<<atof(msg.c_str())<<endl;
  45.  
  46. setPrice(atof(msg.c_str()));
  47.  
  48. p++;
  49.  
  50. q=n;
  51.  
  52. }
  53.  
  54. if(p==2 && all[n]==',')
  55.  
  56. {
  57.  
  58. msg=all.substr(q,(n+1));
  59. cout<<"he"<<msg<<endl<<atof(msg.c_str())<<endl;
  60.  
  61. setPrice(atof(msg.c_str()));
  62. p++;
  63.  
  64. }
  65.  
  66. }
  67.  
  68.  
  69.  
  70. }
  71. void setID(int i)
  72.  
  73. {float ID=i;
  74. cout<<i<<endl;}
  75.  
  76.  
  77.  
  78. void setRate(float r)
  79.  
  80. {
  81. float Rate=r;
  82. cout<<Rate<<endl;
  83. }
  84.  
  85.  
  86.  
  87. void setPrice(float p)
  88.  
  89. {float Price=p;
  90. cout<<Price<<endl;
  91. }
Last edited by Ancient Dragon; Nov 6th, 2008 at 8:42 am. Reason: add code tags
Reputation Points: 8
Solved Threads: 1
Newbie Poster
STUDENT#101 is offline Offline
16 posts
since Aug 2008
Nov 6th, 2008
0

Re: please help with code emmidiately

Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Nov 6th, 2008
0

Re: please help with code emmidiately

Whats wrong with cstdlib & cstdio, this is C++ not C.

The previous post uses string stream which are brilliant for this. You may also want to consider looking at strtol().

Chris
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008
Nov 6th, 2008
0

Re: please help with code emmidiately

Learning to spell and proofreading your posts would also be helpful.
Last edited by Ancient Dragon; Nov 6th, 2008 at 8:45 am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: #include "form.h" problem
Next Thread in C++ Forum Timeline: Factors of a Polynomial





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC