Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

trying to get my c++ arithmetic calculator to detect multiple periods(AKA illegal decimals 1.2.3)

so I was able to get my code to detect characters and not allow them, but now I need it to detect multiple .'s if that makes sense, such as allow 1.23 but not 1.2.3, Please I need help as this is due in 3 hours and even my professors are too busy with other shit to help a student.

#include <iostream>
#include <cstdlib>
#include <time.h>
#include <math.h>
#include <errno.h>   


using namespace std;

const double MAXRANGE = pow(2.0, 16.0); // 65536
const double MINRANGE = -pow(2.0, 16.0);

bool validDouble(const char* argument)
{
    return atof(argument);
}

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

    char z;

    bool isARealDouble = true;
    
    if (argc == 3)
    {
        z = 0;
    }
    else if (argc == 4)
    {
        z = argv[3][0];
    }
    else if (argc < 3)
    {
        cout << "P" << endl;
        return 0;
    }
    else if (argc > 4)
    {
        cout << "P" << endl;
        return 0;
    }

    if ((z == 'a') || (z == 's') || (z == 'm') || (z == 'd') || (z == 'p') || (z == 0))
    {


        if (z == 0)
        {
            float h;
            float x = atof(argv[1]);
            float y = atof(argv[2]);
            if (x <= MINRANGE)
            {
                cout << "R" << endl;
                return 0;
            }
            else if (x >= MAXRANGE)
            {
                cout << "R" << endl;
                return 0;
            }
            if (y <= MINRANGE)
            {
                cout << "R" << endl;
                return 0;
            }
            else if (y >= MAXRANGE)
            {
                cout << "R" << endl;
                return 0;
            }

            for (int i = 1; i < argc; i++)
            {
                isARealDouble = true;

                for (int j = 0; j < strlen(argv[i]); j++)
                {
                
                    cout << argv[i][j] << " ";
                    if (isdigit(argv[i][j]) || argv[i][j] == '.')
                    {   
                        isARealDouble = true;
                                            }
                    else
                    {
                    isARealDouble = false;  
                    cout << "X" << endl;
                    return 0;
                    }
                        
                }
                
                
            }
            if (isARealDouble)
            {
                h = x + y;
                cout << h << endl;
                return 0;
            }
        }

        
    }
    

    return 0;
}

Ignore the if((z == 'a') || (z == 's') ... ect) stuff i left out some code that isnt important.

Link to post
Share on other sites

You could have a counter that you increment every time you detect a '.' (or 46 on the ASCII table). If the counter is higher than 1, so you have more '.', then you don't allow it.

Hopefully I actually understood what you're trying to do and this makes sense.

Desktop: Intel Core i9-9900K | ASUS Strix Z390-F | G.Skill Trident Z Neo 2x16GB 3200MHz CL14 | EVGA GeForce RTX 2070 SUPER XC Ultra | Corsair RM650x | Fractal Design Define R6

Laptop: 2018 Apple MacBook Pro 13"  --  i5-8259U | 8GB LPDDR3 | 512GB NVMe

Peripherals: Leopold FC660C w/ Topre Silent 45g | Logitech MX Master 3 & Razer Basilisk X HyperSpeed | Beyerdynamic Custom One Pro Plus & iFi ZEN DAC | Audio-Technica AT2020USB+

Displays: Alienware AW2521HF & BenQ BL2420PT

Link to post
Share on other sites
14 minutes ago, Mateyyy said:

You could have a counter that you increment every time you detect a '.' (or 46 on the ASCII table). If the counter is higher than 1, so you have more '.', then you don't allow it.

Hopefully I actually understood what you're trying to do and this makes sense.

#include <iostream>
#include <cstdlib>
#include <time.h>
#include <math.h>
#include <errno.h>   


using namespace std;

const double MAXRANGE = pow(2.0, 16.0); // 65536
const double MINRANGE = -pow(2.0, 16.0);

bool validDouble(const char* argument)
{
	return atof(argument);
}

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

	bool isARealDouble = true;
	
	if (argc == 3)
	{
		z = 0;
	}
	else if (argc == 4)
	{
		z = argv[3][0];
	}
	else if (argc < 3)
	{
		cout << "P" << endl;
		return 0;
	}
	else if (argc > 4)
	{
		cout << "P" << endl;
		return 0;
	}

	if ((z == 'a') || (z == 's') || (z == 'm') || (z == 'd') || (z == 'p') || (z == 0))
	{


		if (z == 0)
		{
			float h;
			float x = atof(argv[1]);
			float y = atof(argv[2]);
			if (x <= MINRANGE)
			{
				cout << "R" << endl;
				return 0;
			}
			else if (x >= MAXRANGE)
			{
				cout << "R" << endl;
				return 0;
			}
			if (y <= MINRANGE)
			{
				cout << "R" << endl;
				return 0;
			}
			else if (y >= MAXRANGE)
			{
				cout << "R" << endl;
				return 0;
			}


			for (int i = 1; i < argc; i++)
			{
				isARealDouble = true;

				for (int j = 0; j < strlen(argv[i]); j++)
				{
					
					cout << argv[i][j] << " ";
					if (isdigit(argv[i][j]) || argv[i][j] == '.')
					{	
						isARealDouble = true;
                      	int w = 0
						if (argv[i][j] == '.')
						{
							w =+ 1;
							if (w > 1)
							{
								cout << "X" << endl;
								return 0;
							}
						}
					}
					else
					{
					isARealDouble = false;	
					cout << "X" << endl;
					return 0;
					}
						
				}
				
				
			}
			if (isARealDouble)
			{
				h = x + y;
				cout << h << endl;
				return 0;
			}
		}

	}
	

	return 0;
}

I added a counter but it isnt working it resets w back to zero, I dont know how to keep the value.

Link to post
Share on other sites
6 minutes ago, WoodyWoo said:

I added a counter but it isnt working it resets w back to zero, I dont know how to keep the value.

Try adding in "int w = 0;" before the j loop.

for (int i = 1; i < argc; i++)
{
	int w = 0;
    isARealDouble = true;
    
    for (int j = 0; j < strlen(argv[i]); j++);
    ...
    

 

Desktop: Intel Core i9-9900K | ASUS Strix Z390-F | G.Skill Trident Z Neo 2x16GB 3200MHz CL14 | EVGA GeForce RTX 2070 SUPER XC Ultra | Corsair RM650x | Fractal Design Define R6

Laptop: 2018 Apple MacBook Pro 13"  --  i5-8259U | 8GB LPDDR3 | 512GB NVMe

Peripherals: Leopold FC660C w/ Topre Silent 45g | Logitech MX Master 3 & Razer Basilisk X HyperSpeed | Beyerdynamic Custom One Pro Plus & iFi ZEN DAC | Audio-Technica AT2020USB+

Displays: Alienware AW2521HF & BenQ BL2420PT

Link to post
Share on other sites
3 minutes ago, Mateyyy said:

Try adding in "int w = 0;" before the j loop.


for (int i = 1; i < argc; i++)
{
	int w = 0;
    isARealDouble = true;
    
    for (int j = 0; j < strlen(argv[i]); j++);
    ...
    

 

That was it thank you so much

 

Link to post
Share on other sites

You should also look into finite state machines and think how pocket calculators do these things when they use 4 bit or 8 bit microcontrollers with maybe tens of bytes of memory

 

Some links to read

 

https://medium.com/@rvunabandi/making-a-calculator-in-javascript-64193ea6a492

https://www.codinglawyer.io/posts/build-javascript-calculator

https://www.clear.rice.edu/comp212/06-spring/labs/13/

https://gist.github.com/freshwater/3805768

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Newegg

×