how would YOU maked this c++ better

how would you improve my program

Page 1 of 1

3 Replies - 591 Views - Last Post: 04 February 2010 - 06:41 PM Rate Topic: -----

#1 pawel453  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 25-November 08

how would YOU maked this c++ better

Posted 04 February 2010 - 03:44 PM

hello, i wrote by my self and there are several things wrog, but it compiles and runs
please if you have spear time help me i would love to learn more since i am new...



                                                            
#define MAX 100
#include <iostream>
#include <time.h>
#include <stdlib.h>

int number();
int input(int);
int display(int,int, int);
int input();
long solve(int, int, int);
void displayquestion(int, int, int);
int agian = 'y';

long solve(int numA, int numB, int opbrt)
{
	long answer;
	if (opbrt == 1) answer = numA + numB;
	if (opbrt == 2) answer = numA - numB;
	if (opbrt == 3) answer = numA * numB;
	if (opbrt == 4) answer = numA / numB;
	if (opbrt >= 5 || opbrt <=0)
	{
		std::cout << "INVALID CHOICE";
		opbrt = 1;
		answer = numA + numB;
	}
	return (answer);
}
void displayquestion(int numA, int numB, int opbrt)
{
	if (opbrt == 1) std::cout << numA << "+" << numB;
	if (opbrt == 2) std::cout << numA << "-" << numB;
	if (opbrt == 3) std::cout << numA << "*" << numB;
	if (opbrt == 4) std::cout << numA << "/" << numB;
	std::cout << "   ";
}
int number()
{
	long randomn;
	randomn = rand()%MAX+1;
	return (randomn);
}
int input()
{
	int opbrt;
	std::cin >> opbrt;
	return (opbrt);
}

int main()
{
	std::cout << "\n\n\n\tHello There!!!\n\t";
	std::cout << "Let's Solve Some Math Problems...\n\t";
	std::cout << "\nNow, Would You Like...\n\t";
	std::cout << "1 = Addition\n\t2 = Subtraction\n\t";
	std::cout << "3 = Multiplication\n\t4 = Division\n\n";
	std::cout << "Enter The Number Of The Type of Problem You Want\n";
		int numA = 14, numB = 14;
		void greedings();
		int opbrt = input();
		numA = number();
		numB = number();
		display(numA, numB, opbrt);
	return 0;
}
int display(int numA, int numB, int opbrt)
{
	long input = 0;	
	long answer;
	answer = solve(numA, numB, opbrt);
	std::cout << "\n\n\t";
	std::cout << "What is...\n\n\t";
	displayquestion(numA, numB, opbrt);
	std::cin >> input;
	if (input == answer)
	{
		std::cout << "\n\n\tCORRECT!!! YOU ARE SMART, THE ANSWER";
		std::cout << " IS  " << answer << "\n";
	}
	else
	{
		std::cout << "\n\n\n\tI'M SORRY, THAT IS NOT CORRECT";
		std::cout << ", THE CORRECT ANSWER IS   " << answer;
	}
	std::cout << "PLAY AGIAN??? (Y/N)";
	std::cin >> agian;
	return 0;
}

                                                



Is This A Good Question/Topic? 0
  • +

Replies To: how would YOU maked this c++ better

#2 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: how would YOU maked this c++ better

Posted 04 February 2010 - 04:17 PM

#1 Use standard header files:

#include<iostrea>
#include<ctime>
#include<cstdlib>

#2 Work on a better interface.

#3 Add comments

It does not look like a *bad* program... I don't see anything particularly wrong with it.
Was This Post Helpful? 0
  • +
  • -

#3 achris  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 31-January 10

Re: how would YOU maked this c++ better

Posted 04 February 2010 - 06:09 PM

You should use a char for agian instead of an int. A char is for doing exactly what you're trying to do: read in just one character.

(Also, the name should be "again", not "agian".) :)

There's a C++ trick you can do to make sure the number you get from the user is a valid number:
int num;

if(cin >> num)
{
   cout << "You entered the number " << num << "!\n";
}
else
{
   cout << "That is not a number!\n";
   cin.clear();
   cin.ignore(1000, '\n');
   // These last two lines are necessary to reset cin
   // after someone enters bad input.
}

The reason this works is because when you call cin >> num, it returns a boolean: TRUE for "Everything's okay!" and FALSE for "Something went wrong!" if statements are set up to run only when whatever in the parentheses is TRUE:

if(TRUE) { // This will NOT run, because num1 is FALSE. }
if(FALSE) { // This WILL run, because num2 is TRUE. }


It doesn't matter what is in the parentheses, as long as it returns TRUE or FALSE, C++ can understand it. So putting cin >> num in the parentheses will run whatever in in the if statement only when nothing goes wrong, and everything in the else statement only when something does go wrong (like if the user enters "Bob" when he's supposed to enter a number).

The last two lines are necessary to clean up after someone enters bad input (like in the "Bob" example). If you want to know more about this, read this.

Here's a common way to do input "safely" (where the user can enter bad input, but the program won't crash):
int num; // The number I want to ask the user for.
bool keepAsking = TRUE; // A TRUE or FALSE statement

while(keepAsking)
{
   cout << "Enter a number: ";
   if(cin >> num)
   {
      // What the user entered was a number!
      keepAsking = FALSE; // Exit the 'while' loop.
   }
   {
      // This will only run if the user entered bad input.
      cout << "That is not a number!\n";
      cin.clear();
      cin.ignore(1000, '\n');
      // keepAsking is still TRUE, so the loop will run again.
   }
}

That way, the program will keep asking the user to input a number until they enter something that is valid. The while loop will keep running the code to ask for a number, checking each time to make sure keepAsking is still TRUE (just like if). If the user enters something good, we set keepAsking to FALSE, and next time the while loop checks, it will exit (because keepAsking is not TRUE).

Hope that helps! :) Have fun coding!

This post has been edited by achris: 04 February 2010 - 06:11 PM

Was This Post Helpful? 0
  • +
  • -

#4 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1086
  • View blog
  • Posts: 4,559
  • Joined: 09-June 09

Re: how would YOU maked this c++ better

Posted 04 February 2010 - 06:41 PM

As the others said, you just need to fix your loop but heres
an example when i had this assignment a while back
#include <iostream>
#include <ctime>

using namespace std;

int add(int a, int b) { cout<<a<<"+"<<b<<endl; return a+b;}
int subtract(int a, int b) { cout<<a<<"-"<<b<<endl;  return a-b; }
int divide(int a, int b) { cout<<a<<"/"<<b<<endl; return a/b; }
int multiply(int a, int b) { cout<<a<<"*"<<b<<endl; return a*b; }
typedef int (*function)(int,int);

int main()
{
	srand(time(0));
	int a, b, answer, your_answer;
	char again;
	function problem[4] = {add,subtract,divide,multiply};
	int type;

	do{
		cout<<"[1] Addition"<<endl
			<<"[2] Subtraction"<<endl
			<<"[3] Division"<<endl
			<<"[4] Multiplication"<<endl
			<<"What Type of Question Would You Like : ";
		cin>>type;
		a = rand()%100;
		b = rand()%100;
		answer = problem[type-1](a,b);
		cin.sync();
		cout<<"Enter Answer : ";
		cin>>your_answer;

		if(your_answer == answer)
			cout<<"CORRECT"<<endl;
		else
			cout<<"INCORRECT"<<endl;
		cout<<endl<<"Would you like to try this again ? : ";
		cin>>again;

	}while(toupper(again) == 'Y');

	cin.ignore();
	cin.get();

	return 0;
}


This post has been edited by ImaSexy: 04 February 2010 - 06:43 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1