Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 27th, 2005, 3:04 AM   #61
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5 bl00dninja is on a distinguished road
Talking global variables

accidental double-post due to browser buttons...
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.

Last edited by bl00dninja; Jan 27th, 2005 at 3:09 AM.
bl00dninja is offline   Reply With Quote
Old Jan 27th, 2005, 3:07 AM   #62
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5 bl00dninja is on a distinguished road
Talking global variables

here is the C code where i used a global variable. just think of printf as cout and scanf as cin with a slightly more esoteric sytax.

//C standard I/O library
#include <stdio.h>

//creating my own boolean types for clarity
#define true 1
#define false 0

//global variable for dynamic calculations
double dCurrentV = 0;                                //<--here is the global variable

//function declarations
double add(double x, double y);
double sub(double x, double y);
double mlt(double x, double y);
double div(double x, double y);
int clear(void);

//begin main
int main(void)  
{  
  //user-defined operation choice
  int iOpChoice;
  //boolean do-while loop sentinal value
  int iSentVal = true;
  //user-defined next number choice
  double dNext;
  
  //opening marquee
  printf("\n\n\n\n\n\n\n\n\n\n");
  printf("       ***********************************************\n"  );
  printf("       *                       *\n");
  printf("       *                       *\n");
  printf("       *                       *\n");
  printf("       *                       *\n");
  printf("       *  The four-function calculator simulation  *\n");
  printf("       *         written in C        *\n");
  printf("       *        b hancock 2004        *\n");
  printf("       *                       *\n");
  printf("       *                       *\n");
  printf("       ***********************************************\n\  n\n\n");
  printf("\n\n\n\n\n");
  
clearpoint:;
  printf("\n\n\nFirst number?");
  scanf("%lf", &dCurrentV);
  
  //do=while loop begins here
  do
  {
    
  printf("\n\n\nPlease choose a function from the list\n\n\n");
  printf("1.\tAddition\t\t4.\tDivision\n\n");
  printf("2.\tSubtraction\t\t5.\tClear (Reset)\n\n");
  printf("3.\tMultiplication\t\t6.\tQuit\n\n\n\n");
  
  scanf("%d", &iOpChoice);
  
  //switch starts here
  switch(iOpChoice)
  {
    case 1:
      printf("add?");
      scanf("%lf", &dNext);
      dCurrentV = add(dCurrentV, dNext); 
      printf("\n\n\n\n");
      printf("The current value is %lf", dCurrentV);
      break;
      
    case 2:
      printf("subtract?");
      scanf("%lf", &dNext);
      dCurrentV = sub(dCurrentV, dNext); 
      printf("\n\n\n\n");
      printf("The current value is %lf\n\n\n", dCurrentV);
      break;
      
    case 3:
      printf("multiply by?");
      scanf("%lf", &dNext);
      dCurrentV = mlt(dCurrentV, dNext); 
      printf("\n\n\n\n");
      printf("The current value is %lf\n\n\n", dCurrentV);
      break;
      
    case 4:
      printf("divide by?");
      scanf("%lf", &dNext);
      dCurrentV = div(dCurrentV, dNext);
      printf("\n\n\n\n");
      printf("The current value is %lf\n\n\n", dCurrentV);
      break;
      
    case 5:
      clear();
      goto clearpoint;    //<--try to avoid this (using goto)...i was drunk
      break;  
      
    case 6:
      iSentVal = false;
      break;
    
    default:
      printf("Invalid input\n");
      break;    
      
    } //switch ends here
  }//do-while loop ends here
  while(iSentVal != false);
  return 0;
}//main ends here
  
//function definitions  
double add(double x, double y)
{
  double result;  
  result = (x + y);
  return result;
}

double sub(double x, double y)
{
  double result;  
  result = (x - y);
  return result;
}

double mlt(double x, double y)
{
  double result;  
  result = (x * y);
  return result;
}

double div(double x, double y)
{
  double result;  
  result = (x / y);
  return result;
}

int clear(void)
{
  dCurrentV = 0;
  return dCurrentV;
}
//program ends here

i just found that in a simple program like this, using a global variable was the easiest way to go. for your development i will now post the Java and C++ variants of this same code.

in C++
#include <iostream>

using namespace std;

class Func
{
  public:
    
    Func(double firstV);
    ~Func();
    double add(double cValue);
    double sub(double cValue);
    double mlt(double cValue);
    double div(double cValue);
    int clr(double cValue);
    
  private:
    
    double cValue;
  };
  
  
  
  
  
