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
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.
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