Need Help for simple calculator program

sorry for my english is poor
i make simple calculator, and want to validate it, it is good but not meet my expectation. so the problem is
1. about first number validation, i convert it to string, but user still can input 22xxx where xxx was discarded, my wish is to make user only can input double.
2. about option, i try validate only accept +,-,*,/ it success but problem is user can input -+ and if wrong input like e,r it show message twice, i wish to make user can only input 1 char so if error only one message show.
3. about second number validation same as first number problem but i was wondering why it show message twice, so i want to know how to prevent double messsage.
4. when user continue there is message error on displaying on first number. i don't know what make this error happening, can anyone show me why error message happens and how to prevent it.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74

int main(){
	double a1, a2, j;
	int i;
	string input, input2;
	char opsi;

	
	cout <<"simple calculator \n";
	
	do
	{
   	cout <<"insert first number \n";
   	getline(cin, input);

   	stringstream myStream(input);
   	if (myStream >> a1)
     break;
   	cout << "Invalid number, please try again" << endl;
	} while (true);
	
	do{
		cout <<"chose your option +,-,*,/ \n";
		cin >> opsi;
		if ((opsi == '+') || (opsi == '-') || (opsi == '*') || (opsi == '/'))
		break;
		else
		cout <<"your option wrong " <<"\n";
	} while (opsi != '+' || '-' || '*' || '/');
	

	do
	{
   	cout <<"insert second number \n";
   	getline(cin, input2);

   	stringstream myStream(input2);
   	if (myStream >> a2)
     break;
   	} while (true);
		
	
	
	if (opsi == '+') {
		j = a1 + a2;
		cout << "answer: " << j <<".\n";
	}
	else if (opsi == '-') {
		j = a1 - a2;
		cout << "answer: " << j <<".\n";
	}
	if (opsi == '*') {
		j = a1 * a2;
		cout << "answer: " << j <<".\n";
	}
	if (opsi == '/') {
		j = a1 / a2;
		cout << "answer: " << j <<".\n";
	}
	while (i != 0 or i != 1){
	cout <<"enter 1 for continue or 0 to quit";
	cin	>> i;
	if (i == 1){
		main();
	}
	else if (i == 0){
		return 0;
	}
	else 
	cout << "wrong input ";
		
	}
	
}


thanks
Last edited on
1) use isdigit() then atoi()
2) use getline() then do (if string.size==1 &&(string[0]=='*'¦¦string[0]=='/'))//ect..
Last edited on

1) use isdigit() then atoi()
2) use getline() then do (if string.size==1 &&(string[0]=='*'¦¦string[0]=='/'))//ect..


thank for reply,
i success on number 2 by change to this code
1
2
3
4
5
6
7
8
9
10
cout <<"chose your option +,-,*,/ \n";
		
do{
getline(cin, opsi);
if (opsi.size() == 1 && ((opsi[0] == '+') || (opsi[0] == '-') || (opsi[0] == '*') || (opsi[0] == '/')))
break;
else
cout <<"your option wrong " <<"\n";
}
while (opsi[0] != '+' ||  '-' || '*' || '/');

but for number 1 i still blind i try to use this code
1
2
3
4
5
6
7
8
9
10
cout <<"insert first number \n";
do
	{ 	
	getline(cin, input);
	if(isdigit(input[0]))
	a1 = atof(input);
	 break;    
    else
	cout << "Invalid number, please try again" << "\n";
   	} while (true);

but the compiler said input can not be converted to double, can you tell me what wrong with the code
Last edited on
Try atof(input.c_str())

Try atof(input.c_str())


