why is my code giving errors? :(

Hello!

This is my firs time programming and I need a little help.

I had to write a program that would run on the following commands:
C:\> yourprog.exe 109

C:\> yourprog.exe 0.5
C:\>

the results will either be a steal or split. But when I ran my code on visual studios its giving me errors...

________________________________________________
#include "stdafx.h"
#include <iostream>
#include <stdlib.h>
using namespace std;

int main(int argc, char* argv[])
{

float x= atof(argv[1]);

if ((x >= 100) && (x < 200)) // I will start the game with a steal.
{cout << "split";}
else if (x == 0 ||x == 1 ) // if the result was 0, I will steal. if the result was 1, I will steal.
{cout << "steal";}
else
{cout << "split";} // if 0.5 = split

system("pause");

}
What are the errors? Have you tried fixing them?
Firstly please edit your post so it uses code tags - select the code then press the <> button on the right under formatting.

You cannot compare floating point numbers like that. They are stored as binary fractions and cannot represent every real number. There are lots of whole numbers that aren't represented exactly. Changing the type to double doesn't help.

Google C++ floating point comparisons to find out how to do it properly.
Hey guys, thanks for your help.

So this was my original code:

#include <iostream>
using namespace std;

int main ()
{
float x;
cout << "Please enter an integer value: ";
cin >> x;

if ((x >= 100) && (x < 200)) // I will start the game with a steal.
{cout << "split";}
else if (x == 0 ||x == 1 ) // if the result was 0, I will steal. if the result was 1, I will steal.
{cout << "steal";}
else
{cout << "split";} // if 0.5 = split

system("pause");

}
___________________________________________________

It works perfectly, but I need it to run this way:

C:\> program.exe 109

it will read 109 and give the output.

What do I have to add to my original code to do this?
You should read what TheIdeasMan wrote.

And to handle command line parameters, this article might be helpful:
http://cplusplus.com/articles/DEN36Up4/
Topic archived. No new replies allowed.