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;
}

Recommended Answers

All 3 Replies

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.