Thread: c++ calculater (need help with pointer and trigger??)

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    56

    c++ calculater (need help with pointer and trigger??)

    i have written this c++ code a few days ago,
    now my problem is i want to have the user input and check if its y or n but i get this error message
    Code:
    ANSI c++ forbits comparison between pointers and triggers
    here's my c++ code
    Code:
    #include <iostream.h>
    #include <stdlib.h>
    
    int main()
    {
          int choice;
          int x1;
          int x2;
          int mem1;
          int mem2;
          int answer;
          char drie ;
          cout <<" 1 = +\n";
          cout <<" 2 = -\n";
          cout <<" 3 = *\n";
          cout <<" 4 = /\n";
          cout <<" 5 = %\n";
          cout <<" 6 = documentation/license\n";
          cout <<" enter your choice: \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n ";
          cin>>choice;
          if (choice == 1){
          cout <<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nnenter number 1: ";
          cin>>x1;
          cout <<"\nenter number 2: ";
          cin>>x2;
          answer = x1 + x2;
          cout <<"your answer :"<<answer<<"\n";
          }
          if (choice == 2){
          cout <<"\nenter number 1: ";
          cin>>x1;
          cout <<"\nenter number 2: ";
          cin>>x2;
          answer = x1 - x2;
          cout <<"your answer :"<<answer<<"\n";
          }
          if (choice == 3){
          cout <<"\nenter number 1: ";
          cin>>x1;
          cout <<"\nenter number 2: ";
          cin>>x2;
          answer = x1 * x2;
          cout <<"your answer :"<<answer<<"\n";
          }
          if (choice == 4){
          cout <<"\nenter number 1: ";
          cin>>x1;
          cout <<"\nenter number 2: ";
          cin>>x2;
          answer = x1 / x2;
          cout <<"your answer :"<<answer<<"\n";
          }
          if (choice == 5){
          cout <<"\nenter number 1: ";
          cin>>x1;
          cout <<"\nenter number 2: ";
          cin>>x2;
          answer = x1 % x2;
          cout <<"your answer :"<<answer<<"\n";
          }
          if (choice == 6){
          cout <<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
          cout <<"welcome to tech calculator(TM) (c) stefan feenstra\n\n";
          cout <<"you may not debug or do anything to get the source code\n";
          cout <<"calculater is written in c++ 77 lines of code\n";
          cout <<"this software is freeware, you may redristrubute BUT\n";
          cout <<"you must leave a link to the official website\n";
          cout <<"due to this software is Freeware the auther takes\n"   ;
          cout <<"no responsibillety, if you do not agree to these terms,\n"  ;
          cout <<"you may not use this software\n\n\n\n";
          cout <<"known bugs, when typing a letter at a calculation you cant\n"               ;
          cout <<"type in a second number, or the program will terminate or something else\n";
          cout <<"if you need to report a bug you can send it to [email protected]\n";
          cout <<"under title of \'TECH CALCULATER BUG\'";
          }
          if (choice != 1 && choice != 2 && choice != 3 && choice != 4 && choice != 5 && choice != 6){
          cout <<"\na error acured you entered a wrong number/letter you entered as choice\'"<<choice<<"\'";
          }
          //system("PAUSE");
          cout <<"return ? 1 or 0 : ";
          cin>>drie;
          if (drie == "y"){
          cout <<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
          return main();
          }
          if (drie != "n"){
          return 0;
    }
    }
    P.S. sorry i did not commend it
    if x == y , y == x

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    you mind showing which line gives the error

    also iostream.h is depreated. Do a quick search and you'll find the details about that.

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    56
    the error are in these two lines
    Code:
          //system("PAUSE");
          cout <<"return ? y or n : ";
          cin>>drie;
          if (drie == "y"){ //error
          cout <<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
          return main();
          }
          if (drie != "n"){ //second error
          return 0;
    }
    }
    the two errors are the same so i only posted them once
    if x == y , y == x

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > if (drie == "y"){ //error
    drie is a char, not a string

    So use a char constant to compare with - single quotes
    if (drie == 'y')
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    56
    thanks for helping me,
    no i have a bug when i hit y it just exites the application,
    but he needs to return to main for a other calculation
    if x == y , y == x

  6. #6
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Ok. Let me introduce you to a very nice concept called "switch..case".

    Your code originally makes use of a lot of if..than statements. Granted this is valid, but it leaves you more likely to make mistakes. Take a look at this and see if you can make heads or tails of it:

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    
    int main()
    {
          int choice;
          int x1;
          int x2;
          int answer;
          char drie ;
          	  
    	  cout << "1 = +\n";
          cout << "2 = -\n";
          cout << "3 = *\n";
          cout << "4 = /\n";
          cout << "5 = %\n";
          cout << "6 = Documentation/License\n\n";
          cout << "Enter your choice: ";
          cin >> choice;
    	  cout << "\n";
    
    	  switch (choice)
    	  {
    		case 1:
    			cout << "Enter first number: ";
    			cin >> x1;
    
    			cout << "Enter second number: ";
    			cin >> x2;
    
    			answer = (x1 + x2);
    
    			cout << "\nYour answer: " << answer << "\n\n";
    		break;
    
    		case 2:
    			cout << "Enter first number: ";
    			cin >> x1;
    			
    			cout << "Enter second number: ";
    			cin >> x2;
    			
    			answer = (x1 - x2);
    			
    			cout << "\nYour answer: " << answer << "\n\n";
    		break;
          
    		case 3:
    			cout << "Enter first number: ";
    			cin >> x1;
    			
    			cout << "Enter second number: ";
    			cin >> x2;
    			
    			answer = (x1 * x2);
    			
    			cout << "\nYour answer: " << answer << "\n\n";
    		break;
    
    		case 4:
    			cout << "Enter first number: ";
    			cin >> x1;
    			
    			cout << "Enter second number: ";
    			cin >> x2;
    			
    			answer = (x1 - x2);
    			
    			cout << "\nYour answer: " << answer << "\n\n";
    		break;
          
    		case 5:
    			cout << "Enter first number: ";
    			cin >> x1;
    			
    			cout << "Enter second number: ";
    			cin >> x2;
    			
    			answer = (x1 % x2);
    			
    			cout << "\nYour answer: " << answer << "\n\n";
    		break;
          
    		case 6:
    			cout <<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
    			cout <<"welcome to tech calculator(TM) (c) stefan feenstra\n\n";
    			cout <<"you may not debug or do anything to get the source code\n";
    			cout <<"calculater is written in c++ 77 lines of code\n";
    			cout <<"this software is freeware, you may redristrubute BUT\n";
    			cout <<"you must leave a link to the official website\n";
    			cout <<"due to this software is Freeware the auther takes\n"   ;
    			cout <<"no responsibillety, if you do not agree to these terms,\n"  ;
    			cout <<"you may not use this software\n\n\n\n";
    			cout <<"known bugs, when typing a letter at a calculation you cant\n"               ;
    			cout <<"type in a second number, or the program will terminate or something else\n";
    			cout <<"if you need to report a bug you can send it to [email protected]\n";
    			cout <<"under title of \'TECH CALCULATER BUG\'";
    		break;
    
    		default:
    			cout << "An error occured: You selected an invalid choice: " << choice << "\n\n";
          }
          
          cout << "Enter another function? ";
          cin >> drie;
    
    	  switch (drie)
    	  {
    		case 'Y':
    			main();
    		break;
    
    		case 'y':
    			main();
    		break;
    
    		case 'N':
    			return(0);
    		break;
    
    		case 'n':
    			return(0);
    		break;
    
    		default:
    			return(0);
    	  }
          
    	  return(0);
    }
    Hope this helps!

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    56
    yeah that helps thanks for all the help guys
    and that latest post of me i forgot to change int to char,
    that was kind of stupid
    if x == y , y == x

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Except that you aren't allowed to call main() anymore. Just put everything in a loop. Also there is no real need for:
    Code:
    case 'N':
      return 0;
      break;
    case 'n':
      return 0;
      break;
    There are a couple ways around it:
    way 1:
    Code:
    switch (tolower(drie))
    {
      case 'n':
        return 0;
      case 'y'
        // do something
        break;
    }
    Way 2:
    Code:
    switch(drie)
    {
      case 'n':
      case 'N':
        return 0; // no need for a break after this
      case 'Y':
      case 'y':
        //do something
        break;
    }

  9. #9
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Quote Originally Posted by Thantos
    Except that you aren't allowed to call main() anymore. Just put everything in a loop. Also there is no real need for:
    Code:
    case 'N':
      return 0;
      break;
    case 'n':
      return 0;
      break;
    There are a couple ways around it:
    way 1:
    Code:
    switch (tolower(drie))
    {
      case 'n':
        return 0;
      case 'y'
        // do something
        break;
    }
    Way 2:
    Code:
    switch(drie)
    {
      case 'n':
      case 'N':
        return 0; // no need for a break after this
      case 'Y':
      case 'y':
        //do something
        break;
    }
    That's all very true. I just used main() because he used it and really didn't want to code a loop for him. That's his job.

    As for the ways around the multiple cases, I never considered tolower - thanks! That'll help in some of my programs (I literally have 52 case statements, I can cut that in half).

  10. #10
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Quote Originally Posted by Lithorien
    That'll help in some of my programs (I literally have 52 case statements, I can cut that in half).
    Wow. I hope that I don't see that code. Ever.
    The length of that switch statement must be staggering, considering that many statements within your case-blocks are exactly the same in your example.
    Keyboards aren't expensive, but a little saving could help nevertheless.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  11. #11
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Code:
    	  switch (choice)
    	  {
    		case 1:
    			cout << "Enter first number: ";
    			cin >> x1;
    
    			cout << "Enter second number: ";
    			cin >> x2;
    
    			answer = (x1 + x2);
    
    			cout << "\nYour answer: " << answer << "\n\n";
    		break;
    
    		case 2:
    			cout << "Enter first number: ";
    			cin >> x1;
    			
    			cout << "Enter second number: ";
    			cin >> x2;
    			
    			answer = (x1 - x2);
    			
    			cout << "\nYour answer: " << answer << "\n\n";
    		break;

    you should really avoid duplication of code whenever possible. a better way would be something like:

    Code:
     cin >> choice;
     cout << "\n";
     if(choice < 1 || choice > 6) {
      // ...report...
      } else {
      cout << "Enter first number: ";
      cin >> x1;
      cout << "Enter second number: ";
      cin >> x2;
      switch (choice) {
       case 1: answer = (x1 + x2);
       break;
       //...etc...
       }
     cout << "\nYour answer: " << answer << "\n\n";
     }
    also noticed a typo here - you subtract when you were supposed to divide:

    Code:
    case 4:
    			cout << "Enter first number: ";
    			cin >> x1;
    			
    			cout << "Enter second number: ";
    			cin >> x2;
    			
    			answer = (x1 - x2);
    ...but don't forget - divide by zero is a big no-no, so be sure to check for that...

    [edit]
    damn I'm slow. =)
    [/edit]
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  12. #12
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Quote Originally Posted by Sang-drax
    Wow. I hope that I don't see that code. Ever.
    The length of that switch statement must be staggering, considering that many statements within your case-blocks are exactly the same in your example.
    Keyboards aren't expensive, but a little saving could help nevertheless.
    It is pretty staggering. I also havn't gone through and optomized yet, so my code just works. :P

    Yeah. I believe I have 300 lines in that switch statement alltogether. It's a thing of great fear. O.O

  13. #13
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    If you want to make a better expression parser google for recursive decent parsers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM