GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 02-Sep-2003, 15:12
Dariklar Dariklar is offline
New Member
 
Join Date: Sep 2003
Location: Illinois
Posts: 9
Dariklar is on a distinguished road

Beginning C++ math prog


CPP / C++ / C Code:
// File name: math.cpp
// Author: Josh Kilhoffer
// Email address: [email]Dariklar@hotmail.com[/email]
// Description: simple math program.
// Last changed: September 2nd, 2003 5:01PM central

#include <iostream>
using namespace std;

int main()
{
     double first_number, second_number, sum, product, difference, divisor;
	 char A, B, C, D, response;
	     cout.setf(ios::fixed);
		 cout.setf(ios::showpoint);
		 cout.precision(3);

     cout << "What would you like to do?\n";
	 cout << "Would you like to: (A) Add two numbers, (B) Subtract two numbers,\n";
	 cout << " (C) Multiply two numbers, or (D) Divide two numbers?\n";
	 cout << "When entering your answer, please be sure to capitalize the letter.\n";
	 cin >> response;
	 if response = A;
	 {

		 cout << "What are your two numbers?\n";
		 cin >> first_number;
	     cin >> second_number;
		 sum = first_number + second_number;
		 cout << "The sum of those two numbers is " << sum << endl;
	 }
	 if response = B;
	 {
		 cout << "What are your two numbers?\n";
		 cin >> first_number;
		 cin >> second_number;
		 difference = first_number - second_number;
		 cout << "The difference between those two numbers is " << difference << endl;
	 }
	 if response = C;
	 {
		 cout << "What are your two numbers?\n";
	     cin >> first_number;
		 cin >> second_number;
		 product = static_cast<double>(first_number) * (second_number);
		 cout << "The product of those two numbers is " << product << endl;
	 }
	 if response = D;
	 {
		 cout << "What are your two numbers?\n";
		 cin >> first_number;
		 cin >> second_number;
		 divisor = static_cast<double>(first_number)/(second_number);
		 cout << "The first number goes into the second number " << divisor << " times\n";
	 }
     return 0;
}
when I try to compile I get these errors...4 to be exact...any help would be appreciated...
CPP / C++ / C Code:
--------------------Configuration: math - Win32 Debug--------------------
Compiling...
math.cpp
C:\Programs\math.cpp(18) : error C2061: syntax error : identifier 'response'
C:\Programs\math.cpp(27) : error C2061: syntax error : identifier 'response'
C:\Programs\math.cpp(35) : error C2061: syntax error : identifier 'response'
C:\Programs\math.cpp(43) : error C2061: syntax error : identifier 'response'
Error executing cl.exe.

math.obj - 4 error(s), 0 warning(s)
__________________
Dariklar, 63 human monk
Migboaf, 64 dwarvish cleric
Xegony, EverQuest.
http://dariklar.googlepages.com
  #2  
Old 02-Sep-2003, 18:33
Dariklar Dariklar is offline
New Member
 
Join Date: Sep 2003
Location: Illinois
Posts: 9
Dariklar is on a distinguished road

Fixed it!


CPP / C++ / C Code:
// File name: math.cpp
// Author: Josh Kilhoffer
// Email address: [email]Dariklar@hotmail.com[/email]
// Description: simple math program.
// Last changed: September 2nd, 2003 7:49PM central

#include <iostream>
using namespace std;

int main()
{
     double first_number, second_number, sum, product, difference, divisor;
	 char A, B, C, D, response;
	 A = 'A';
	 B = 'B';
	 C = 'C';
	 D = 'D';

	     cout.setf(ios::fixed);
		 cout.setf(ios::showpoint);
		 cout.precision(3);

     cout << "What would you like to do?\n";
	 cout << "Would you like to: (A) Add two numbers, (B) Subtract two numbers,\n";
	 cout << " (C) Multiply two numbers, or (D) Divide two numbers?\n";
	 cout << "When entering your answer, please be sure to capitalize the letter.\n";
	 cin >> response;
	 cout << "You typed in a " << response << endl;
	 if (response == A)
	 {

		 cout << "What are your two numbers?\n";
		 cin >> first_number;
	     cin >> second_number;
		 sum = first_number + second_number;
		 cout << "The sum of those two numbers is " << sum << endl;
	 }
	 if (response == B)
	 {
		 cout << "What are your two numbers?\n";
		 cin >> first_number;
		 cin >> second_number;
		 difference = first_number - second_number;
		 cout << "The difference between those two numbers is " << difference << endl;
	 }
	 if (response == C)
	 {
		 cout << "What are your two numbers?\n";
	     cin >> first_number;
		 cin >> second_number;
		 product = static_cast<double>(first_number) * (second_number);
		 cout << "The product of those two numbers is " << product << endl;
	 }
	 if (response == D)
	 {
		 cout << "What are your two numbers?\n";
		 cin >> first_number;
		 cin >> second_number;
		 divisor = static_cast<double>(first_number)/(second_number);
		 cout << "The first number goes into the second number " << divisor << " times\n";
	 }
     return 0;
}