Func::Func(double firstV)
{
  cValue = firstV;
}    

Func::~Func()
{
}

double Func::add(double cValue)
{
  double y;
  cout<<"\n\n\nAdd?\n\n\n\n"<<endl;
  cin>>y;
  cValue = (cValue + y);
  return cValue;
}

double Func::sub(double cValue)
{
  double y;
  cout<<"\n\n\nSubtract?\n\n\n\n"<<endl;
  cin>>y;
  cValue = (cValue - y);
  return cValue;
}

double Func::mlt(double cValue)
{
  double y;
  cout<<"\n\n\nMultiply by?\n\n\n\n"<<endl;
  cin>>y;
  cValue = (cValue * y);

  return cValue;
}

double Func::div(double cValue)
{
  double y;
  cout<<"\n\n\nDivided by?\n\n\n\n"<<endl;
  cin>>y;
  cValue = (cValue / y);
  return cValue;
}             

int Func::clr(double cValue)
{
  cValue = 0;
  return cValue;
}


  


int main()
{
  cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\  n"<<endl;
  cout<<"        **************************************************  ***\n";
  cout<<"        *                          *\n";
  cout<<"        *                          *\n";
  cout<<"        *                          *\n";
  cout<<"        *                          *\n";
  cout<<"        *   The four-function calculator simulation   *\n";
  cout<<"        *         written in C++          *\n";
  cout<<"        *         b hancock 2004          *\n";
  cout<<"        *                          *\n";
  cout<<"        *                          *\n";
  cout<<"        *                          *\n";
  cout<<"        *                          *\n";
  cout<<"        **************************************************  ***\n";
  
  int uChoice1;
  double cValue;
  bool cont = true;
  
  cout<<"\n\n\nInitial value?\n\n\n\n"<<endl;
  cin>>cValue;
  cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"  <<endl;
  cout<<"The current value is "<<cValue<<"\n\n"<<endl;
  
  Func ffc(cValue);
  
  do
  {    
    cout<<"\n\nPick a function.\n\n"<<endl;
    cout<<"1.\tAddition\t4.\tDivision\n\n"<<endl;
    cout<<"2.\tSubtraction\t5.\tClear\n\n"<<endl;
    cout<<"3.\tMultiplication\t6.\tQuit.\n\n"<<endl;
    cin>>uChoice1;
    
    switch (uChoice1)
    {
      case 1:
        cValue = ffc.add(cValue);
        cout<<"\n\n\n\n\n\n\n\n\n\n";
        cout<<"The current result is "<<cValue<<"\n\n\n"<<endl;
        break;
        
      case 2:
        cValue = ffc.sub(cValue);
        cout<<"\n\n\n\n\n\n\n\n\n\n";
        cout<<"The current result is "<<cValue<<"\n\n\n"<<endl;
        break;
        
      case 3:
        cValue = ffc.mlt(cValue);
        cout<<"\n\n\n\n\n\n\n\n\n\n";
        cout<<"The current result is "<<cValue<<"\n\n\n"<<endl;
        break;
        
      case 4:
        cValue = ffc.div(cValue);
        cout<<"\n\n\n\n\n\n\n\n\n\n";
        cout<<"The current result is "<<cValue<<"\n\n\n"<<endl;
        break;       
        
      case 5:
        cValue = ffc.clr(cValue);
        cout<<"\n\n\n\n\n\n\n\n\n\n";
        cout<<"The value is now cleared to "<<cValue<<"\n\n\n"<<endl;
        break;
        
      case 6:
        cont = false;
        break;  
        
      default:
        cout<<"\n\n\n\nInvalid input, please re-enter.\n\n";
        continue; 
    }     
  }while(cont == true);   
  
  return 0;
}

here it is in Java:

//import the java input/output library
import java.io.*;


//class that contains the math functions
class MFunctions {

//result variable
double result;

String inData;

//constructor...not much here...
//remnant from the days when the user input was supplied to
//this class as an argument
//MFunctions() {
//}

//addition method
double add(double v1, double v2) {
 
 result = (v1 + v2);
 return result;
}


//subtraction method
double subtract(double v1, double v2) {
 
 result = (v1 - v2);
 return result;
}


//multiplication method
double multiply(double v1, double v2) {
 
 result = (v1 * v2);
 return result;
}


//division method
double divide(double v1, double v2) {
 
 result = (v1 / v2);
 return result;
}


//clear method
double clear(double v1) {
 
 result = v1;
 return result;
}


//square method
double square(double v1) {
 
 result = (v1 * v1);
 return result;
}


//square root method
double sqroot(double v1) {
 
 result = Math.sqrt(v1);
 return result; 
}


//"power of" method
//doesn't quite work right when power is a floating-point input
double power(double v1, double v2, double v3) {
 
 int i = 1;
 
 //get initial input
 //v3 remains as the initial input
 //so that v1 can be accurately calculated
 //increment loop until power is reached
  while (i < v2) {
  v1 = (v1 * v3);
  i++;
  
  }//while loop ends here
 
 result = v1;
 
 return result;
 
  }//"power-of" method ends here


}//MFunctions class ends here






