Thanks to all of you guys the atof somehow works this time. And I forgot to mention that it is a C++ class so some of the code is seeming complicated to me:)
The original question is:
write a program that will find the min, max, or mean of a set of floating point numbers. The first argument to your program will be a string that specifies which operation the user desires:
min'',
max'',sum'', or
mean''. All other arguments are numbers.
If main is defined to accept arguments
int main (int argc, char** argv[])
and let's say it is compiled into an executable file anexe.exe, the way to use the program we learnt in class is type the following in the cmd or shell: anexe max 1 2 3 4 5
===========
My question is:
The arguments should be put in an array argv[], and argv[0] is the file name, argv[1] is the string min/max/sum/mean , and argv[2] and later should be numbers. But What is the data type of these? Are they char ? If so, how can I convert them to the float?
=========== My code is :
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
int main (int argc, char* argv[])
{
std::string option = std::string(argv[1]);
if (argc >1)
{
int i = 2;
if ( option == "min")
{
float least = atof (argv[2]);
for( i = 2;i <=(argc-1);i++)
{
int temp = atof(argv[i]);
if (temp < least)
{least = temp;}
}
cout <<"\n"<<least;
return 0;
}