Simple program, not so simple problem

This is a discussion on Simple program, not so simple problem within the C++ Programming forums, part of the General Programming Boards category; The purpose of my program is to take two numbers and an operator (+, -, *, /) from the user ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    2

    Simple program, not so simple problem

    The purpose of my program is to take two numbers and an operator (+, -, *, /) from the user and perform the operation corresponding with the operator the user chose.

    like a lame calculator.

    but its not working.

    The following code will not compile in Visual C++ 2005 Express v8:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	int firstNum, secNum;
    	char oper;
    	
    	cout << "Please type in the first number" << endl;
    	cin >> firstNum;
    	
    	cout << "Please type in the operation" << endl;
    	cin >> oper;
    	
    	cout << "Please type in the second number" << endl;
    	cin >> secNum;
    
    
    	switch (oper)
    	{
    		case '+':
    			cout << firstNum << " + " << secNum " = " << firstNum + secNum << endl;   //this is line 23
    			break;
    		case '-':
    			cout << firstNum << " - " << secNum " = " << firstNum - secNum << endl; //line 26
    			break;
    		case '*':
    			cout << firstNum << " * " << secNum " = " << firstNum * secNum << endl; //line 29
    			break;
    		case '/':
    			if (firstNum == 0 || secNum == 0)
    			{
    				cout << "Cannot divide into/divide by zero" << endl;
    			} else 
    			{
    				cout << firstNum << " / " << secNum << " = " << firstNum / secNum << endl;
    			}
    			break;
    		default:
    			cout << "Invalid Operator" << endl;
    	}	
            return 0;
    }

    I get the following errors:

    c:\learning\calulate\calulate\source.cpp(23) : error C2143: syntax error : missing ';' before 'string'
    c:\learning\calulate\calulate\source.cpp(23) : warning C4554: '<<' : check operator precedence for possible error; use parentheses to clarify precedence
    c:\learning\calulate\calulate\source.cpp(23) : error C2296: '<<' : illegal, left operand has type 'const char [4]'
    c:\learning\calulate\calulate\source.cpp(26) : error C2143: syntax error : missing ';' before 'string'
    c:\learning\calulate\calulate\source.cpp(26) : warning C4554: '<<' : check operator precedence for possible error; use parentheses to clarify precedence
    c:\learning\calulate\calulate\source.cpp(26) : error C2296: '<<' : illegal, left operand has type 'const char [4]'
    c:\learning\calulate\calulate\source.cpp(29) : error C2143: syntax error : missing ';' before 'string'
    c:\learning\calulate\calulate\source.cpp(29) : error C2296: '<<' : illegal, left operand has type 'const char [4]'
    Last edited by nolsen; 01-18-2008 at 09:25 AM.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Missing a << here
    Code:
    " + " << secNum " = " << firstNum +
    I do it all the time.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    2
    yeah, but you're able to recognize it. haha

    it happens to me all of the time to.

    thank you so much

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 02:30 PM
  2. Running Program Problem
    By warfang in forum C++ Programming
    Replies: 10
    Last Post: 03-28-2007, 02:02 PM
  3. simple login program problem
    By suckss in forum C Programming
    Replies: 11
    Last Post: 11-11-2006, 04:02 PM
  4. Problem with a simple program
    By Salgat in forum C Programming
    Replies: 10
    Last Post: 06-15-2006, 05:57 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM

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