//class containing the "main" method
class FFC {

public static void main(String[] args) throws IOException {
 
 //generic input variable
 String inData, userChoice;
 
 //user-defined input variables
 double initialV, nextV;
 
 double x = 0;
 
 //boolean variable for quitting loop
 boolean sentV = false;
 
 //initiate java IO library
 InputStreamReader isr = new InputStreamReader(System.in);
   BufferedReader br = new BufferedReader(isr);
 
 //instantiate object of "MFunctions" class
 MFunctions mf = new MFunctions();

 //initial user input
   System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n  \n\n\n");
   System.out.println("Welcome to the currently expanding calculator simulation!");
   System.out.print("When prompted for a math function choice ");
   System.out.print("please type either: \nthe number of your choice, ");
   System.out.println("\nor the symbol beside the number.");
   System.out.println("\nIf you receive a result of either: ");
   System.out.println("'NaN' or 'infinity'");
   System.out.println("OR repeated calculations reach the UNEXPECTED result of either '1' or '0'");
   System.out.println("Your calculations have exceeded the bounds of the java language.");
 
   System.out.println("\n\nEnter a number.\n\n\n\n\n\n\n\n\n\n\n");
   inData = br.readLine();
   initialV = Double.parseDouble(inData);
   
   //quitting loop 
   while (sentV != true) {
    
    //determine the math function from the user
    System.out.println("\n\n\nChoose a basic math function.\n");
    System.out.println("1. +\t(addition)");
    System.out.println("2. -\t(subtraction)");
    System.out.println("3. *\t(multiplication)");
    System.out.println("4. /\t(division)");
    System.out.println("5. c\t(clear)");
    System.out.println("6. =\t(equals/finished)");
    System.out.println("7. s\t(square)");
    System.out.println("8. sq\t(square root)");
    System.out.println("9. pw\t(power of)");
    System.out.println("\n\n\n\n\n");
    userChoice = br.readLine();
    
    
    //if user chooses to add...
    if (userChoice.trim().startsWith("+") || userChoice.trim().startsWith("1") ) {
   
    //get next number from user
    System.out.println("\n\n" + initialV + " Plus?\n\n");
    inData = br.readLine();
    nextV = Double.parseDouble(inData);    
    //print current value and assign that value to initial value
    //this is repeated in the following...
    System.out.println("\n\n\n\n\n\n\n\n\n\n");
    System.out.println("Current value is: " + mf.add(initialV, nextV) + "\n");
    initialV = mf.add(initialV, nextV);
   
    }//end user add choice
   
    
    //if user chooses to subtract
    if (userChoice.trim().startsWith("-") || userChoice.trim().startsWith("2") ) {
   
    System.out.println("\n\n" + initialV + " Minus?\n\n");
    inData = br.readLine();
    nextV = Double.parseDouble(inData);   
    //see above comment in add choice
    System.out.println("\n\n\n\n\n\n\n\n\n\n");
    System.out.println("\n\nCurrent value is: " + mf.subtract(initialV, nextV) + "\n");
    initialV = mf.subtract(initialV, nextV);
   
    }//end user subtract choice
   
   
    //if user chooses to multiply
    if (userChoice.trim().startsWith("*") || userChoice.trim().startsWith("3") ) {
   
    System.out.println("\n\n" + initialV + " Multiplied by?\n\n");
    inData = br.readLine();
    nextV = Double.parseDouble(inData);   
    //see above comment in add choice
    System.out.println("\n\n\n\n\n\n\n\n\n\n");
    System.out.println("\n\nCurrent value is: " + mf.multiply(initialV, nextV) + "\n");
    initialV = mf.multiply(initialV, nextV);
   
    }//end user multiplication choice
   
    
    //if user chooses to divide
    if (userChoice.trim().startsWith("/") || userChoice.trim().startsWith("4") ) {
   
    System.out.println("\n\n" + initialV + " Divided by?\n\n");
    inData = br.readLine();
    nextV = Double.parseDouble(inData);
    //see above comment in add choice
    System.out.println("\n\n\n\n\n\n\n\n\n\n");
    System.out.println("\n\nCurrent value is: " + mf.divide(initialV, nextV) + "\n");
    initialV = mf.divide(initialV, nextV);
   
    }//end user division choice   
  
    //if user chooses to clear
    if (userChoice.trim().startsWith("c") || userChoice.trim().startsWith("5") ) {
    
    System.out.println("\n\n\n\n\n\n\n\n\n");
    System.out.println("The new initial value is zero.\n\n");
    System.out.println("Enter the new initial value.");
    System.out.println("\n\n\n\n\n");
    inData = br.readLine();
    initialV = Double.parseDouble(inData);
    System.out.println("\n\n\n\n\n\n\n\n\n");
    System.out.println("\n\nCurrent value is: " + mf.clear(initialV) + "\n");
    initialV = mf.clear(initialV);
    
    }//end user clear choice
    
    
    //if user chooses square
   if (userChoice.trim().startsWith("s") || userChoice.trim().startsWith("7") ) {
    System.out.println("\n\n\n\n\n\n\n\n\n\n");
    System.out.println("\n\nCurrent value is: " + mf.square(initialV) + "\n");
    initialV = mf.square(initialV);
    
   }//end user square choice  
   
   
   //if user chooses square root
   if (userChoice.trim().startsWith("sr") || userChoice.trim().startsWith("8") ) {
    System.out.println("\n\n\n\n\n\n\n\n\n\n");
    System.out.println("\n\nCurrent value is: " + mf.sqroot(initialV) + "\n");
    initialV = mf.sqroot(initialV);
    
   }//end user square root choice 
   
   
   //if user chooses "power-of"
   
   final double thirdV = initialV;
   
   if (userChoice.trim().startsWith("pw") || userChoice.trim().startsWith("9") ) {
     System.out.println(initialV + "\n\nTo the power of?");
     inData = br.readLine();
     nextV = Double.parseDouble(inData);
     System.out.println("\n\n\n\n\n\n\n\n\n\n");
     System.out.println("\n\nCurrent value is: " + mf.power(initialV, nextV, thirdV) + "\n");
     initialV = mf.power(initialV, nextV, thirdV);
     
   }//end user "power-of" choice
     
   
  
    //if user chooses to quit
    if (userChoice.trim().startsWith("=") || userChoice.trim().startsWith("6") ) {
   
    sentV = true;
    
    }//end user final quitting choice
       
   
  }//while loop ends here
   
   
   //final value printed on-screen for user
   System.out.println("\n\n\n\n\n\n\n");
   System.out.println("The final result is:\t" + initialV);
   System.out.println("\n\n\n\n\n\n\n");
     
   

  }//main ends here
  
  
}//class ends here

