Solved

how to convert inputted text to float on c++

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

830 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