Newbie needs a bit of help

This is a discussion on Newbie needs a bit of help within the C++ Programming forums, part of the General Programming Boards category; I'm new to coding and I know the code isn't fully functional but bare with me and help me deal ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    9

    Newbie needs a bit of help

    I'm new to coding and I know the code isn't fully functional but bare with me and help me deal with the problem at hand, thanks!

    Code:
    #include <iostream>
    using namespace std;
    
    void numcheck();
    int add(int,int);
    int subtract(int,int);
    int divide(int,int);
    int multiply(int,int);
    
    
    
    
    void main()
    {
    	int input;
    	int num1,num2;
    
    	cout << "Pick your Method: " << endl;
    	cout << "1)Add\n" << "2)Subtract\n" << "3)Divide\n" << "4)Multiple" << endl;
    
    	cin >> input;
    
    	  switch (input)
    	  {
    		case 1://subtract
    			 numcheck();
    			 cin >> num1,num2;
    			 int s;
    			 s = subtract(num1,num2);
    			 cout << s << endl;
    			 break;
    		case 2://divide
    			 numcheck();
    			 cin >> num1,num2;
    			 int d;
    			 d = divide(num1,num2);
    			 cout << d << endl;
    			 break;
    		case 3://multiply
    			 numcheck();
    			 cin >> num1,num2;
    			 int m;
    			 m = multiply(num1,num2);
    			 cout << m << endl;
    	  }
    }
    
    void numcheck()
    {
    	cout << "Enter two numbers for the Equation"<< endl;
    }
    
    int add(int a, int b)
    {
    	int add;
    	add = a+b;
    	return(add);
    }
    
    int subtract(int a,int b)
    {
    	int subtract;
    	subtract = a-b;
    	return(subtract);
    }
    
    int divide(int a, int b)
    {
    	int divide;
    	divide = a/b;
    	return(divide);
    }
    
    int multiply(int a,int b)
    {
    	int multiply;
    	multiply = a*b;
    	return(multiply);	
    }
    Error:
    c:\documents and settings\compaq_administrator\my documents\visual studio 2008\projects\calculator\calculator\main.cpp(29) : warning C4700: uninitialized local variable 'num2' used

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Code:
    cin >> num1,num2;
    Should be

    Code:
    cin >> num1  >> num2;
    Also, and this is just informational more than anything.

    Code:
    int add(int a, int b)
    {
      return a+b;
    }
    Works too. Also, you shouldn't name variables the same name as the function. I am surprised that even compiled without mentioning that fact to you.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    9
    yea i was surprised too, maybe microsoft allows you to do this

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Well its interesting trivia to know. That doesn't mean you should do it.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    It compiles because you're using the comma operator which says, basically, evaluate the left, then the right, and have the whole expression take on the value of the right operand.

    It's like saying
    Code:
    cin >> a;
    b;
    (because you're not using the value of the expression). And, of course, a statement like "b;" does absolutely nothing.

    BTW, if you enable warnings your compiler should warn you about this sort of thing.
    Code:
    $ cat shiftcomma.cpp
    #include <iostream>
    
    int main() {
        int a, b;
        std::cin >> a, b;
        return 0;
    }
    $ g++ -W -Wall -ansi -pedantic -g shiftcomma.cpp -o shiftcommashiftcomma.cpp: In function ‘int main()’:
    shiftcomma.cpp:5: warning: right-hand operand of comma has no effect
    $
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,027
    http://cpwiki.sf.net/void_main
    Simply put, don't use it.
    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.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    9
    ok here is my updated program

    Code:
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    void numcheck();
    int add(int,int);
    int subtract(int,int);
    int divide(int,int);
    int multiply(int,int);
    void selection_case();
    
    
    
    void main()
    {
    	
    	int restart;
    	
    
    	cout << "Pick your Method: " << endl;
    	cout << "1)Subtract\n" << "2)Divide\n" << "3)Multiple\n" << "4)Add" << endl;
    
    	selection_case();
    	
    
    	cout << "Make Another Selection(Press R): " << endl;
    
    	cin >> restart;
    
    	if (restart = 'r')
    	{
           system("CLS");
    	   selection_case();
    	}
    	else
    	{
    	cout <<	"Thanks" << endl;
    	exit(1);
    	}
    
    }
    
    void numcheck()
    {
    	cout << "Enter two numbers for the Equation"<< endl;
    }
    
    int add(int a, int b)
    {
    	return a+b;
    }
    
    int subtract(int a,int b)
    {
    	return a-b;
    }
    
    int divide(int a, int b)
    {
    	return a/b;
    }
    
    int multiply(int a,int b)
    {
    	return a*b;	
    }
    
    void selection_case()
    {
    
    	int num1,num2;
    	int input;
    	cin >> input;
    
    	switch (input)
    	  {
    		case 1://subtract
    			 numcheck();
    			 cin >> num1 >> num2;
    			 int s;
    			 s = subtract(num1,num2);
    			 cout << s << endl;
    			 break;
    		case 2://divide
    			 numcheck();
    			 cin >> num1 >> num2;
    			 int d;
    			 d = divide(num1,num2);
    			 cout << d << endl;
    			 break;
    		case 3://multiply
    			 numcheck();
    			 cin >> num1 >> num2;
    			 int m;
    			 m = multiply(num1,num2);
    			 cout << m << endl;
    		case 4://add
    			numcheck();
    			cin >> num1 >> num2;
    			int a;
    			a = add(num1,num2);
    			cout << a << endl;
    	  }
    }
    my code now has a little bit of function. the problem I've run into is not exactly an error. In bold, is where I believe I'm having trouble. When I get to this point I am suppose to type in r if I want to clear the screen and ask the question again. I do this but it just gives me the end of program "press any button to continue". I'm thinking it may have something to do with getchar(); , but Im not too sure.

  8. #8
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    Code:
    	if (restart = 'r')
    should be
    Code:
    	if (restart == 'r')
    Last edited by kcpilot; 10-13-2008 at 11:39 AM. Reason: typo

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    And how do you expect cin to read 'r' into an int?

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,027
    Get rid of void main.
    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.

  11. #11
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    char restart

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    And don't forget your breaks in your switch statements.

    Oh, and don't declare variables in switch statements, either. http://stackoverflow.com/questions/9...itch-statement
    (Unless you have each case statement in its own block.)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 64 bit testing
    By DrSnuggles in forum C++ Programming
    Replies: 7
    Last Post: 11-20-2007, 03:20 AM
  2. Bit processing in C
    By eliomancini in forum C Programming
    Replies: 8
    Last Post: 06-07-2005, 11:54 AM
  3. Porting from 32 bit machine to 64 bit machine!
    By anoopks in forum C Programming
    Replies: 10
    Last Post: 02-25-2005, 08:02 PM
  4. Copy bit to bit
    By Coder2Die4 in forum C Programming
    Replies: 15
    Last Post: 06-26-2003, 10:58 AM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-26-2001, 12:45 AM

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