1 Replies - 710 Views - Last Post: 04 October 2009 - 11:45 AM Rate Topic: -----

#1 fatalanth   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 03-October 09

Hi im new

Posted 03 October 2009 - 07:04 PM

hi guys im new to c++. i learned a little about python and a scripting program for the command programming is coming along for me. however i am having some issues figuring out the gui functions of visual c++ 2005.
so far ive made my own hello world, and took my own path in a calculator program which has worked quite well, besides error handling.

heres that calculator code

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

#include <cmath>

using std::pow;

int main()
{
	double numberA = 0.0;
	double numberB = 0.0;
	double numberC = 0.0;
	char function = ' ';
	char exit = 'y';

	while (exit == 'y')
	{
		cout << "Enter mathmatical problem using +, -, *, /,^(for exponets), or %: ";
		cin >> numberA;
		cin >> function;
		cin >> numberB;

		switch (function)
	{
		case '+':
			numberC = numberA + numberB;
			break;
		case '-':
			numberC = numberA - numberB;
			break;
		case '*':
			numberC = numberA * numberB;
			break;
		case '/':
			numberC = numberA / numberB;
			break;
		case '%':
			numberC = (numberA /100) * numberB;
			break;
		case '^':
			numberC = pow(numberA, numberB);
			break;
		default:
			cout << "error incorrect function" << endl;
		}

	cout << numberC << "   continue? y/n: ";
	cin >> exit;

	}

	return 0;

}


any way it works just fine for me

(note that i didnt use quotes that is my personal preference if youd like me to explain something i will)

this is my first program so when posting help please use language i can understand as most of the commands i know are in there.

anyway the only help i would ask for so far would be:

what can i do about the error handling? (it is made to handle an incorrect function however it cant handle an incorrect number ex. 12+12 equals 24, 12u12 equals "error incorrect function", t12+12 equals endless loop of "error incorrect function")

also i would ask if there is a tutorial on visual basic 2005 that i can learn about the features of the graphic editor? (windows rather than command prompt) id like to make a window based calculator with buttons without a guide


i would also like to note that im on mac running an emulator so for now i cant do a few functions until i get the proper controls.

... that was alot of typing but in any case id like to again say hi to everyone and ask: Hows life?

Is This A Good Question/Topic? 0
  • +

Replies To: Hi im new

#2 OliveOyl3471   User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 135
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: Hi im new

Posted 04 October 2009 - 11:45 AM

I assume you want to handle errors that occur when the user inputs something other than what is expected, like a letter instead of a number?

In that case, you can do this:

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include <cmath>

using std::pow;

//This function clears the error state
//and ignores all chars up to & including a new line 
	void clearError()
	{
	  cin.clear();//clear the error state
	  cin.ignore(10000,'\n'); //ignore extra chars up to 10000 chars or a new line
	}

int main()
{
	double numberA = 0.0;
	double numberB = 0.0;
	double numberC = 0.0;
	char function = ' ';
	char exit = 'y';

	while (exit == 'y')
	{
		cout << "Enter mathmatical problem using +, -, *, /,^(for exponets), or %: ";

		cin >> numberA;
		 while(!cin.good())  //checks to see if what they input matches the data type, if so, input is good
		 {
		   cout<<"Sorry, you must enter a number.\n";
		   clearError(); //call function to clear the error when cin is not good
		   cin >> numberA; //allow user to reenter number
		 }

		cin >> function;
		cin >> numberB;

		switch (function)
	{
		case '+':
			numberC = numberA + numberB;
			break;
		case '-':
			numberC = numberA - numberB;
			break;
		case '*':
			numberC = numberA * numberB;
			break;
		case '/':
			numberC = numberA / numberB;
			break;
		case '%':
			numberC = (numberA /100) * numberB;
			break;
		case '^':
			numberC = pow(numberA, numberB);
			break;
		default:
			cout << "error incorrect function" << endl;
		}

	cout << numberC << "   continue? y/n: ";
	cin >> exit;
	exit = tolower(exit); //in case they enter an uppercase letter, this will change it to lowercase

	}

	return 0;

}


Instead of using the function, you can just use the code that is within it, but I thought since you want to check each time the user inputs something, it would use less code if we use the function.

If you prefer:
		cout << "Enter mathmatical problem using +, -, *, /,^(for exponets), or %: ";
		cin >> numberA;
		 while(!cin.good())
		 {
		   cout<<"Sorry, you must enter a number.\n";
		   cin.clear();//clear the error state
		   cin.ignore(10000,'\n'); //ignore extra chars up to 10000 chars or a new line
		   cin >> numberA; //allow user to reenter number
		 }


This post has been edited by OliveOyl3471: 04 October 2009 - 11:48 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1