-2

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'', ormean''. 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;
        }
Deanie
  • 2,316
  • 2
  • 19
  • 35
  • What is the datatype of `argv`? Ergo, what is the datatype of `argv[i]`? So you need to know how to turn a `char*` into a `float`. You have many options, but I'd personally consider using a [`std::stringstream`](http://en.cppreference.com/w/cpp). – BoBTFish Feb 05 '14 at 16:20
  • look here [link](https://www.google.com/search?btnG=1&pws=0&q=C+convert+char*+to+float) – BeerBaron Feb 05 '14 at 16:20
  • I'm voting to close this question as off-topic because it was a homework question and the edit's should not have been approved as it is now the original question + a new one. – David Jun 10 '16 at 04:56

4 Answers4

1

First, it is

char* argv[]  

not

char** argv[]  

And the data type of argv[x] is char*
ie. a (pointer to) an array of many char´s,
unknown count, but terminated with \0.
=A C-style string.

To convert it to an int-type,
use, for example, atoi or sscanf.

Jarod42
  • 203,559
  • 14
  • 181
  • 302
deviantfan
  • 11,268
  • 3
  • 32
  • 49
0

Depending on your compiler you should have:

atof( char *str );

that converts char to float, and as pointed out by my colleague, you'll have:

char* argv[]

not, char** argv[];

IssamTP
  • 2,408
  • 1
  • 25
  • 48
0

argv[i] will have type char *; your value arguments will be string representations of floating-point numbers, like "3.1415". To convert a string representation of a floting-point number to it's floating-point equivalent, you can do one of the following:

#include <stdio.h>
...
double val;
int success = sscanf( argv[i], "%f", &val );
if ( success == 0 )
  // argv[i] is not a valid floating-point number string

or

#include <stdlib.h>
#include <ctype.h>
...
double val;
char *chk;
val = strtod( argv[i], &chk );
if ( !isspace( *chk ) && *chk != 0 )
  // argv[i] is not a valid floating-point number string

The second method catches errors that the first won't, so it's the one I prefer.

John Bode
  • 119,563
  • 19
  • 122
  • 198
-1
int value = atoi(argv[1].c_str());

should be a little helpful.

Zuko
  • 2,764
  • 30
  • 30