syntax errors

This is a discussion on syntax errors within the C++ Programming forums, part of the General Programming Boards category; Hello, I'm having a problem with with code. Here is my code Code: #include <stdio.h> #include <iostream> #include namespace std ...

  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
    22,302
    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?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    22,302
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    2,918
    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,866
    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
    Posts
    23,020
    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, 11: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, 05: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, 03:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21