please help with code emmidiately

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2008
Posts: 16
Reputation: STUDENT#101 is an unknown quantity at this point 
Solved Threads: 1
STUDENT#101 STUDENT#101 is offline Offline
Newbie Poster

please help with code emmidiately

 
0
  #1
Nov 6th, 2008
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.
  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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: please help with code emmidiately

 
0
  #2
Nov 6th, 2008
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 671
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: please help with code emmidiately

 
0
  #3
Nov 6th, 2008
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
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,629
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1496
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: please help with code emmidiately

 
0
  #4
Nov 6th, 2008
Learning to spell and proofreading your posts would also be helpful.
Last edited by Ancient Dragon; Nov 6th, 2008 at 8:45 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC