Learning C++

This is a discussion on Learning C++ within the C++ Programming forums, part of the General Programming Boards category; I just started to learn c++ and wrote a simple little calculator program, but i wanted to know what i ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    3

    Question Learning C++

    I just started to learn c++ and wrote a simple little calculator program, but i wanted to know what i would need to do to accept command line arguments so that it can work like this:

    C:\> calc.exe 5 * 5

    5 * 5 = 25

    Here is the code that i got so far:

    ======================calc.cpp===================
    Code:
    #include <iostream>
    
    int main()
    {
    
    int v1, v2, total;
    
    char type;
    
    std::cin >> v1 >> type >> v2;
    
    switch ( type )
    	{
    		case '+':
    		total = v1 + v2;
    		std::cout << v1 << " + " << v2 << " = " << total << std::endl;
    		break;
    		
    		case '-':
    		total = v1 - v2;
    		std::cout << v1 << " - " << v2 << " = " << total << std::endl;
    		break;
    
    		case '*':
    		total = v1 * v2;
    		std::cout << v1 << " * " << v2 << " = " << total << std::endl;
    		break;
    
    		case '/':
    		total = v1 / v2;
    		std::cout << v1 << " / " << v2 << " = " << total << std::endl;
    		break;
    
    		default:
    		std::cout << "Incorrect format (number * number)" << std::endl; 
    		
    	}
    
    	return 0;
    
    }
    ==============================================

    Any help is appreciated.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    3
    Here is what i got so far but it doesn't want to compile:

    Code:
    #include <iostream>
    
    int main(int argc, char *argv[])
    {
    
    int total;
    
    	switch ( argv[2] )
    		{
    			case '+':
    			total = argv[1] + argv[3];
    			std::cout << argv[1] << " + " << argv[3] << " = " << total << std::endl;
    			break;
    					
    			case '-':
    			total = argv[1] - argv[3];
    			std::cout << argv[1] << " - " << argv[3] << " = " << total << std::endl;
    			break;
    
    			case '*':
    			total = argv[1] * argv[3];
    			std::cout << argv[1] << " * " << argv[3] << " = " << total << std::endl;
    			break;
    
    			case '/':
    			total = argv[1] / argv[3];
    			std::cout << argv[1] << " / " << argv[3] << " = " << total << std::endl;
    			break;
    			*/
    			default:
    			std::cout << "Incorrect format (number * number)" << std::endl; 
    		
    		}
    
    	return 0;
    
    }
    Below are the compile errors that i am getting:

    Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
    calc2.cpp:
    Error E2383 calc2.cpp 12: Switch selection expression must be of integral type i
    n function main(int,char * *)
    Error E2085 calc2.cpp 15: Invalid pointer addition in function main(int,char * *
    )
    Warning W8057 calc2.cpp 41: Parameter 'argc' is never used in function main(int,
    char * *)
    *** 2 errors in Compile ***

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    Well each argv[n] is a \0 terminated character string.

    So to test a single letter, use switch ( argv[2][0] )

    > total = argv[1] + argv[3];
    Being strings, you need something like strtol() or strtod() to convert from a string to an int.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    3
    Cool it works, thanks a lot for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Machine Learning with Lego Mindstorms
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 01-30-2009, 01:34 PM
  2. Best Approach for Learning
    By UCnLA in forum C Programming
    Replies: 5
    Last Post: 03-21-2008, 02:35 AM
  3. Need Help On a Simple Bank Program
    By oobootsy1 in forum C# Programming
    Replies: 9
    Last Post: 08-08-2005, 10:51 AM
  4. Fun Learning a New Language
    By UnregdRegd in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 09-30-2003, 10:03 PM
  5. Learning Rate Of C++
    By Krak in forum C++ Programming
    Replies: 27
    Last Post: 01-29-2003, 12:53 PM

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