the Java program has a lot more functionality because it was the first language that i learned. maybe looking at this simple code will help you. a four-function calculator is a GREAT programming project for just starting out.
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.

Last edited by bl00dninja; Jan 27th, 2005 at 3:17 AM.
bl00dninja is offline   Reply With Quote
Old Jan 29th, 2005, 4:28 PM   #63
Broax
Hobbyist Programmer
 
Broax's Avatar
 
Join Date: Jan 2005
Location: Porto, Portugal
Posts: 142
Rep Power: 4 Broax is on a distinguished road
Send a message via MSN to Broax
I finally have Widows XP again! (following a shot period without *any* OS)
I've decided I just can't get the hang of Linux, and that I'm just too hooked on M$ stuff to change! I mean... I can't just play Vice City on Linux!

Anyway, I'm a bit drunkish, to actually try to program anything today so I'll just leave a "i'm still alive note", and I'll take programming tomorrow!


Ich liebe ihr!
__________________
PORTUGALPORTUGA
PORTUGALPORTUGA
PORTUGALPORTUGA
PORTUGALPORTUGA
PORTUGALPORTUGA
Broax is offline   Reply With Quote
Old Jan 29th, 2005, 5:00 PM   #64
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Been wondering where you'd gone. Happy coding.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Jan 30th, 2005, 3:44 PM   #65
Broax
Hobbyist Programmer
 
Broax's Avatar
 
Join Date: Jan 2005
Location: Porto, Portugal
Posts: 142
Rep Power: 4 Broax is on a distinguished road
Send a message via MSN to Broax
Character Input is EAASSY!

CIN is easy! Very easy!

And thus I managed to go through one whole lesson of the tutorial without bothering anyone!

Alhtough I did had a lot of errors in the code! :eek:

#include <iostream>
using namespace std;

int main()
{
    string myname = "Broax";
    string pretty;
    int babawaba = 42;
    string end;
        
    cout << myname << " is really sad because he doesn't know /n the if-then thing yet.. :(\n";
    cout << "Never the less he know the cin and string and int and all that shit, so say \n";
    cout << "something pretty like your name:\n";
    cout << "\n";
    cout << "BEWARE: If you're a wise-ass and type in something like a number, I shall give \n";
    cout << "a well programmed program crash! Anyway... Say something:\n";
    cout << "\n";
    cin >> pretty;
    cout << "So you said " << pretty << "! Wise choice! \n";
    cout << "And for that I shall grant you with a integer variable! The answer to all \n";
    cout << "the questions on the universe! \n";
    cout << babawaba << "\n";
    cout << "\n";
    cout << "Now to make me shut up, thus bringing an and to this horrible nightmare in console\n";
    cout << "Just press anykey followed by 'return'. \n";
    cin >> end;
    cout << "Bye bye!" endl;
    
    return 0;
}

Now I shll move onto the next lesson which is all about the wonderfull world of Control Structures!

But I have one small question... I don't think the way I used to terminate the program is actually correct so I wonder what string closes the program? It's about that...

Anyway... I figure the soon I shall be able to try to do something stupid and basic like a calculator or something like that... what'ya think?
__________________
PORTUGALPORTUGA
PORTUGALPORTUGA
PORTUGALPORTUGA
PORTUGALPORTUGA
PORTUGALPORTUGA
Broax is offline   Reply With Quote
Old Jan 30th, 2005, 5:04 PM   #66
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Go for it. A proper calculator is actually quite difficult, but one that does the 4 basic functions isn't that hard and will teach you a lot about I/O, parsing and other stuff. Yeah.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Jan 30th, 2005, 5:46 PM   #67
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 5 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
A comment about your program regarding 'cin':

You can not enter a string for an integer.
You can enter an integer for a string.
__________________

tempest is offline   Reply With Quote
Old Jan 30th, 2005, 7:42 PM   #68
Broax
Hobbyist Programmer
 
Broax's Avatar
 
Join Date: Jan 2005
Location: Porto, Portugal
Posts: 142
Rep Power: 4 Broax is on a distinguished road
Send a message via MSN to Broax
yes... I just discovered it..

My super-crash won't work! maybe I'll implement that feature on a newer version of Super-Usless-App!
__________________
PORTUGALPORTUGA
PORTUGALPORTUGA
PORTUGALPORTUGA
PORTUGALPORTUGA
PORTUGALPORTUGA
Broax is offline   Reply With Quote
Old Feb 1st, 2005, 9:48 AM   #69
Broax
Hobbyist Programmer
 
Broax's Avatar
 
Join Date: Jan 2005
Location: Porto, Portugal
Posts: 142
Rep Power: 4 Broax is on a distinguished road
Send a message via MSN to Broax
First of all... Sorry for the double post...
Secondly... Well, I know you'll find my programming attempts f***ing pathetic, but I managed to make a very simple calculator all by my own.

It truly sucks, but it does the basic 2+2=4 stuff, and it's a somewhat creative solution for my "how the f*** will I do this" question.

Actually I'm a bit proud of myself!

Anyway, here's the code:

#include <iostream>
using namespace std;

int main()
{
int a;
int b;
string c;
int total;
string end;

           cout << "Select what type of calculation you'll make from +, -, x and /" << endl;
           cin >> c;
           cout << "Specify first value of the calculation:";
           cin >> a;
           cout << "Specify second value of the calculation:";
           cin >> b;
           if (c=="+")
           total=a+b;
           if (c=="-")
           total=a-b;
           if (c=="x")
           total=a*b;
           if (c=="/")
           total=a/b;
           
           cout << "the total is:" << total << endl;
           cout << "Type anything to end this stupid calc-wannabe" << endl;
           cin >> end;
           
           return 0;
           }

Yes... I know... it's stupid, futil and pathetic... but it works! :p

Nevertheless I'd appreciate if someone could tell me how to properly terminate the program.

Anyway... what d'ya think?
__________________
PORTUGALPORTUGA
PORTUGALPORTUGA
PORTUGALPORTUGA
PORTUGALPORTUGA
PORTUGALPORTUGA
Broax is offline   Reply With Quote
Old Feb 1st, 2005, 2:04 PM   #70
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Looks good. However, I suggest either using else if or a switch-case statement.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:32 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC