0

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;
}
4
Contributors
3
Replies
4
Views
8 Years
Discussion Span
Last Post by Ancient Dragon
0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.