954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

please help with code emmidiately

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.

#include<iostream>
#include<string>
#include<stdlib.h>
#include<stdio.h>
using namespace std;

void setID(int i);
void setRate(float r);
void setPrice(float p);
//using namespace std;
int main()

{

     string all,msg;

     cin>>all;    //1234,566666,0.9

     int p=0,q=0;

     for(int n=0;n<(all.length());n++)

     {

            if(  p==0 && all[n]==',')

             {

                   msg=all.substr(0,(n+1));

                   setID(atoi(msg.c_str()));

                   q=n;

                   p++;

             }

            if(p==1 && all[n]==',')

             {

                            msg=all.substr(q,(n+1));
                            cout<<"ha"<<msg<<endl<<atof(msg.c_str())<<endl;

                            setPrice(atof(msg.c_str()));

                            p++;

                            q=n;

             }       

           if(p==2 && all[n]==',')

             {

                            msg=all.substr(q,(n+1));
                            cout<<"he"<<msg<<endl<<atof(msg.c_str())<<endl;

                            setPrice(atof(msg.c_str()));
                            p++;

            }

     }

                            

}
void setID(int i)

{float ID=i;
cout<<i<<endl;}



void setRate(float r)

{
float Rate=r;
cout<<Rate<<endl;
}



void setPrice(float p)

{float Price=p;
cout<<Price<<endl;
}
STUDENT#101
Newbie Poster
16 posts since Aug 2008
Reputation Points: 8
Solved Threads: 1
 
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

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

Freaky_Chris
Master Poster
702 posts since Apr 2008
Reputation Points: 325
Solved Threads: 118
 

Learning to spell and proofreading your posts would also be helpful.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You