Solved

how to convert inputted text to float on c++

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 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.

752 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