Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

how to convert inputted text to float on c++

Posted on 2007-03-19
1
Medium Priority
?
882 Views
Last Modified: 2008-01-09
Hi Experts;

i have c++ string to float conversation problem;
my scenario;
application getting variable from environment variable (APP) and convert to float and summing this float variable with loop variable and last step set environment variable with result

i compile this codes (foo.cpp)
c++ foo.cpp -o foo

but result

foo.cpp: In function âint main()â:
foo.cpp:18: error: cannot convert âfloatâ to âconst char*â for argument â2â to âint setenv(const char*, const char*, int)â
foo.cpp:21: error: cannot convert âchar*â to âfloatâ in assignment

how to convert inputted text to float on c++
my code ;

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
#include <string>

using namespace std;

int main ()
{
  float temp_var;
  string env;
  int i;
   getline(cin,env);
   for (i=0;i<=100;i++)
   {
       temp_var = atof(getenv("APP"));  
       setenv("APP",temp_var+i,1);  
       cout << i << endl;
   }
   temp_var= getenv("APP");  
   cout << temp_var<< endl;
  return 0;
}
 

Thanks ..
0
Comment
Question by:nedimk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 86

Accepted Solution

by:
jkr earned 1000 total points
ID: 18750283
For 'setenv()', the 2nd parameter needs to be a 'const char*', you cannot pass a float - you'll need to convert that first. Make that

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main ()
{
  float temp_var;
  string env;
  int i;
   getline(cin,env);
   for (i=0;i<=100;i++)
   {
       stringstream ss;
       string val;
       temp_var = atof(getenv("APP"));  
       ss << (temp_var + i);
       val = ss.str();
       setenv("APP",val.c_str(),1);  // pass a 'const char*'
       cout << i << endl;
   }
   temp_var= getenv("APP");  
   cout << temp_var<< endl;
  return 0;
}
 
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question