The answer! This math calculator works
__________________
Dariklar, 63 human monk
Migboaf, 64 dwarvish cleric
Xegony, EverQuest.
http://dariklar.googlepages.com
  #3  
Old 03-Sep-2003, 02:20
Garth Farley Garth Farley is offline
Invalid Email Address
 
Join Date: May 2002
Location: Ireland
Posts: 638
Garth Farley is a jewel in the roughGarth Farley is a jewel in the roughGarth Farley is a jewel in the rough
Yup, one of the most common errors, using = instead os == for equality.

And funny syntax of if statements. Glad you've worked that one out!

GF
  #4  
Old 05-Sep-2003, 11:54
Dariklar Dariklar is offline
New Member
 
Join Date: Sep 2003
Location: Illinois
Posts: 9
Dariklar is on a distinguished road

improved!


CPP / C++ / C Code:
// File name: math.cpp
// Author: Josh Kilhoffer
// Email address: [email]Dariklar@hotmail.com[/email]
// Description: simple calculator.
// Last changed: September 3rd, 2003 12:55PM central



#include <iostream>
#include <cmath>
using namespace std;

int main()
{
   int sum, product, difference, divisor, first_number, second_number, sqrt1;
   double remainder;
   char response;
   cout.setf(ios::fixed);            // magic formula to
   cout.setf(ios::showpoint);        // sets number of decimals
   cout.precision(1);
   cout << "What would you like to do?\n";
   cout << "Would you like to: (A) Add two numbers, (B) Subtract two numbers,\n";
   cout << "(C) Multiply two numbers, (D) Divide two numbers, or\n";
   cout << "(E) Take the square root of a number?\n";
   cin >> response;
   if ((response == 'A') || (response == 'a'))
   {
       cout << "What are your two numbers?\n";
	   cout << "First number: ";
       cin >> first_number;
	   cout << "and you want " << first_number << "added to what number? ";
	   cin >> second_number;
       sum = static_cast<double>(first_number) + (second_number);
       cout << "The sum of those two numbers is " << sum << ".\n";
   }
   if ((response == 'B') || (response == 'b'))
   {
       cout << "What are your two numbers?\n";
	   cout << "First number: ";
       cin >> first_number;
	   cout << "and you want to subtract what number from " << first_number << "?\n";
       cin >> second_number;
       difference = static_cast<double>(first_number) - (second_number);
       cout << "The difference between those two numbers is " << difference << ".\n";
   }
   if ((response == 'C') || (response == 'c'))
   {
       cout << "What are your two numbers?\n";
	   cout << "First number: ";
       cin >> first_number;
	   cout << "and you want to multiply " << first_number << "by what? ";
       cin >> second_number;
       product = static_cast<double>(first_number) * (second_number);
	   cout << "The product of those two numbers is " << product << ".\n";
   }
   
   if ((response == 'D') || (response == 'd'))
   {
       cout << "What are your two numbers?\n";
	   cout << "First number: ";
       cin >> first_number;
	   cout << "and you want to divide " << first_number << " by what?";
       cin >> second_number;
       divisor = static_cast<double>(first_number)/(second_number);
	   remainder = first_number%second_number;
       cout << "The second number goes into the first number " << divisor << " times with";
	   cout << "the remainder " << remainder << endl;
   }
   if ((response == 'E') || (response == 'e'))
   {
	   cout << "What number would you like to take the square root of?\n";
	   cin >> first_number;
	   sqrt1 = sqrt(first_number);
	   cout << "The square root of that number is " << sqrt1 << ".\n";
   }
     return 0;
}

This one works a little better, allowing for you to capitalize it or not, and able to take the sqrt, though, I don't know how to figure out the remainder of a sqrt function....
neato || conjunction that I just learned
__________________
Dariklar, 63 human monk
Migboaf, 64 dwarvish cleric
Xegony, EverQuest.
http://dariklar.googlepages.com
  #5  
Old 08-Sep-2003, 03:44
Garth Farley Garth Farley is offline
Invalid Email Address
 
Join Date: May 2002
Location: Ireland
Posts: 638
Garth Farley is a jewel in the roughGarth Farley is a jewel in the roughGarth Farley is a jewel in the rough
If you want a more precise answer for the square root function, look at these lines:

CPP / C++ / C Code:
int first_number, sqrt1;

if ((response == 'E') || (response == 'e'))
   {
     cout << "What number would you like to take the square root of?\n";
     cin >> first_number;
     sqrt1 = sqrt(first_number);
     cout << "The square root of that number is " << sqrt1 << ".\n";
   }

The answer to the suqare root should be more precise than an integer, so have sqrt1 a double or a float. Then it'll print out a more precise figure.

GF
  #6  
Old 10-Sep-2003, 06:44
Dariklar Dariklar is offline
New Member
 
Join Date: Sep 2003
Location: Illinois
Posts: 9
Dariklar is on a distinguished road

game


CPP / C++ / C Code:
#include <iostream>
using namespace std;

int main()
{
	int score1(0), score2(0), pl1choice, pl2choice, secretNum, dist1, dist2, round(0);

    cout << "This is a game that will allow two players to play a guessing game.\n";
	cout << "The goal of the game is to pick a number between 1 and 10 and be \n";
	cout << "the closest to a \"secret number \" picked at random by the computer.\n";
	cout << "Each time a player is the closest, they win a point. The game continues\n";
	cout << "until 1 of the players has 3 points.\n";

	do
	{

    cout << "\nFirst player, please enter your guess.\n";
	cin >> pl1choice;
	cout << "Second player, please enter your guess.\n";
	cin >> pl2choice;
	secretNum = (rand() % 10) +1;
	dist1 = abs(pl1choice - secretNum);
	dist2 = abs(pl2choice - secretNum);
	if (dist1 < dist2)
	{
		score1++;
	cout << "First player gets the point this time!\n\n";
	cout << "You were " << dist1 << " away from the secret number!\n\n";
	cout << "The secret number was " << secretNum << "\n\n";
	}
	if (dist2 < dist1)
	{	
		score2++;
	cout << "Second player gets the point this time!\n\n";
	cout << "You were " << dist2 << " away from the secret number!\n\n";
	cout << "The secret number was " << secretNum << "\n\n";
	}
	if (dist2 == dist1)
	{
		cout << "Tie this time, no one receives a point!\n";
		cout << "The secret number was " << secretNum << "\n\n";
	}
    round++;
	cout << "First player has " << score1 << "points.\n\n";
	cout << "Second player has " << score2 << "points.\n\n";
	cout << "This is round " << round << "!\n\n";
	} while ((score1 < 3) && (score2 < 3));
		if (score1 > score2)
			cout << "Congratulations player one on winning!\n\n";
		else
			cout << "Congratulations player two on winning!\n\n";
		return 0;
}
a game I had to make for programming class, and find its easier to post stuff here and copy + paste than email myself...
__________________
Dariklar, 63 human monk
Migboaf, 64 dwarvish cleric
Xegony, EverQuest.
http://dariklar.googlepages.com
  #7  
Old 10-Sep-2003, 11:51
Dariklar Dariklar is offline
New Member
 
Join Date: Sep 2003
Location: Illinois
Posts: 9
Dariklar is on a distinguished road
CPP / C++ / C Code:
// Created by Josh Kilhoffer
// A guessing game
// Last changed: September 10th, 2003

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
  int score1(0), score2(0), pl1choice, pl2choice, secretNum, dist1, dist2, round(0), points;
  char response;
  srand (time(0));
  cout << "This is a game that will allow two players to play a guessing game.\n";
  cout << "The goal of the game is to pick a number between 1 and 10 and be \n";
  cout << "the closest to a \"secret number \" picked at random by the computer.\n";
  cout << "Each time a player is the closest, they win a point. The game continues\n";
  cout << "until 1 of the players has 3 points.\n";
  cout << "How many points would you like to play to?\n";
  cin >> points;

  do
  {

  cout << "\nFirst player, please enter your guess.\n";
  cin >> pl1choice;
  cout << "Second player, please enter your guess.\n";
  cin >> pl2choice;
here3:
    if ((pl1choice >= 11) || (pl1choice < 1))
		goto here1;
	if ((pl2choice >= 11) || (pl2choice < 1))
		goto here2;


  secretNum = (rand() % 10) +1;
  dist1 = abs(pl1choice - secretNum);
  dist2 = abs(pl2choice - secretNum);
  if (dist1 < dist2)
  {
    score1++;
  cout << "First player gets the point this time!\n\n";
  cout << "You were " << dist1 << " away from the secret number!\n\n";
  cout << "The secret number was " << secretNum << "\n\n";
  }
  if (dist2 < dist1)
  {  
    score2++;
  cout << "Second player gets the point this time!\n\n";
  cout << "You were " << dist2 << " away from the secret number!\n\n";
  cout << "The secret number was " << secretNum << "\n\n";
  }
  if (dist2 == dist1)
  {
    cout << "Tie this time, no one receives a point!\n";
    cout << "The secret number was " << secretNum << "\n\n";
  }
    round++;
  cout << "First player has " << score1 << "points.\n\n";
  cout << "Second player has " << score2 << "points.\n\n";
  cout << "This is round " << round << "!\n\n";
  cout << "Press any key to go to the next round.\n";
  cin >> response;
  system("cls");

  } while ((score1 < points) && (score2 < points));

    if (score1 > score2)
      cout << "Congratulations player one on winning!\n\n";
    else
      cout << "Congratulations player two on winning!\n\n";
   goto end;

here1:
	{
	cout << "First player, your choice was invalid...please enter your choice again.\n";
		cin >> pl1choice;
	goto here3;
	}
here2:
	{
		cout << "Second player, your choice was invalid...please enter your choice again.\n";
		cin >> pl2choice;
		goto here3;
	}
end:

    return 0;
}

the finished game when I had finished class that day... hehe still in class now
pretty sloppy but some new neat things that I learned
__________________
Dariklar, 63 human monk
Migboaf, 64 dwarvish cleric
Xegony, EverQuest.
http://dariklar.googlepages.com
 
 

Recent GIDBlog2nd Week of IA Training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Math Formula: FORECAST JdS Open Discussion Forum 4 16-Oct-2003 08:58

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:51.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.