Solved

how to convert inputted text to float on c++

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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…
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 clear a vector as well as how to detect empty vectors in C++.

760 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now