okay i try to change it to
1
2
3
4
5
6
7
8
9
10
cout <<"insert first number \n";
do
	{ 	
	getline(cin, input);
	if(isdigit(input[0]))
	a1 = atof(input.c_str();
	 break;    
         else
	cout << "Invalid number, please try again" << "\n";
   	} while (true);

but it tell me error 'else' without previous 'if'
why if clause doesn't recognized in the code
well you kind of put a break before the else statement... break means it ends the function and moves to the next one
Last edited on
i change to this code
1
2
3
4
5
6
7
8
9
do
	{ 	
	getline(cin, input);
		if (isdigit(input[0]))
		a1 = atof(input.c_str());
		else 
		cout <<"please insert number correctly. \n";
    
   	} while (!isdigit(input[0]));

but the result still same like my first code, user still can input like 1te or 12te with only 1 or 12 that read, the rest te dicarded
Last edited on
because you are only checking if the first character is a digit or not do somethign like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
    string input;
    bool error;
    double a1;
    do {
        error = false;
        std::cout << "Please enter a number: " << std::flush;
        std::getline(std::cin, input);
        for(unsigned int i = 0; i<input.size(); i++)
            if(!isdigit(input[i]))
                error = true;
        if(error)
            cout << "Invalid number - Please enter another number.\n" << std::flush;
        a1 = atof(input.c_str());
    } while(error);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <sstream>
#include <string>

template <typename T>
bool validate(const std::string& token, T& value)
{
    std::istringstream is(token) ;

    T val ;

    if ( is >> val && is )
    {
        if ( is.peek() == std::char_traits<char>::eof() )
        {
            value = val ;
            return true ;
        }
    }

    return false ;
}

int main()
{
    std::string input ;
    double num = 1.0 ;

    while ( num > 0.0 && std::getline(std::cin, input) )
    {
        if ( validate(input, num) )
            std::cout << num << '\n' ;
        else
            std::cout << input << " is not valid input.\n" ;
    }
}
1.999.9
1.999.9 is not valid input.
number
number is not valid input.
99.99
99.99
99.99e2
9999
9fe93d
9fe93d is not valid input.
0
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
string input;
    bool error;
    double a1;
    do {
        error = false;
        std::cout << "Please enter a number: " << std::flush;
        std::getline(std::cin, input);
        for(unsigned int i = 0; i<input.size(); i++)
            if(!isdigit(input[i]))
                error = true;
        if(error)
            cout << "Invalid number - Please enter another number.\n" << std::flush;
        a1 = atof(input.c_str());
    } while(error);


i change to this code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
        double a1, a2, j;
	int i, input1valid,input2valid;
	string input2, opsi, input;
	bool error;
	
	cout <<"simple calculator \n";
	
	
   	cout <<"insert first number \n";
	do
	{ 	
	    error = false;
        cout << "Please enter a number: " << flush;
        getline(cin, input);
        for( input1valid = 0; input1valid<input.size(); input1valid++)
            if(!isdigit(input[input1valid]))
                error = true;
                cout << "the number that you input: " << input <<"\n"; 
        if(error)
            cout << "Invalid number - Please enter another number.\n" << flush;
        else
        a1 = atof(input.c_str());
        
   	} while (error || getline(cin, input)=="");
	
	
	cout <<"chose your option +,-,*,/ \n";
	do{
	getline(cin, opsi);
	if (opsi.size() == 1 && ((opsi[0] == '+') || (opsi[0] == '-') || (opsi[0] == '*') || (opsi[0] == '/')))
	break;
	else
	cout <<"your option wrong " <<"\n";
	}
	while (opsi[0] != '+' ||  '-' || '*' || '/');
	
	
	
	cout <<"insert second number \n";
	do
	{ 	
	    error = false;
        cout << "Please enter a number: " << flush;
        getline(cin, input2);
        for( input2valid = 0; input2valid<input.size(); input2valid++)
            if(!isdigit(input2[input2valid]))
                error = true;
        if(error)
            cout << "Invalid number - Please enter another number.\n" << flush;
        else
        a2 = atof(input2.c_str());
        
   	} while (error || getline(cin, input2)=="");

sorry for long reply, i try modified my code according giblit, is good for first number but the second number user still can input 123er why the code doesn't work on second number.
and for cire post, i still learn and not yet figure how to use fuction, i will try to google it.

Last edited on
Topic archived. No new replies allowed.