Solved

how to convert inputted text to float on c++

Posted on 2007-03-19
1
878 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 250 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

628 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