Thread: syntax errors

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    33

    syntax errors

    Hello,
    I'm having a problem with with code. Here is my code

    Code:
    #include <stdio.h>
    #include <iostream>
    #include namespace std
    
    main()
    {
    	int *num1;
    	int *num2;
    	int *op;
    
    	num1 = new int;
    	num2 = new int;
    	op = new int;
    
    	cout<<"Enter first number:"; 	//error: 'cout' was not declared in scope
    	cin>> *num1;			//error: 'cin' was not declared in scope
    
    	cout<<"Enter second number:";
    	cin>> *num2;
    
    	while(*op != 6)
    	{
    		cout<<"1: Addition"
    	    	    <<"2: Subtraction"
    	    	    <<"3: Multiplication"
    	    	    <<"4: Division"
    	    	    <<"5: Modulus"
    	    	    <<"6: Exit\n"
    	    	    <<"Please enter a choice:";
    		cin<< *op;
    
    		switch(*op)
    		{
    			case (1):
    				cout<< *num1 + *num2;
    				break;
    			case (2):
    				cout<< *num1 + *num2;
    				break;
    			case (3):
    				cout<< (*num1) * (*num2);
    				break;
    			case (4):
    				if(*num2 == 0)
    					cout<<"Unable to perform division."
    				    	    << " Please enter a nonzero second number";
    				else
    					cout<< (*num1) / (*num2);
    					break;
    			case (5):
    				cout<< (*num1) % (*num2);
    				break;
    			case (6):
    				break;
    			default:
    				cout<< "Please enter valid option";
    				break;
    		}
    	}
    
    }
    when compiling i would get these errors

    calculator.cpp: In function `int main()':
    calculator.cpp:30: error: no match for 'operator<<' in 'std::cin << *op'

    line 30 being
    Code:
    cin<< *op;
    any idea on what could cause this? I'm new to c++, please bare with me.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    	while(*op != 6)
    	{
    		cout<<"1: Addition"
    	    	    <<"2: Subtraction"
    	    	    <<"3: Multiplication"
    	    	    <<"4: Division"
    	    	    <<"5: Modulus"
    	    	    <<"6: Exit\n"
    	    	    <<"Please enter a choice:";
    		cin<< *op;

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    CommonTater has pointed out the source of the error, but actually you could have found it yourself quite easily because your compiler provided the line number (30) in the error message.

    Besides this, you failed to match new with delete, but you don't need them here, so why use them?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    33
    can you show me an example of delete? do I always need to delete when an anonymous variable is looped?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by aromash
    can you show me an example of delete?
    You should have learnt it when you learnt to use new. My point is that you could have written:
    Code:
    int num1;
    int num2;
    int op;
    
    cout << "Enter first number:";
    cin >> num1;
    
    cout << "Enter second number:";
    cin >> num2;
    Now you do not need delete.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Also the line
    Code:
    #include namespace std
    should be
    Code:
    using namespace std;
    Jim

  7. #7
    Registered User
    Join Date
    Aug 2011
    Location
    Montreal, Quebec, Canada
    Posts
    73
    While we're at it:

    - You should not declare main without return type. It is either int main() or int main(int argc, char** argv) or nothing.
    - You should use <cstdio> when using that header, but you are not even using it so you can just remove it.
    - Parentheses are not necessary in the case statements.
    - In the line displaying the menu you do not need the << operator on each line. You can simply finish that line with an \ and continue the text on the other line.

  8. #8
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    All of these problems could be avoided if you just selected a free IDE that offered syntax highlighting.........
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Alexandre View Post
    - You should not declare main without return type. It is either int main() or int main(int argc, char** argv) or nothing.
    This one is puzzling. Because the standard mandates that all function must have a return type, this code should not compile. Not to mention #include namespace std probably wouldn't compile very well or spot out a lot of warnings.
    So that begs the question: what compiler/IDE are you using?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. syntax errors
    By newguyps in forum C++ Programming
    Replies: 5
    Last Post: 07-29-2011, 10:25 AM
  2. why i get 2 syntax errors here .
    By transgalactic2 in forum C Programming
    Replies: 2
    Last Post: 11-30-2008, 03:18 PM
  3. HELP!! Syntax errors
    By theused96 in forum C Programming
    Replies: 2
    Last Post: 09-20-2007, 04:29 PM
  4. syntax errors
    By nesagsar in forum C++ Programming
    Replies: 33
    Last Post: 12-14-2006, 02:03 PM
  5. errors syntax
    By bazzano in forum C Programming
    Replies: 2
    Last Post: 09-30-2005, 02:44 AM